对函数不定参数的总结
定义的时候
* args 接受不确定个数的位置参数,并存储为元祖
* kwargs 接受不确定长度的关键字参数,并存储为字典 dict
调用的时候
*args 将事先定义好的元祖或列表,作为多个参数传入
列:
def add(a,b,c): return sum(a,b,c) alis=[1,2,3] add(*alis)
name="nana" def get_name(): name="lili" return name print(name,get_name()) # 打印结果,nana,lili
# 函数中可以使用全局变量,优先会使用函数内部变量,如果没有的情况下才去找全局变量 name="nana" def get_name(): return name print(name,get_name()) # 打印结果,nana,nana # 全局变量不可使用函数中的变量 def get_age(): age=18 return age print(age,get_age()) # 第一个age不可以,因为age没有被定义
name="nana" def get_name(): name = name+"hahah". # ==> 这不OK return name # 因修改如下: name="nana" def get_name(): global name #==>提前声明全局变量 name = name+"hahah". # ==> 这不OK return name print(name,get_name()) # 打印结果,nanahahah,nanahahah
str = '1+6' print (str) # 结果:1+6 print(eval(str)) # 结果:7 str ="{"name":"nana","age":18}" print(type(str)) # string print(type(eval(str))) # dict
lis =["name","age","gender"] lis1=["zhouzhou",18,"男"] print(dict(zip(lis,lis1))) # {"name":"zhouzhou","age":18,"gender":"男"}
来练习一下吧~~
# 这个题在做的时候 首先su1需要提前先定义,如果直接写su1 *=i会报错需要定义。 def multiply_surplus20(*agrs): su1=1 for i in agrs: su1 *=i su1 %=20 return su1 print(multiply_surplus20(3,11,66,322))
def biaozhun (age,gender): return 15<=age<=22 and gender =="女" def zixun(): time =1 list=[] while time <11: name = input("请输入name") age =int(input("请输入age")) gender = input("请输入gender") if biaozhun(age,gender): list.append({"name":name,"age":age,"gender":gender}) time +=1 return list if __name__ == '__main__': print(zixun())
def calculator(a,b): types=int(input("请输入1or2or3or4分别对应加减乘除,输入0退出")) if types ==1: return a+b elif types ==2: return a-b elif types ==3: return a*b elif types ==4: return a/b elif types ==0: return "欢迎下次使用" else: print("输入有误,请重新输入") return calculator(a,b) if __name__ == '__main__': print(calculator(1,2))
#有一组用例数据如下:
cases = [
[‘case_id’, ‘case_title’, ‘url’, ‘data’, ‘excepted’],
[1, ‘用例1’, ‘www.baudi.com’, ‘001’, ‘ok’],
[4, ‘用例4’, ‘www.baudi.com’, ‘002’, ‘ok’],
[2, ‘用例2’, ‘www.baudi.com’, ‘002’, ‘ok’],
[3, ‘用例3’, ‘www.baudi.com’, ‘002’, ‘ok’],
[5, ‘用例5’, ‘www.baudi.com’, ‘002’, ‘ok’],
]
#要求一:把上述数据转换为以下格式
res1 = [
{‘case_id’: 1, ‘case_title’: ‘用例1’, ‘url’: ‘www.baudi.com’, ‘data’: ‘001’, ‘excepted’: ‘ok’},
{‘case_id’: 4, ‘case_title’: ‘用例4’, ‘url’: ‘www.baudi.com’, ‘data’: ‘002’, ‘excepted’: ‘ok’},
{‘case_id’: 2, ‘case_title’: ‘用例2’, ‘url’: ‘www.baudi.com’, ‘data’: ‘002’, ‘excepted’: ‘ok’},
{‘case_id’: 3, ‘case_title’: ‘用例3’, ‘url’: ‘www.baudi.com’, ‘data’: ‘002’, ‘excepted’: ‘ok’},
{‘case_id’: 5, ‘case_title’: ‘用例5’, ‘url’: ‘www.baudi.com’, ‘data’: ‘002’, ‘excepted’: ‘ok’}
]
lis=[] for i in range(1,len(cases)): lis.append(dict(zip(cases[0],cases[i]))) print(lis)
#要求二:把上面转换好的数据中case_id大于3的用例数据获取出来,得到如下结果
res = [
{‘case_id’: 4, ‘case_title’: ‘用例4’, ‘url’: ‘www.baudi.com’, ‘data’: ‘002’, ‘excepted’: ‘ok’},
{‘case_id’: 5, ‘case_title’: ‘用例5’, ‘url’: ‘www.baudi.com’, ‘data’: ‘002’, ‘excepted’: ‘ok’}
]
lis=[] new_lis=[] for i in range(1,len(cases)): lis.append(dict(zip(cases[0],cases[i]))) if lis[i-1]["case_id"]>3: new_lis.append(lis[i-1]) print(lis)