8人Python-----day06
2021/6/29 20:50:37
本文主要是介绍8人Python-----day06,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# 无参无返回 def hello1(): print("hello") return # 无参有返回 def hello2(): return 1000 # 返回值 结束函数 print(hello1()) print(hello2()) # 有参有返回 def hello3(a, b): return a + b print(hello3(10, 300)) # 有参无返回 def hello4(a, b): print(a + b) return print(hello4(12, 23)) print(hello3(3, 4) * 10) print(hello4(3, 4) * 10)
变量作用域
变量作用域指的是变量生效的范围,主要分为两类:局部变量和全局变量。
- 局部变量
所谓局部变量是定义在函数体内部的变量,即只在函数体内部生效。
def test1(): a = 100 print(a) for i in range(1, 10): print(i)
局部变量的作用:在函数体内部,临时保存数据,即当函数调用完成后,则销毁局部变量。
- 全局变量
所谓全局变量,指的是在函数体内、外都能生效的变量
b = 999 def test1(): a = 100 print(a + b) print(a) test1()
重名的时候
b = 999 c = 666 def test1(): a = 100 print(a + b) print(a) c = 111 print(c) test1() # for i in range(1, 10): # print(i)
global 关键字
b = 999 c = 666 def test1(): a = 100 print(a + b) print(a) # global声明c 是全局变量 global c c = 111 print(c)
函数的参数
位置参数(必须参数):调用函数时根据函数定义的参数位置来传递参数。
# 默认参数 def user_info(name, age=18, gender="女"): print('您的名字是{}, 年龄是{}, 性别是{}'.format(name, age, gender)) # # user_info('TOM', 20, '男') # user_info(12, "小李子", "女") user_info("小桂子") # 关键字参数 key=value user_info("小桂子", age=20) user_info("小桂子", age=20, gender="男") user_info("小桂子", gender="男", age=222, ) # 原则 : 让别人看的懂
学员管理系统
显示
def print_info(): print('-' * 20) print('欢迎登录学员管理系统') print('1: 添加学员') print('2: 删除学员') print('3: 修改学员信息') print('4: 查询学员信息') print('5: 显示所有学员信息') print('6: 退出系统') print('-' * 20) info = [] # [{}, {}, ] def add_info(): new_id = input("请输入学号") new_name = input("请输入姓名") new_tel = input("请输入手机号") global info # 检测学生的姓名是否存在 for d in info: if new_name == d['name']: print("该用户已经存在") return # 不存在, 添加学员 info_dict = {} info_dict['id'] = new_id info_dict['name'] = new_name info_dict['tel'] = new_tel # 追加到学生列表里面 info.append(info_dict) print(info) def del_info(): del_name = input("请输入删除学员的名字") global info for d in info: if del_name == d['name']: # 删除 info.remove(d) print("删除成功!") return print("该学员不存在") print(info) def modify_info(): """改手机号""" modify_name = input('请输入要修改的学员的姓名:') for d in info: if modify_name == d['name']: d['tel'] = input('请输入新的手机号:') return print("修改成功") print(info) def search_info(): # 姓名查找 search_name = input("请输入查找学员的名字") global info for d in info: if search_name == d['name']: print(d) return def print_all(): for d in info: print(d) while True: print_info() user_num = input('请选择您需要的功能序号:') if user_num == '1': add_info() elif user_num == '2': del_info() elif user_num == '3': modify_info() elif user_num == '4': search_info() elif user_num == '5': print_all() elif user_num == '6': print('退出系统') break
递归
特点
- 函数内部自己调用自己
- 必须有出口(终止条件)
def sum_number(num): if num == 1: return 1 return sum_number(num-1) + num print(sum_number(3)) print(sum_number(100)) # 1~ n阶乘 1* 2 * 3 * ...n # 阶乘 10*9*8*7...1 def cac1(n): if n == 1: return 1 return n *cac1(n-1) print(cac1(10)) # # 求 [1,2,3,4,5] 的和 ls = [1, 2, 3, 4, 5] def arraySum(ls, start, end): if start==end: return ls[end] return ls[start] + arraySum(ls, start+1, end) print(arraySum(ls, 0, 4)) # 使用递归求 s = [5,1,4,6,2] #求最大l
这篇关于8人Python-----day06的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python