zoukankan      html  css  js  c++  java
  • Python自动化开发函数01

        Python自动化开发-函数01

    1. 理清函数概念

    例1:带参数需要传参的函数

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test01(x):
        '''
        需要传参数的函数,功能是y=2*x+13。
        test01:自定义的函数名字
        '''
        y=2*x+13
        return y
    z=test01(4)
    print(z)
    View Code

    代码运行结果:

    21

    例2:不带参数不需要传参的函数

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test01():
        '''
        不需要传参数的函数,功能是y=2*x+13。
        test01:自定义的函数名字
        '''
        x=6
        y=2*x+13
        return y
    z=test01()
    print(z)
    View Code

    代码运行结果:

    25

    2. 函数的优势

      使用函数的好处如下:1)代码重用;2)保持一致性,容易维护;3)可拓展性比较好。

    3. 函数和过程的区别

        过程:没有返回值(return)的函数。

        例1:

          

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test01():
        '''
        过程:过程是没有返回值的函数!
        :return: 
        '''
        mess="helloword"
        print(mess)
    
    
    def test02():
        '''
        函数test02:这个函数有返回值,过程是没有返回值的函数。
        :return: 
        '''
        mess02="helloword02"
        print(mess02)
        return mess02
    
    test01()
    test02()
    View Code

    代码运行结果:

    helloword
    helloword02  

        

        例2: 

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test01():
        '''
        过程:过程是没有返回值的函数!
        :return:
        '''
        mess="helloword"
        print(mess)
    
    
    def test02():
        '''
        函数test02:这个函数有返回值,过程是没有返回值的函数。
        :return:
        '''
        mess02="helloword02"
        print(mess02)
        return mess02
    
    a1=test01()
    a2=test02()
    test01()
    test02()
    print(a1)
    print(a2)
    View Code

    代码运行结果:

    helloword
    helloword02
    helloword
    helloword02
    None
    helloword02 

        例3:

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test01():
        '''
        过程:过程是没有返回值的函数!
        :return:
        '''
        mess="helloword"
        print(mess)
    
    
    def test02():
        '''
        函数test02:这个函数有返回值,过程是没有返回值的函数。
        :return:
        '''
        mess02="helloword02"
        print(mess02)
        return mess02,(22,33,),[11,"abc",(3,"www",)],{"name":"lily","age":23}
    
    a1=test01()
    a2=test02()
    test01()
    test02()
    print(a1)
    print(a2)
    View Code

    代码运行结果:

    helloword
    helloword02
    helloword
    helloword02
    None
    ('helloword02', (22, 33), [11, 'abc', (3, 'www')], {'name': 'lily', 'age': 23})

    4. 函数的参数

        函数参数的分类

            函数的参数主要分为形参和实参。形参即变量名,实参即变量值,函数调用时,将值绑定到变量名上,函数调用结束,解除绑定。

            例1:形参和实参

            

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test01(x,y):
    
        '''
        这个函数功能是z=x**y
        x:x是形参
        y:y是形参
        x**y:x的y次方
        z=x**y:x的y次方赋值给z
        '''
        z=x**y
        return z
    
    r=test01(5,3)
    
    '''
    5和3是实参
    '''
    print(r)
    View Code

    代码运行结果:

    125

            例2:关键字参数赋值传参

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test01(x,y,z):
        print(x)
        print(y)
        print(z)
    #3,5,10这种赋值参数叫做关键字参数,位置传参顺序无需固定
    test01(x=3,z=5,y=10)
    View Code

    代码运行结果:

    3
    10
    5

             例3:位置参数和关键字参数混用,位置参数必须放在关键字参数的左边。

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test02(x,y,z):
        print(x)
        print(y)
        print(z)
    #位置参数和关键参数混用,位置参数必须放在关键参数左边。
    test02(1,3,z=9)
    View Code

    代码运行结果:

    1
    3
    9

            例4:默认参数

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    def test03(x,type="haha"):
        '''
        type="haha":类似这种叫做"默认参数"
        '''
        print(x)
        print(type)
    test03("hehe")
    View Code

    代码运行结果:

    hehe
    haha

            例5:参数组:**表示字典,*表示列表,它们也叫可变长参数,有利于后期开发新功能易于扩展!

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    #参数组:**表示字典,*表示列表
    def test04(x,*y):
        print(x)
        print(y)
    test04(1,2,3,4,5)
    View Code

    代码运行结果:

    1
    (2, 3, 4, 5)

            例6:参数组:**表示字典,*表示列表,它们也叫可变长参数,有利于后期开发新功能易于扩展!

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    #参数组:**表示字典,*表示列表
    def test05(x,*y):
        print(x)
        print(y)
        print(y[0][0])
    test05(22,[1,2,3])
    View Code

    代码运行结果:

    22
    ([1, 2, 3],)
    1

            例7: 参数组:**表示字典,*表示列表,它们也叫可变长参数,有利于后期开发新功能易于扩展!

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    #参数组:**表示字典,*表示列表
    def test06(x,**kwargs):
        #**kwargs:接受字典传参
        print(x)
        print(kwargs)
    test06(22,y=2,z=9)
    View Code

    代码运行结果:

    22
    {'z': 9, 'y': 2}

            例8:参数组:**表示字典,*表示列表,它们也叫可变长参数,有利于后期开发新功能易于扩展!

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    #参数组:**表示字典,*表示列表
    def test07(x,*args,**kwargs):
        print(x)
        print(args)
        print(kwargs)
    test07(3,5,1,2,4,y=11,z=20,r=666)
    View Code

    代码运行结果:

    3
    (5, 1, 2, 4)
    {'r': 666, 'z': 20, 'y': 11}

            例9:参数组:**表示字典,*表示列表,它们也叫可变长参数,有利于后期开发新功能易于扩展!

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    #参数组:**表示字典,*表示列表
    def test07(x,*args,**kwargs):
        print(x)
        print(args,args[-1])
        print(kwargs,kwargs["y"])
    test07(3,5,1,2,4,y=11,z=20,r=666)
    View Code

    代码运行结果:

    3
    (5, 1, 2, 4) 4
    {'r': 666, 'z': 20, 'y': 11} 11

            例10:参数组:**表示字典,*表示列表,它们也叫可变长参数,有利于后期开发新功能易于扩展!

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    #参数组:**表示字典,*表示列表
    def test08(x,*args,**kwargs):
        print(x)
        print(args,args[-1])
        print(kwargs,kwargs["qq"])
    test08(11,*[1,2,3],**{"qq":"88888"})
    View Code

    代码运行结果:

    11
    (1, 2, 3) 3
    {'qq': '88888'} 88888

    你不向我走来,我便向你走去。
  • 相关阅读:
    POJ2993——Emag eht htiw Em Pleh(字符串处理+排序)
    POJ2109——Power of Cryptography
    POJ2993——Help Me with the Game(字符串处理+排序)
    POJ1573——Robot Motion
    POJ2632——Crashing Robots
    POJ1068——Parencodings
    POJ3295——Tautology
    POJ2506——Tiling
    POJ2524——Ubiquitous Religions
    性能问题“三板斧”
  • 原文地址:https://www.cnblogs.com/renyongbin/p/15739270.html
Copyright © 2011-2022 走看看