三元表达式:‘返回值’ if x =='条件' else ‘返回值’
'''三元表达式'''
li_apple = ['屁股%s号' %str(i+1) for i in range(10) if i<7]
print(li_apple)
'''生成器函数,基于迭代器的函数,以yield为返回值,记录本次返回状态。'''
def the_emperor():
print('马上打天下的-----> ',end='')
yield "太祖"
time.sleep(2)
print('早早被消失的-----> ', end='')
yield "惠宗"
time.sleep(2)
print('不正常继位的-----> ',end='')
yield "太宗"
time.sleep(2)
print('终于正常了的-----> ',end='')
yield "高宗"
time.sleep(2)
print('开始折腾了的-----> ',end='')
yield "世宗"
die=the_emperor()
print(die.__next__())
print(die.__next__())
print(die.__next__())
print(die.__next__())
print(die.__next__())
文件处理:
'''文件打开 r,w,读写,写入会清空原内容'''
fl=open('歌词','w')
r_f=fl.write('僕が死のうと思ったのは ウミネコが桟橋で鳴いたから')
rl = open('歌词','r')
print(rl.read())
rl.close()
'''文件操作with方法,复制文件内容至其他文件再追加内容'''
with open('歌词','r') as fla , open('歌词2','w') as rl :
ci = fla.read()
rl.write(ci)
with open('歌词2','a') as rla:
rla.write('\n波の随意に浮かんで消える 過去も啄ばんで飛んでいけ ')
'''遍历生成器'''
def mun():
for i in range(7):
ret = 0
if i%2 == 0:
ret = i **2
# else:
# continue
yield ret
for times in mun():
print(times)
生成器可被for循环,但只可被遍历一次。