1.定义一个方法 func,该func可以引入任意多的整型参数,结果返回其中最大与最小的值。
def func(*num):
for i in num:
if not isinstance(i,int):
return("输入的参数必须为整数")
a=sorted(num)
return("最大值%s,最小值%s." % (a[-1],a[0]))
print(func(1,2,3,4))
2.定义一个方法func,该func可以引入任意多的字符串参数,结果返回(长度)最长的字符串。
def func(*num):
for i in num:
if not isinstance(i,str):
return("输入的参数必须为字符串")
a=sorted(num,key=lambda k:len(k))
return(a[-1])
print(func("123","2323","1"))
3.用lambda和filter完成下面功能:输出一个列表,列表里面包括:1-100内的所有偶数。(提示:可以用filter,lambda)
t=list(range(1,101))
a=list(filter(lambda x:x%2==0,t))
print(a)
4.用位置匹配,关键字匹配,收集匹配(元组收集,字典收集)分别写4个函数,完成功能;
传递3个列表参数:
[1,2,3],[1,5,65],[33,445,22]
返回这3个列表中元素最大的那个,结果是:445
def WzPp(a,b,c):
if (not isinstance(a,list)) or (not isinstance(b,list)) or (not isinstance(c,list)):
return "非列表"
a.extend(b)
a.extend(c)
return max(a)
print(WzPp([1,2,3],[1,5,65],[33,445,22]))
def GjzPp(a=[],b=[],c=[]):
if (not isinstance(a,list)) or (not isinstance(b,list)) or (not isinstance(c,list)):
return "非列表"
a.extend(b)
a.extend(c)
return max(a)
print(WzPp(b=[1,2,3],c=[1,5,65],a=[33,445,22]))
def WzPp(*arg):
a=list(arg)
b=[]
for x in a:
b.extend(x)
return max(b)
print(WzPp([1,2,3],[1,5,65],[33,445,22]))
5.定义一个func(name),该函数效果如下。
assert func("lilei") == "Lilei"
assert func("hanmeimei") == "Hanmeimei"
assert func("Hanmeimei") == "Hanmeimei"
def func(name):
return name.capitalize()
assert func("lilei") == "Lilei"
assert func("hanmeimei") == "Hanmeimei"
assert func("Hanmeimei") == "Hanmeimei"
6.定义一个func(name,callback=None),效果如下。
assert func("lilei") == "Lilei"
assert func("LILEI",callback=string.lower) == "lilei"
assert func("lilei",callback=string.upper) == "LILEI"
def func(name,callback=None):
if callback==None:
return name.capitalize()
else:
return callback(name)
assert func("lilei") == "Lilei"
assert func("LILEI",callback=str.lower) == "lilei"
assert func("lilei",callback=str.upper) == "LILEI"
7.定义一个func(*kargs),效果如下。
l = func(1,2,3,4,5)
for i in l:
print i,
#输出 1 2 3 4 5
l = func(5,3,4,5,6)
for i in l:
print i
#输出 5 3 4 5 6
def func(*kargs):
return kargs
l = func(1,2,3,4,5)
for i in l:
print (i,end="") #不换行
8.定义一个func(*kargs),该函数效果如下。
assert func(222,1111,'xixi','hahahah') == "xixi"
assert func(7,'name','dasere') == 'name'
assert func(1,2,3,4) == None
def func(*kargs):
a=list(filter(lambda x:isinstance(x,str),kargs))
if a==[]:
return None
else:
return a[0]
assert func(222,1111,'xixi','hahahah') == "xixi"
assert func(7,'name','dasere') == 'name'
assert func(1,2,3,4) == None
5.定义一个func(name=None,**kargs),该函数效果如下。
assert func("lilei”) == "lilei"
assert func("lilei",years=4) == "lilei,years:4"
assert func("lilei",years=10,body_weight=20) == "lilei,years:10,body_weight:20"
def func(name=None,**kargs):
a=["%s:%s" % (x,y) for x,y in kargs.items()]
a.insert(0,name)
return ",".join(a)
assert func("lilei") == "lilei"
assert func("lilei",years=4) == "lilei,years:4"
assert func("lilei",years=10,body_weight=20) == "lilei,years:10,body_weight:20"
'''
def func(name=None,**kargs):
a=[]
for x,y in kargs.items():
a.extend([",",str(x),":",str(y)])
b="".join(a)
return "%s%s" % (name,b)
assert func("lilei") == "lilei"
assert func("lilei",years=4) == "lilei,years:4"
assert func("lilei",years=10,body_weight=20) == "lilei,years:10,body_weight:20"
'''