八.Python_三元表示式 列表解析 生成器
阿新 • • 發佈:2018-12-23
Python_三元表示式 列表解析 生成器
#16.三元表示式 列表解析 生成器 # 三元表示式 # name="tom" # print("ok" if name=="tom" else "no") #列表解析 # a=[x for x in range(10)if x<5] # print(a) #生成器 # b=(i for i in range(15) ) # print(b.__next__()) # print(b.__next__()) # print(b.__next__()) #__next__() == next() # def test(): # yield 0 # yield 1 # yield 2 # yield 3 # yield 4 # # a=test() # print(a.__next__()) # print(a.__next__()) #str # s="Hello World!" # st=s.__iter__() # print(st.__next__()) # print(st.__next__()) # # # chi=("egg%s"%i for i in range(10) if i>0) # print(next(chi)) # print(next(chi))
# # # s=['dog','cat'] # # # res=s.__iter__() # # # print(res.__next__()) # # # print(res.__next__()) # # #生成器就是函式中有yield而不是return # # #生成器 # # def test(): # # yield 1 # # yield 2 # # yield 3 # # g=test() # # print(g) # # print(g.__next__()) # #三元表示式 # #name='cat' # name='dog' # res='you are so clever' if name =='cat' else 'you are so stupy' # print(res) #列表解析 # egg_list=[] # for i in range(10): # egg_list.append('雞蛋%s'%i) # print(egg_list) # # res=['雞蛋%s'%i for i in range(10)]#生成列表好用,但是佔記憶體 # print(res) # res=['雞蛋%s'%i for i in range(10) if i <5]#只有三元表示式沒有四元表示式 # print(res) # #生成器老母雞 # laomuji=('雞蛋%s'%i for i in range(10))#生成器表示式 # print(laomuji) # print(laomuji.__next__()) # print(laomuji.__next__()) # print(next(laomuji)) #列表求和sum函式 s=[1,2,3,4,5] print(sum(s)) print(sum( i for i in range(6) ))#0-5