條件判斷語句:
if … elif…elif…else…
中間可以添加更多的elif,它是else if 的縮寫,也可以不要,else也可以不添加
循環語句:
For x in a:
A是list類型的或其他的有集合性質的
Else也可以配合for使用,當for循環正常結束後就進入else語句執行,注意else和誰對齊就和誰配對,如下:
y=int(input(“pleaseinput an integer:”))
for x in range(0,10):
if x==y:
print("OK")
break;
else
print(“No”)
如果你輸入5就會輸出OK,輸入一個大於9的數就輸出no
函數定義:
Def fname(param):
也可以用*name表示多個參數,**name表示字典
形如:def concat(*args,sep=’/’):
ReturnSep.join(args);
如:concat(‘usr’,’application’)會返回’usr/application’
你也可以使用**args作爲參數
def parrot(voltage,state='a stiff',action='voom'):
... print("-- Thisparrot wouldn't",action,end=' ')
... print("if youput",voltage,"voltsthrough it.",end=' ')
... print("E's",state,"!")
...
>>> d={"voltage":"fourmillion","state":"bleedin' demised","action":"VOOM"}
>>> parrot(**d)
-- This parrot wouldn't VOOM if you putfour million volts through it. E's bleedin' demised !
Lamba的用法:
如:
def test(x):
return lambda a: a+x+2
b=test(10)
print(b(11))
test(x) 返回的其實是一個函數,這個函數有一個參數a,返回的值就是a+x+2,
x是調用test(x)給出的