Python full stack day18 (ternary operation, list parsing, generator expression)

Source: Internet
Author: User

One, what is the generator

Can be understood as a data type that automatically implements the iterator protocol (other data types need to call their own built-in __iter__ method), so the generator is an iterative object.

Second, the representation of the generator classification in Python

1, Generator function: The general function definition, however, returns the result using the yield statement instead of the return statement. The yield statement returns one result at a time, in the middle of each result, suspends the state of the function so that the next time it leaves the execution day18-5.py

def test ():    yield 1    yield 2g=test () print (g) print (g.__next__ ()) print (g.__next__ ()) <generator Object test At 0x000001d40cf6f3b8>12

Unlike return, which can yield multiple values because it is an iterative object, there is a next method

2, Generator expression: Similar to list derivation, but the generator returns an object that produces results on demand, rather than building a list of results at a time

  

Three, ternary expressions, if the expression is set to return the previous value, otherwise return the value after day18-7.py

Name= ' Zhangsan ' Print (' SB ' if name = = ' Zhangsan ' Else ' handsome ')

  

Four, a list of expressions, an example of day18-8.py

#使用for循环实现生鸡蛋egg_list =[]for i in range:    egg_list.append (' egg%s '%i) print (egg_list) #使用列表表达式生鸡蛋l =[' egg%s '%i for  I in range (]print) (l) [' Egg 0 ', ' egg 1 ', ' Egg 2 ', ' Egg 3 ', ' Egg 4 ', ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 ' [' Egg 0 ', ' egg 1 ', ' Egg 2 ', ' Egg 3 ', ' Egg 4 ', ' Egg 5 ', ' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 '

Using for loops and list expression generation is the same as the result

Use ternary expressions plus an if criterion (ternary expression can be ternary or two yuan, but not four)

l=[' egg%s '%i for I in range (Ten) if I>5]print (l) [' Egg 6 ', ' Egg 7 ', ' Egg 8 ', ' Egg 9 ']

The above list generated using an expression is directly in memory, and you can use a builder expression

egg_list=[' egg%s '%i for I in range]laomuji= (' egg%s '%i for I in range) print (Laomuji) print (Next (Laomuji))    # The essence of next is to call __next__print (laomuji.__next__ ()) print (Next (Laomuji)) <generator object <genexpr> at 0x000001d39c97f3b8> eggs 0 eggs 1 eggs 2

PS: cannot be unrestricted next because the definition of the generator when the range 0-9 so up to the egg 9

Summarize:

1, the list parsing [] replaced () is the generator expression

2, list parsing and builder expressions are a convenient way to program, but generator expressions are more memory-saving

3,python not only so the iterator protocol makes the for loop more generic. Most built-in functions also access objects using an iterator protocol. For example, the SUM function is a python built-in function that accesses an object using an iterator protocol, and the generator implements an iterator protocol, so we can directly function to calculate the sum of a series of values

Print (SUM (x * * 2 for X in range (4))) 14

  

Python full stack day18 (ternary operation, list parsing, generator expression)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.