# 2,写函数,接收n个数字,求这些参数数字的和。(动态传参) # def sum(*num): # s=0 # for i in num: # s+=i # return s #3,读代码,回答:代码中,打印出来的值a,b,c分别是什么?为什么? # a=10 # b=20 # def test5(a,b): # print(a,b) # c = test5(b,a) # print(c) #a=20,b=10,c=None #4,读代码,回答:代码中,打印出来的值a,b,c分别是什么?为什么? # a=10 # b=20 # def test5(a,b): # a=3 # b=5 # print(a,b) # c=test5(a,b) #a=3,b=5,c=None # print(c) #5,写函数,传入函数中多个实参(均为可迭代对象如字符串,列表,元祖,集合等),将每个实参的每个元素依次添加到函数的动态参数args里面. # 例如 传入函数两个参数[1,2,3] (22,33)最终args为(1,2,3,22,33) # def fn(*args): # print(args) # fn(*[1,2,3],*(22,33)) #6,写函数,传入函数中多个实参(实参均为字典),将每个实参的键值对依次添加到函数的动态参数kwargs里面. # 例如 传入函数两个参数{‘name’:’alex’} {‘age’:1000}最终kwargs为{‘name’:’alex’ ,‘age’:1000} # def fn(**kwargs): # print(kwargs) # fn(**{"name":"alex"} ,**{"age":1000}) #7, 下面代码成立么?如果不成立为什么报错?怎么解决? # 7.1 # a = 2 # def wrapper(): # print(a) # wrapper() #成立,输出a=2 # # # 7.2 # a = 2 # def wrapper(): # global a # a += 1 # print(a) # wrapper() #不成立,全局作用域变量与局域作用域变量命名重复,执行错误 # # 7.3 # def wrapper(): # a = 1 # def inner(): # print(a) # inner() # wrapper() #成立,输出a=1 # 7.4 # def wrapper(): # a = 1 # def inner(): # nonlocal a # a += 1 # print(a) # inner() # wrapper() #不成立,当前的局域变量中与外一层的局部变量a无法读取 #8,写函数,接收两个数字参数,将较小的数字返回. # def min(a,b): # c=a if a<b else b # return c #9,写函数,接收一个参数(此参数类型必须是可迭代对象),将可迭代对象的每个元素以’_’相连接,形成新的字符串,并返回. # 例如 传入的可迭代对象为[1,'老男孩','武sir']返回的结果为’1_老男孩_武sir’ # def func(*args): # s="" # for i in args: # s=s+"_"+str(i) # s1=s[1:] # print(s1) # func(*[1,'老男孩','武sir']) #10,写函数,传入n个数,返回字典{‘max’:最大值,’min’:最小值} # 例如:min_max(2,5,7,8,4) 返回:{‘max’:8,’min’:2}(此题用到max(),min()内置函数) # def fn(**kwargs): # dic={} # dic["min"]=min(args) # dic["max"]=max(args) # return dic # print(fn(2,5,7,8,4)) # def fn(*args): # m=args[0] # mi=args[0] # for i in args: # if i>m: # m=i # if i<mi: # mi=i # return {'max':m,'min':mi} # print(fn(2,3,4,34,5,6,7,8)) #11,写函数,传入一个参数n,返回n的阶乘 # 例如:cal(7) 计算7*6*5*4*3*2*1 # def fn(num): # cal=1 # while num>0: # cal=cal*num # num-=1 # print(cal) # fn(3) #12写函数,返回一个扑克牌列表,里面有52项,每一项是一个元组 # 例如:[(‘红心’,2),(‘草花’,2), …(‘黑桃’,‘A’)] # ll=["红心","方块","梅花","黑桃"] # def fn(*args): # lst=[] # for a in args: # for b in ll: # if a==1: # lst.append((b,"A")) # elif a==11: # lst.append((b, "J")) # elif a==12: # lst.append((b,"Q")) # elif a==13: # lst.append((b,"K")) # else: # lst.append((b,a)) # print(lst) # fn(1,2,3,4,5,6,7,8,9,10,11,12,13) # def func(): # a = ['红心', '草花', '方片', '黑桃'] # b = [2, 3, 4, 5, 6, 7, 8, 9, 10] # c = ['A', 'J', 'Q', 'K'] # li = [] # for i in b: # for p in a: # tp = (p, i) # li.append(tp) # for m in c: # for n in a: # tp1 = (n, m) # li.append(tp1) # print(li) # print(len(li)) # func() #13 有如下函数: # def wrapper(): # def inner(): # print(666) # wrapper() # 你可以任意添加代码,用两种或以上的方法,执行inner函数. # def wrapper(): # def inner(): # print(666) # inner() # wrapper() # def wrapper(): # def inner(): # # print(666) # ret=inner() # print(ret) # wrapper() #14. #1. # 有函数定义如下: # def calc(a,b,c,d=1,e=2): # return (a+b)*(c-d)+e # 请分别写出下列标号代码的输出结果,如果出错请写出Error。 # print(calc(1,2,3,4,5))__2___ # print(calc(1,2))___Error_ # print(calc(e=4,c=5,a=2,b=3))___ -7 # print(calc(1,2,3))_____8 # print(calc(1,2,3,e=4))____10 # print(calc(1,2,3,d=5,4))__Error___ #2(此题有坑)下面代码打印的结果分别是__list1=["a",10]__list2=[123] list3=["a",10]_____,________,________. # def extendList(val,list=[]): #默认值参数如果是个可变的数,则会随着运行的结果发生变化 # list.append(val)#如果默认值参数是一个可变的数据类型, 如果有人调用的时候改变了他. 其他位置看到的也跟着改变了 # return list # list1 = extendList(10) #list1=[10] # list2 = extendList(123,[]) #list=[123] # # # # # list3 = extendList('a') # # print('list1=%s' % list1) # print('list2=%s' % list2) # print('list3=%s' % list3) # 3, 写代码完成99乘法表.(升级题) # 1 * 1 = 1 # 2 * 1 = 2 2 * 2 = 4 # 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 # ...... # 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 # 第一种方法 # print( ' '.join([' '.join(['%s*%s=%-2s' % (y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)])) # 第二种方法 # for m in range(1,10): # for n in range(1,m+1): # print('%s×%s=%s'%(m,n,m*n),end=' ') # print() # 第三种方法 # a = 1 # while a <=9: # # b = 1 # while b <= a: # print("%dx%d=%d " % (a, b, a*b), end="") # b = b + 1 # print() # 换行 # a = a + 1 # a=input("请输入一个数") # b=0 # for i in a: # if i.isdigit(): # b=b+1 # print(b) # # s=123 # for c in s: # print(c) 数字不可迭代 # user_list=[ # {"name": "alex", "hobby": "抽烟"}, # {"name": "alex", "hobby": "喝酒"}, # {"name": "alex", "hobby": "烫头"}, # {"name": "wusir", "hobby": "喊麦"}, # {"name": "wusir", "hobby": "街舞"}, # {"name": "alex", "hobby": "泡吧"}, # {"name":"太白", "hobby":"开车"} # ] # lst=[] # for user in user_list: # for i in lst: # if user['name']==i['name']: # i['hobby_list'].append(user['hobby']) # break # else: # dic={} # dic['name']=user['name'] # dic['hobby_list']=[user['hobby']] # lst.append(dic) # print(lst)