8人Python-----day06

8人Python-----day06,第1张

概述#无参无返回defhello1():print("hello")return#无参有返回defhello2():return1000#返回值结束函数print(hello1())print(hello2())#有参有返回defhello3(a,b):returna+bprint(hello3(10,300))#有参无返回defhello4(a,b):
# 无参无返回def hello1():    print("hello")    return# 无参有返回def hello2():    return 1000 # 返回值  结束函数print(hello1())print(hello2())# 有参有返回def hello3(a, b):    return a + bprint(hello3(10, 300))# 有参无返回def hello4(a, b):    print(a + b)    returnprint(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 = 999def test1():    a = 100    print(a + b)    print(a)test1()

重名的时候

b = 999c = 666def test1():    a = 100    print(a + b)    print(a)    c = 111    print(c)test1()# for i in range(1, 10):#     print(i)

global 关键字

b = 999c = 666def 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=valueuser_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)            returndef 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) + numprint(sum_number(3))print(sum_number(100))# 1~ n阶乘   1* 2 * 3 * ...n# 阶乘 10*9*8*7...1def 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全部内容,希望文章能够帮你解决8人Python-----day06所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1183561.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存