• Python函数和模块


    函数与模块

    函数就是对代码的封装,其优点非常的多,如代码重复利用,保持可扩展性,保持代码一致性,定义函数也是每个编程者必修课。

    函数定义:

     def test(): 
         """函数的注释""" 
         print() 
         return 0 
    def:定义函数的关键字 
    test :函数名,随便起 
    ()储存形参,如果定义参数(x,y) 
    """函数的注释""" 表示对函数的说明 
    print()逻辑代码 
    return 返回关键字 当执行到return的时候,表示函数运行结束,并且返回,非必须 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    如何运行函数:

    函数名() #如果定义了形式参数,需要传入实际参数 如:test()
    函数参数及调用

    1.位置参数,传入参数根据位置,参数个数
    def test(x,y): 
        print(x) 
        print(y) 
    test(1,2) # 不能多传 
    
    • 1
    • 2
    • 3
    • 4
    2.默认值:如果不传,则使用默认值,传入则更新
    def test(x,y=2): 
        print(x) 
        print(y) 
    test(1) 
    
    • 1
    • 2
    • 3
    • 4
    3.关键字参数:跟位置无关
    //位置参数要在关键字参数的前面 
    //1个变量不可以被赋值两次 
    def test(x,y): 
        print(x) 
        print(y) 
    test(x=1,y=2) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    4.非固定位置参数=> *args
    //把多传入的位置参数,存放在一个元组中 
    def test(x,y,*args): 
        print(x) 
        print(y) 
        print(args) 
    test(1,2) 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    5.非固定关键字参数 **kwargs =>keyword arguments
    多余的关键字保存在一个字典中 
    def test(x,y,**kwargs): 
        print(x) 
        print(y) 
        print(kwargs) 
    test(1,2,name="xiaoming",age=35,hobby="aiyouyong") 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    6.非固定位置参数和非关键字参数配合使用
    def test(x,y,*args,**kwargs): 
    
    • 1

    模块

    写的每一个py的文件都是一个独立的模块,模块与模块之间是可以相互调用方法和函数的。

    引入自定义模块的方法:

    第一种:
    from 包名 import 模块名 
    from Day03 import func 
    第二种:
    from 包名.模块名 import 函数名 
    from Day03.func import atm_money 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    区别: 第一种从包引入到具体的某个模块,可以通过模块.函数名的形式使用该模块下任何的函数 第二种从包引入到具体的模块后,再引入这个模块下具体的函数,在其他模块中只能使用引入的这个函数

    练习

    实现一个简单的函数版学生管理系统,实现新增学生、查看学生、删除学生、编辑学生的功能

    students = [] # [{“Id”:1,"Name":"张三"}]
    
    print("""
    欢迎登录学生管理系统:
    1. 新增学生;
    2. 查看学生;
    3. 修改学生;
    4. 删除学生;
    5. 退出
    """)
    
    def addStudent():
        Id = input("请输入学号:")
        for student in students:
            if student["Id"] == Id:
                # 学号存在,报错
                print("学号已存在")
                return
        Name = input("请输入学生的名字:")
        Age = input("请输入学生的年龄:")
        students.append({"Id": Id, "Name": Name, "Age": Age})
        print(students)
    
    def search_student():
        Id = input("请输入学生学号:")
        for student in students:
            if student["Id"] == Id:
                print(student)
                return  # 查询到了之后退出
        print("您输入的学号不存在")
    
    def updateStudent():
        # 由用户输入要修改的学生学号
        Id = input("请输入要修改的学生学号:")
        # 去列表查找学号是否存在
        for student in students:
            if student["Id"] == Id:
                # 存在的话我们继续提示用户输入要修改的值
                Name = input("请输入修改后的学生姓名:")
                Age = input("请输入修改后的年龄:")
                student["Name"] = Name
                student["Age"] = Age
                return
        # 不存在就退出
        print("您输入的学生学号不存在")
    
    def deleteStudent():
        Id = input("请输入要删除的学生学号:")
        # 查找学号是否存在
        for student in students:
            if Id == student["Id"]:
                # 存在就删除
                students.remove(student)
                print("删除成功")
                return
        # 不存在就提示找不到该学号
        print("您输入的学号不存在")
    
    while True:
        n = input("请输入指令:")
        if n == "1":
            addStudent()
        elif n == "2":
            search_student()
        elif n == "3":
            updateStudent()
        elif n == "4":
            deleteStudent()
        elif n == "5":
            break
        else:
            print("您输入的指令不存在,请重新输入")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
  • 相关阅读:
    SpringMVC14-SpringMVC执行流程
    知识图谱1(实体抽取)
    Synopsys ICC学习(1)
    基于html+css的图展示94
    ts泛型,映射,条件类型和类型提取infer和一些常用工具库的说明
    RIP协议;OSPF协议;BGP协议
    Git版本控制系统
    request 请求类封装
    js单行代码-----dom
    当商品跨多个分类时,如何精确统计总销售额?
  • 原文地址:https://blog.csdn.net/a1137588003/article/details/132789288