The first 29 learning python generator

Builder:

# Ternary expressions with three operation 
name = "zhangshan"
RET = "SB" IF name == "zhangshan" the else "SMATE" # determines whether the name is equal zhangshan, SB returns equal, but the determination is True, SB zhangshan
# is placed before
Print (RET)


# list analyzing
list = []
for I in Range (10):
list.append (I)
Print (list)

L = [I for I in Range (10)]
Print (L )



# 2 embodiment generating generator 
laomuji = ( "egg% s"% i for i in range (10) ) # becomes parentheses, is the object
laomuji1 = [ "egg% s"% i for i in range (10) ] # brackets generate a list of a comparison of total content
Print (laomuji) # <Generator Object <genexpr> AT 0x024C3570>
<AT 0x024C3570 Generator Object <genexpr>> Print (laomuji1) #

DEF Test ():
the yield. 1
the yield 2
G = Test () to get a generator object # <Object generator Test AT 0x02623330>
Print (G)

Guess you like

Origin www.cnblogs.com/jianchixuexu/p/11569555.html