a,b = 1,2 print(a,b) a,b = b,a print(a,b)
tu1 = (1) print(tu1,type(tu1)) #1 <class 'int'>
tu2 = (1,)
print(tu2,type(tu2)) #(1,) <class 'tuple'>
tu3 = ([1])
print(tu3,type(tu3)) #[1] <class 'list'>
tu4 = ([1],)
print(tu4,type(tu4)) #([1],) <class 'tuple'>
li = [1,2,33,33,2,1,4,5,6,6] set1 = set(li) print(set1) #{1, 2, 33, 4, 5, 6} li = list(set1) print(li) #[1, 2, 33, 4, 5, 6]
for i in range(0,10,-1): print(i)
運行結果:成功運行,不輸出任何結果,不報錯面試
d =lambda p:p*2 t =lambda p:p*3 x = 2 x = d(x) #4 x = t(x) #12 x = d(x) #24 print(x) #24
解析:和匿名函數配合使用的內置函數:min max filter map sorted函數
print(list(map(lambda tup:{tup[0]:tup[1]},zip((('a'),('b')),(('c'),('d')))))) # 上面一行代碼等於下面三行,能夠實現本題功能 ret = zip((('a'),('b')),(('c'),('d'))) res = map(lambda tup:{tup[0]:tup[1]},ret) print(list(res)) #數據類型強轉能夠代替循環打印
def multipliers(): return [lambda x:i*x for i in range(4)] print([m(2) for m in multipliers()]) #[6, 6, 6, 6]
# 請修改multipliers的定義來產生指望的結果 def multipliers(): return (lambda x:i*x for i in range(4)) print([m(2) for m in multipliers()]) #[0, 2, 4, 6]