第10章--語句簡介
pass 空佔位符
yield 生成器函數 ??
nonlocal 命名空間(3.0) ??
try/except 捕捉異常 ??
raise 觸發異常 ??
assert 調試檢查 assert x>y,'x too small'
with/as 環境管理器(2.6) ?? with open('data') as myfile:process(myfile)
冒號,縮進語法,對齊--所見即所得
一行多條語句,用;隔開 x=1;y=2;print(x+y)
任何在(), [], {}中的內容都可以跨行
if(a==b and
c==d and):
簡單語句可以直接跟在:後面 if x > y:print(x)
input('please enter your name:') 接受輸入函數
交互小例子:
while True:
i = input('please enter x and n, we will calculate power(x, n)\n')
if(i == 'stop'):break
l = i.split()
try:
print(int(l[0]) ** int(l[1]))
except:
print('bad!'*6)
print('bye')
第11章--複製_表達式_打印
name,age='cai',42 <==> [name,age]=['cai', 42]
a,b,c='cai' >>> a='c',b='a',c='i'
a,*b='cai' >>> a='c', b=['a', 'i'] (3.0)
等號左邊,不能有兩個帶*的元素
帶*的元素可以在中間,會自動匹配 a,*b,c,d='qwert' b=['w','e']
tips:
增強賦值,對於列表和字典是在原處修改
l=m=[1,2] l +=[3,4] l,m>>>([1,2,3,4],[1,2,3,4])
l=m=[1,2] l = l + [3,4] l,m>>>([1,2,3,4],[1,2])
以保留字命名的文件,無法導入
3.0標準打印print
print([object,……][, sep=' '][, end='\n'][, file=sys.stdout])
流重定向 sys.stdout=open('log.txt','a') 用print寫入文件log.txt
log=open('log.txt','a') print(data, file=log)