# 函数的作用 # 1.增强代码的可读性 # 2.降低代码的重复性 # def 函数的名字(): # 函数体,也叫代码块 # len() # def len_func(): # 函数的定义 # print('你好啊') # # len_func() # 函数的调用 # lst = [1, 2, 3, 4] # # # def len_func(): # 函数的定义 # count = 0 # for i in lst: # count = count + 1 # print(count) # # # len_func() # 函数的调用 # len_func() # 练习 # a = 1 ,b =2 c = 3 , 写函数,有3个值a,b,c 基数 a*b/c的结果并打印 # a = 1 # b = 2 # c = 3 # # def func(): # # print(a * b / c) # # func() # 返回值 # def len_func(): # 函数的定义 # lst = [1, 2, 3, 4] # count = 0 # for i in lst: # count = count + 1 # return count # # ret=len_func() # 函数的调用 # print(ret) # a = 1 # b = 2 # c = 3 # # # def func(): # # print(a * b / c) # return a * b / c # # # ret = func() # print(ret +100) # 思考 # def demo(): # print(123) # return True # print(456) # return False # # # ret = demo() # print(ret) # def demo2(): # a = 1 # b = 2 # c = a + b # return c # # # ret = demo2() # # d = 10 # d = d + ret # print(d) # print(ret+10) # print(ret,type(ret)) # def demo3(): # 只写return # return # # # ret = demo3() # print(ret) # def demo4(): # for i in range(10): # print(i) # if i == 3: # return # 当i=3时候我就结束这个程序 # # # demo4() # 总结一下:return # 不写return # 所有代码执行完毕自动结束函数 # 返回值是None # 只写return # 遇到return,函数结束 # 返回值是None # return 结果 # 程序结束,返回结果给调用者 # return 结果1,结果2,结果3 # 会变成元组返回给调用者 # 总结函数: # 定义:def 关键字开头,空格之后接函数名称和圆括号(),最后还有一个:号 # def 是固定的,不能变,必须是连续的def三个字母,不能分开 # # 函数名:函数名只能包含字母,数字,下划线,不能以数字开头,虽然函数名可有随便起, # 一般函数名尽量简单,能表达函数的功能,一般是小写字母开头 # # 调用:就是函数名(),要记得加上括号
参数
# 假设我现在有一个字符1,和字符2,列表1 我要计算这个3个长度分别是多少 # 参数 # # def login(): # # 登录 # username = input('>>') # pwd = input('>>>') # # # login() # # # def reg(): # """ # 注册 # :return: # """ # username1 = input('>>') # pwd2 = input('>>>') # # # reg() # # # def func1(a, b): # print(a + b) # print(a - b) # return # a = 666 # print(a) # # # func1(1, 2) # def len_func(s1): # 形参 # # count = 0 # for i in s1: # count = count + 1 # return count # # # str1 = input('>>>请输入字符1') # str2 = input('>>>请输入字符2') # ret = len_func(str1) # 实参 # ret2 = len_func([1,2,3,4,5,6]) # print(ret) # print(ret2) # 练习1,求和 ,把这个函数补充完成,打印返回结果 # def sum_func(a, b, c): # return a + b + c # # # ret = sum_func(1, 2, 3) # # print(ret) # # 练习2 # # 写一个函数,传入两个参数,比较两个参数值得大小,并返回比较大哪一个数值 # # def func(a,b): # if a >b: # return a # else: # return b # # ret2=func(1111,2222) # print(ret2) # 以下两个哪个比较好 def len_func(s): n = 0 for i in s: n = n + 1 print(n) len_func('helloworld') def len_func(s): n = 0 for i in s: n = n + 1 return n ret1=len_func('helloworld') ret2=len_func('hello') print(ret1>ret2) # 写函数要注意得: # 1.尽量用return,而不是print来处理结果 # 2.在函数内部,我们每一个函数中得代码都要注意不要出现重复代码,保证每一个函数是最精简得功能
#站在实参角度 # 1.按照位置传参 # def func(a,b): # print(a) # print(b) # func(2,3) # 2.按照关键字传参,永远是先按照位置传,再按照关键字传 # def func(a,b,c): # print(a) # print(b) # print(c) # func(1,b=2-100,c=3*7) # 动态传参: *args 动态传参打包成元组 # def func(*args): # # print(args) # count=0 # for i in args: # count+=i # return count # a=func(2,42,436,6457,3123) # b=func(243225,6457,3123) # print(a,b) # 组合使用 # def demo(a,b,c,*args): # #正常人不会这么写,Python3.x之后更新特性 # print(a) # print(b) # print(c) # print(args) # demo(1,2,3,423,55,312,312,231) # *args动态参数放在位置参数之后,在默认参数之前 # 先位置参数,再args,默认参数,**kwargs # 动态参数二 **kwargs # def func(**kwargs): func(*lst) #相当于把lst的值一个一个拆出去传过去了 ,列表和元组都可以 dic={'k1':2,'k2':3} 分开取,单独去只能取key,除非指定values func(*dic.keys()) func(*dic.values())
站在形参角度
# def student(name, gender, python_score): # print('南京2班得同学%s,性别%s,以及python得成绩%s' % (name, gender, python_score)) # # # student('永晨', '真男人', 99) # student('有才', '真男人', 99) # student('学霸', '不详', 100) # def student(name, python_score, gender='真男人'): # print('南京2班得同学%s,性别%s,以及python得成绩%s' % (name, gender, python_score)) # # # student('永晨', 99) # student('有才', 99) # student('学霸', 100, '女') # 动态传参 # def sum_func(*args): # *变量名 ,起啥名字都可以,但是我们约定俗成*args # print(args, type(args)) # 用得时候不能带* # count = 0 # for i in args: # count = count + i # return count # # # ret1 = sum_func(1, 2, 100, 1000, 10000, 66666, 4444444, 2222) # ret2 = sum_func(1, 2, 100, 1000, 10000) # print(ret1) # print(ret2) # 组合使用 # def demo(a, b, c, *args): # print(a) # print(b) # print(c) # print(args) # # # demo(1, 2, 3, 4, 5, 6, 7, 8) # def demo(*args,a,b,c): # print(a) # print(b) # print(c) # print(args) # # # demo(1, 2, 3, 4, 5, 6, 7, 8) # 报错, a,b,c没有接收到值 # def demo(*args,a,b,c): # # 正常人不会这么写, python3.x之后又新特性 # print(a) # print(b) # print(c) # print(args) # # # demo(1, 2, 3, 4, 5, a=6, b=7, c=8) # def demo(a,b,c='helloworld',*args): # 这样写默认参数毫无意义 # # print(a) # print(b) # print(c) # print(args) # # # demo(1, 2, 3, 4, 5,6,7,9) # def demo(a,b,*args,c='helloworld'): # # # print(a) # print(b) # print(c) # print(args) # # # demo(1, 2, 3, 4, 5,6,7,9) # 这里* args就不好使了 # def demo(a,b,*args,c='wind'): # # # print(a) # print(b) # print(c) # print(args) # # # demo(1, 2, 3, 4, 5,6,7,9,a='班长') # 报错,班长这个值没有人接收 # 动态参数二 **kwargs # def func(**kwargs): # print(kwargs) # # func(1,2,3) # 不好使了,按位置传参 # def func(**kwargs): # print(kwargs) # {'a': 1, 'b': 2, 'c': 3} # # func(a=1,b=2,c=3) # 按关键字传参, # def demo(a,b,*args,c='wind',**kwargs): # # # print(a) # print(b) # print(c) # print(args) # print(kwargs) # # # # demo(1, 2, 3, 4, 5,6,7,9,d='班长') # 报错,班长这个值没有人接收 # print(demo(1, 2, 3, 4, 5,6,7,9,d='班长')) # 额外示例 def func(a,b,c): print(a,b,c) # func(1,2,3) # lst = [1,2,3] # # func(*lst) # 相当于把lst得值一个一个拆出去传过去了 # tup = (1,2,3) # func(*tup) dic = {'k1':1,"k2":2,"k3":3} # # func(*dic) func(*dic.values()) # *args动态参数放在位置参数之后,在默认参数之前 # 先位置参数,在*args,默认参数,**kwargs
# 1.写函数,接收两个数字参数,返回比较大的数字 # 2.写函数,获取传入列表的所有奇数位索引,对应的元素,并将其作为新列表返回。 # 例如:传入:[34,23,52,352,352,3523,5],返回:[23,352,3523] # def func(lst): # new_lst = [] # for i in range(len(lst)): # if i % 2 != 0: # new_lst.append(lst[i]) # return new_lst # # # ret = func([34, 23, 52, 352, 352, 3523, 5]) # print(ret) # 3.写函数,判断用户传入的对象(列表)长度是否大于5,如果大于5,那么仅保留前五个长度的内容并返回。不大于5返回本身。 # 例如: # 传入1:[34,23,52,352,666,3523,5] 返回1:[34,23,52,352,666] # 传入2:[34,23,52] 返回2:[34,23,52] # def len_func(lst): # if len(lst) > 5: # return lst[0:5] # else: # return lst # # # ret = len_func([34, 23, 52, 352, 666, 3523, 5]) # print(ret) # 4.写函数,检查传入的字符串是否含有空字符串,返回结果,包含空字符串返回True,不包含返回False # 例如: # 传入:"hello world" # 返回:True # def func(str1): # for i in str1: # if i.isspace(): # return True # return False # # ret = func("hello world") # print(ret) # 5.定义一个函数,实现两个数四则运算,要注意有3个参数,分别是运算符和两个运算的数字. # 例如:传入:10,*,20返回:200 # def func(num1, symbol, num2): # if symbol == '+': # s = num1 + num2 # elif symbol == '-': # s = num1 - num2 # elif symbol == '*': # s = num1 * num2 # elif symbol == '/': # s = num1 / num2 # return s # # # ret = func(10, '/', 20) # print(ret) # 6.写函数,计算传入n个数字参数的和。(动态传参) # def func(*args): # count = 0 # for i in args: # count += i # # return count # # ret=func(1,2,3,4,5,6,9) # print(ret) # 7.写函数,统计字符串中有几个字母,几个数字,几个其他字符,并返回结果 # def func(n): # num =0 # zimu = 0 # other =0 # for i in n: # if i.isdigit(): # num += 1 # elif i.isalpha(): # zimu += 1 # else: # other += 1 # return '数字%d,字母%d,其他字符%d'%(num,zimu,other) # ret = func('123abcdef!@#$') # print(ret) # 8.写一个函数,判断用户传入的对象(字符串、列表、元组)的元素是否为空 # 布尔值 False '' [] {} () # False '' [] {} () # def func(*s1): # print(s1) # for i in s1: # if i: # return True # else: # return False # # ret=func('',[],(),{}) # print(ret) # 9.双色球改成 函数版一个while循环,可以调用2次来实现 # 6.双色球选购游戏 # 基础需求: # 先让用户依次选择6个红球(红球的选择范围是1-32),再选择2个蓝球(篮球的选择范围是1-16),最后统一打印用户选择的球号。 # 确保用户不能选择重复的,选择的数不能超出范围 def choose_ball(count,max_num,ball_color): n = 0 s = '' while n < count: num1 = input('请输入%s球的号码:'%ball_color) num = int(num1) if num <= max_num and num >= 1: if num1 in s: print('%s输入重复,请重新输入'%ball_color) else: print(f'您选择的%s{num1}'%ball_color) n += 1 if n < count: s = s + num1 + ',' else: s = s + num1 else: print('请选择1-%s之间数字'%max_num) return s s1=choose_ball(6,32,'红色') s2=choose_ball(2,16,'蓝色') print('您选择的红球有:%s,您选择的蓝球有:%s' %(s1,s2))