美文网首页
Python函数

Python函数

作者: 阿猫阿狗py | 来源:发表于2018-07-21 21:21 被阅读16次

    基本语法:[定义/声明函数、调用函数]
    define 定义
    def 函数名称():
    函数中要执行的代码

    函数的参数:
    参数:就是一种变量
    是一种特殊的变量:专门给函数接收数据使用的
    生活场景:函数->行为
    参数->行为需要的资源

    def show_info(name):#name变量:名义上函数执行需要一个数据
        '''
    
        :param name:
        :return:
        '''
        print("用户姓名:%s"%name)
    
    #调用执行了函数:但是没有提供数据
    show_info("李易峰")#调用执行函数,传递了一个具体的实际的数据:实参
    
    #多个参数的操作
    def show_msg(name,msg):
        #留言的函数
        print("%s给您留言%s"%(name,msg))
    
    show_msg("12","你好")
    

    函数的返回值

    '''
    函数的返回值
    '''
    
    def check_phone():
        #
        print("检查过程·····")
        name = "小红"
        return name
    check_phone()
    c = check_phone()
    print(c)
    

    交叉赋值

    def rest():
        print("休息")
        a = "十分钟"
        b = "喝水"
        return a,b
    x,y = rest()
    print("内容:%s,%s"%(x,y))
    

    全局变量

    #声明了一个全局变量
    jobs = "爬虫开发工程师"
    
    def eat():
        food = "大烩菜"
        print("局部变量",food)
        print("全局变量",jobs)
    eat()
    

    global关键字

    语法:global 变量名
    globall的意义:
    1.global 变量名称:表示在函数中要使用指定名称的全局变量
    2.global 变量名称:表示在函数声明一个全局变量

    name = "jerry"
    print("函数外部查询变量:",name)
    
    def test():
        global name #声明函数中要使用全局变量 name
        global msg #在函数中声明定义了一个全局变量 msg
        print("函数内部查询变量:",name) #jerry
        print("函数内部操作变量")
        age = 18
        print("函数中的局部变量:",age)
        name = "hello"
        msg = "hello 函数中的全局变量"
    
    test()
    print("全局变量:",name)
    print("在函数内部定义的全局变量:",msg)
    

    参数操作

    1.位置参数

    def test1(p1,p2,p3):
        print(p1,p2,p3)
    
    test1(10,20,30)
    

    2.默认值参数

    如果函数参数中出现了默认值参数~一般默认值参数放在普通参数的后面

    def test2(n1,n2,opra="+"):
        print("计算n1[%s]和n2[%s]的[%s]运算"%(n1,n2,opra))
    
    test2(10,20)
    test2(10,20,"-")
    

    3实参关键字参数

    主要目的是为了提高代码的可读性
    实参关键字参数,调用时可以根据参数名称进行赋值[打破位置参数要求的数顺序]

    def test3(p1,p2,p3):
        print(p1,p2,p3)
    test3(10,20,30)
    test3(p2=10,p1=20,p3=30)
    

    4.实参强制关键字参数

    参传递数据时,形式参数中符号*后面的所有参数~都要是同k=v的关键字参数方式传递数据

    def test4(*,p1,p2,p3):
        print(p1,p2,p3)
    

    函数参数的四种形式

    1.不需要提供数据,不需要返回结果

    def test():
        print("你好!")
    

    2.不需要提供数据,需要返回结果

    def test1():
        a = 1
        b = 2
        return a+b
    c = test1()
    print("a+b=",c)
    输出:a+b= 3
    

    3.需要提供数据,不需要返回结果

    def test2(name):
        print("名字是:",name)
    test2("jerry")
    输出:名字是: jerry
    

    4.需要提供结果,需要返回数据

    def test2(a,b):
        c = a+b
        return c
    sum = test2(1,2)
    print("和为:",sum)
    输出:和为: 3
    

    参数的进阶操作

    函数操作进阶
    1.可变参数
    2.关键字参数
    3.函数类型描述符

    1.可变参数

    函数在执行过程中,可以接受0~n个参数

    ef test(*args):#arguments参数
        print("可变参数,在普通参数前面添加i一个符号*")
        print("test()函数就可以接受0~n个参数进行操作")
        print(args)
        for x in args:
            print(x)
        print("******************")
    test()
    test("hello")
    test("hello","jerry")
    test(1,3,5)
    test(1,1,2,3,5,8,13,21)
    

    2.可变关键字参数

    可变关键字参数,就是在普通参数的前面添加符号**
    变关键字参数,可以接受多个k=v的参数数据
    并且可以接受0~n个k=v的数据
    并且将数据包含在字典中

    def test2(**kwargs):
        print(kwargs,kwargs.get("name"))
    test2(name='tom')
    test2(name='shuke',age=62)
    test2(name='jerry',age=62,gender="男")
    输出:
    {'name': 'tom'} tom
    {'name': 'shuke', 'age': 62} shuke
    {'name': 'jerry', 'age': 62, 'gender': '男'} jerry
    

    3. 万能形参

    满足规范的情况下,可以接收任何参数
    在下面的代码中
    test3(name = "jerry","hello") #这条语句是错误的不满足规范

    ef test3(*args,**kwargs):
        print(args,kwargs)
    test3()
    test3("hello")
    test3("hello",name="jerry")
    test3("hello",name="jerry",age = 1)
    test3(name="jerry",age=1)
    #test3(name = "jerry","hello") #是错误的不满足规范
    输出:
    () {}
    ('hello',) {}
    ('hello',) {'name': 'jerry'}
    ('hello',) {'name': 'jerry', 'age': 1}
    () {'name': 'jerry', 'age': 1}s
    

    解包操作

    解包:将组合数据类型拆分

        print(p1,p2)
    
    #正常调用
    test(10,20)
    
    #非正常调用:列表解包
    nums = [10,20]
    test(*nums)
    
    #字典解包
    nums = {"p1":1,"p2":2}
    test(**nums)
    输出:
    10 20
    10 20
    1 2
    

    相关文章

      网友评论

          本文标题:Python函数

          本文链接:https://www.haomeiwen.com/subject/atqnpftx.html