圣诞节的整理前两周的内容2
一、b模式:
# f=open('1','rb') # data=f.read() # print(data) # f=open('test1','wb') # f.write(bytes('hello',encoding='utf-8')) #编码方法1 # f.write('杨戬'.encode('utf-8')) #编码方法2
二、迭代器和生成器:
#方法一 # ll=[1,2,3,4] # for item in ll: # print(item) # x='hello' # iter_test=x.__iter__() # # print(iter_test) # print(iter_test.__next__()) # print(iter_test.__next__()) # print(iter_test.__next__()) # print(iter_test.__next__()) # print(iter_test.__next__()) # # print(iter_test.__next__()) #方法二 # ll=[1,2,3,4] # p=ll.__iter__() # print(p.__next__()) # print(p.__next__()) # print(p.__next__()) # print(p.__next__()) #方法三 # p=[1,2,3,4] # index=0 # while index<len(p): # print(p[index]) # index+=1 # # 方法四: # ll=[1,2,3,4,5] # diedai_ll=ll.__iter__() # while True: # try: # print(diedai_ll.__next__()) # except StopIteration: # print('迭代完毕,循环终止') # break # 生成器生成器!!!! # p=[1,2,3,4,5] # def test(): # yield 1 # yield 2 # yield 3 # g=test() # print(g) # # 这时,g就可以直接用.__next__()了 # print(g.__next__()) # print(g.__next__()) # print(g.__next__()) print(sum(x**2 for x in range(4)))