尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

Python零基础学习--函数

Python零基础学习--函数 目录一函数基础1、函数定义2、函数参数及返回值3、函数说明文档4、案例​编辑二函数进阶1、变量作用域2、函数参数详解1传参方式2默认参数3不定长参数4函数的参数类型3、匿名函数4、案例一函数基础1、函数定义注函数定义时不会执行内部代码只有调用时才会执行函数必须先声明再使用所以调用语句必须在定义语句之后。2、函数参数及返回值函数可以同时返回多个返回值多个返回值之间逗号分隔。可以用多个变量依次接收返回值如果用一个变量来接收多个返回值会封装到元组之中如果想获得其中第一个返回值可以使用解包操作后面的被封装成列表def func(): return 1, 2, 3 one, *two func() print(one, two) #1 [2,3]3、函数说明文档4、案例def triangle_area(base, height): 根据传入的底和高计算三角形面积 :param base: 底 :param height: 高 :return: 面积 return base * height / 2 def vowel_num(s): 计算传入的字符串中元音字母的个数 :param s: 字符串 :return: 个数 # vowel [a, e, i, o, u, A, E, I, O, U] num 0 for i in s: # if i in vowel: if i in aeiouAEIOU: num 1 return num print(vowel_num(Hello Python World OK)) def grade_statics(score_list): 计算传入的班级学员高考成绩列表中的最高分、最低分、平均分保留1位小数 :param score_list: 成绩列表 :return: 最高分、最低分、平均分 return round(max(score_list), 1), round(min(score_list), 1), round(sum(score_list) / len(score_list), 1) s_list [589, 609, 605, 643, 677, 455, 477] max_score, min_score, avg_score grade_statics(s_list) print(f最高分为{max_score} 最低分为{min_score} 平均分为{avg_score})二函数进阶1、变量作用域global关键字应用如下num1 100 num2 100 def triangle_area(base, height): 根据传入的底和高计算三角形面积 :param base: 底 :param height: 高 :return: 面积 num1 1000 global num2 num2 1000 print(num1, num1) print(num2, num2) return base * height / 2 triangle_area(1, 1) print(num1, num1) print(num2, num2) num1 1000 num2 1000 num1 100 num2 10002、函数参数详解1传参方式2默认参数3不定长参数4函数的参数类型3、匿名函数典型的应用场景# 按照每个元素字符个数从小到大排序 data_list [C, C, Python, Jack, PHP, Java, Go, JavaScript, Rust] print(data_list) data_list.sort(keylambda item: len(item)) print(data_list) data_list.sort(keylambda item: len(item), reverseTrue) print(data_list)4、案例案例一# 案例一根据传入的数字计算该数字阶乘的结果 def jie_cheng(n): if n 0: print(错误) elif n 1 or n 0: return 1 else: return n * jie_cheng(n-1) print(jie_cheng(10))案例二def cal(products, discountNone, freight0.0): if discount is None: discount {优惠券: 0, 积分抵扣: 0} total_cost sum([i[数量]*i[价格] for i in products]) cost total_cost print(f总金额为{total_cost},) if total_cost 5000: if discount[优惠券] total_cost: print(f可以使用优惠券,优惠券额度为{discount[优惠券]}) cost total_cost - discount[优惠券] if discount[积分抵扣] 100: dk min(discount[积分抵扣] // 100, cost) print(f可以使用积分抵扣,积分有{discount[积分抵扣]},可抵扣{dk}元) cost cost - dk else: print(消费金额不足) cost cost freight print(f总金额为{cost}) p [{商品名: 鼠标, 数量: 188, 价格: 2}, {商品名: 键盘, 数量: 388, 价格: 1}, {商品名: 手机, 数量: 3999, 价格: 1}] # 总价 6000 d {优惠券: 10, 积分抵扣: 4000} f 9.9 p [{商品名: 鼠标, 数量: 188, 价格: 2}, {商品名: 键盘, 数量: 388, 价格: 1}, {商品名: 手机, 数量: 6999, 价格: 1}] # 总价 6000 d {优惠券: 10, 积分抵扣: 4000} f 9.9 cal(p, d, f)注Python 在定义函数时不是调用时只创建一次默认字典对象所以为discount指定默认值时如果你在函数内部修改了它下一次调用时这个字典还是同一个数据会被保留def cal(products, discount{优惠券: 0, 积分抵扣:0}, freight0.0): discount[优惠券] 100 print(discount) # 第一次调用 cal([]) # 输出{优惠券: 100, 积分抵扣: 0} # 第二次调用以为会重置但其实没有 cal([]) # 输出{优惠券: 200, 积分抵扣: 0} ← 意外居然累加了要想安全的使用默认字典可以使用None作为默认值。def cal(products, discountNone, freight0.0): # 如果没有传入 discount就新建一个干净的字典 if discount is None: discount {优惠券: 0, 积分抵扣: 0}
返回列表