挑战每日打卡python基础题
come with me !今日练习:函数的五种参数、可变与不可变类型、map函数
1、函数使用位置参数、关键字参数、默认参数、元组参数、字典参数(key),传入单词,计算长度
#函数使用位置参数、关键字参数、默认参数、元组参数、字典参数(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"))
#结果:10
23
2、不可变:数字、字符串、元组、
可变:列表、字典、集合
#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 ;序列list、string
f = lambda x,y:x+y+10 # 只支持简单的,不支持复杂的
def func(x):
return x+1
print(f(3,7))
3、 list(range(10))列表,实现每个元素+2,输出列表,map+lambda实现
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+函数
def sub1(a):
return a+2
print(list(map(sub1,[1,2,3,4,5,6,7,8,9])))
(3)list+map+内置函数
# 实现大写转小写,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)
#输出结果:
[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