挑戰(zhàn)每日打卡python基礎(chǔ)題
come with me !今日練習(xí):函數(shù)的五種參數(shù)、
1、函數(shù)使用位置參數(shù)、關(guān)鍵字參數(shù)、默認(rèn)參數(shù)、元組參數(shù)、字典參數(shù)(key),傳入單詞,計算長度
#函數(shù)使用位置參數(shù)、關(guān)鍵字參數(shù)、默認(rèn)參數(shù)、元組參數(shù)、字典參數(shù)(key),傳入單詞,計算長度
def func(a,b,c="hi",*args,**kwargs):
length = 0
length += len(a)
length += len(b)
length += len(c)
for value in args:
length += len(value)
#print(value)
for value in kwargs:
length += len(value)
#print(value)
return length
print(func("good","best"))
print(func("good","best","are","l","love","you",ki="jack",po="yogo"))
#結(jié)果:10
23
2、不可變:數(shù)字、字符串、元組、
可變:列表、字典、集合
#def add_end(l=[]): # 獨一份,全局的l,建議不要這樣傳參
def add_end(l):
l.append('END')
return l
print(add_end([1,2,3]))
print(add_end([]))
print(add_end([]))
print(add_end([]))
def add(a,b):
return a+b
add(1,3)
# lambda map filter reduce ;序列l(wèi)ist、string
f = lambda x,y:x+y+10 # 只支持簡單的,不支持復(fù)雜的
def func(x):
return x+1
print(f(3,7))
3、 list(range(10))列表,實現(xiàn)每個元素+2,輸出列表,map+lambda實現(xiàn)
ps:至少寫30多個lambda的題目,才能熟練
(1)map
def sub(a):
return a - 1
res = map(sub,[1,2,3,4])
print(res)
print(type(res))
print(list(res))
(2)map+lambda+range
f1 = map(lambda x:x+2,list(range(10)))
print(f1)
print(list(map(lambda x:x+2, range(10))))
# [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
(3)list+map+函數(shù)
def sub1(a):
return a+2
print(list(map(sub1,[1,2,3,4,5,6,7,8,9])))
(3)list+map+內(nèi)置函數(shù)
# 實現(xiàn)大寫轉(zhuǎn)小寫,map
import string
# print(dir(string))
print(list(map(lambda x : chr(ord(x)+32), string.ascii_uppercase)))
print(list(map(lambda x : x.upper(), string.ascii_uppercase)))
a = "".join(list(map(lambda x : chr(ord(x)+32),string.ascii_uppercase)))
print(a)
#輸出結(jié)果:
[3, 4, 5, 6, 7, 8, 9, 10, 11]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
abcdefghijklmnopqrstuvwxyz