第五章Python 函数编程

第五章Python 函数编程

本节所讲内容:

1.5.1  Python函数定义和调用

1.5.2  python函数参数

1.5.3  python函数的划分

1.5.1  Python函数定义

       函数(function):是一个可以被重复调用的带有一个入口和一个出口的固定的程序段

       函数比较代码复制

1、  减少冗余代码

2、  代码结构清晰

3、  有助于保持代码的一致性

 

function

python :

1、  关键字: def

2、  函数名:

命名规范:

1、  字母开头

2、  不允许有关键字

3、  不允许有特殊符号

4、  不允许莫名其妙的函数名 a,b

3、  参数:参数是定义在参数括号里,由调用时传入,作用在函数内部的变量

1、  如果有参数,写在参照括号里

2、  如果没有,写空括号

3、  缩进的语句块

 

def say_hello(name):

    print("hello I am "+name)

python函数的调用

       函数在没有调用之前不会执行

       函数名加括号,并且进行对应的传参的形式

 

       def say_hello(name):

            print("hello I am "+name)

say_hello("while")

 

hello I am while

>>> 

函数的参数

按照定义和传递我们将参数划分为形成和实参

在定义函数时定义的参数我们称之为 形参

在调用函数时我们传递值我们称之为 实参

       def say_hello(name):

            print("hello I am "+name)

 

say_hello("while")

say_hello("for")

       name形参

       "while" "for"实参

       我们在使用过程当中有划分出了五种参数

Python 2 当中是四种

              位置参数

              关键字参数

              默认参数

              参数组

                     命名关键字参数 python 3新加入

1.位置参数:

是我们在传参的时候,实参传递的顺序按照形参定义的顺序进行传递的传参方式。

       def say_hello(name,age):

            print("hello %s is %d years old "%(name,age))

 

say_hello("while",18)

say_hello(18,"while")

hello while is 18 years old

Traceback (most recent call last):

  File "C:/Users/bian/Desktop/python_function.py", line 13, in <module>

    say_hello(18,"while")

  File "C:/Users/bian/Desktop/python_function.py", line 10, in say_hello

    print("hello %s is %d years old "%(name,age))

TypeError: %d format: a number is required, not str

      

       def say_hello(name,age):

            print("hello %s is %d years old "%(name,age))

 

say_hello("while",18,10)

say_hello("while")

 

       Traceback (most recent call last):

  File "C:/Users/bian/Desktop/python_function.py", line 12, in <module>

    say_hello("while",18,10)

TypeError: say_hello() takes 2 positional arguments but 3 were given

>>>

Traceback (most recent call last):

  File "C:/Users/bian/Desktop/python_function.py", line 12, in <module>

    say_hello("while")

TypeError: say_hello() missing 1 required positional argument: 'age'

2.关键字参数

是我们在传参的时候,以形参等于实参的形式忽略形参定义的顺序进行传参的传参方式

       def say_hello(name,age):

            print("hello %s is %d years old "%(name,age))

 

say_hello("while",18)

say_hello(age = 18,name = "while")

 

hello while is 18 years old

hello while is 18 years old

>>> 

形参写错了

Traceback (most recent call last):

  File "C:/Users/bian/Desktop/python_function.py", line 22, in <module>

    say_hello(agea = 18,name = "while")

TypeError: say_hello() got an unexpected keyword argument 'agea'

3.默认值参数

是在我们定义参数时候,我们给形参一个默认值,在我们调用函数的时候,如果不给有默认值的形参传参,会自动采用默认值。

       def say_hello(name,age=18):

            print("hello %s is %d years old "%(name,age))

 

say_hello("while")

say_hello("while",19)

 

hello while is 18 years old

hello while is 19 years old

>>> 

注意:默认值参数必须写在正常参数的后面

 

4. 参数组

指我们将参数变成数组或者字典,忽略参数个数进行传参

元组参数组

        通过给形参前面添加*使参数变成一个元组,所有传递的参数变成元组的元素

def say_hello(*names):

    print(names)

 

say_hello()

say_hello("while","for","mk","rm")

say_hello(*range(10))

 

()

('while', 'for', 'mk', 'rm')

(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

>>> 

字典参数组

        通过给形参前面添加**使参数变成一个字典,所有传递的参数变成字典的键值对,这里传参要求键等于值的形式。

                     def say_hello(**kw):

                          print(kw)

 

say_hello()

say_hello(name = "while")

say_hello(**{"age":18})

                    

                     {}

{'name': 'while'}

{'age': 18}

>>> 

5.命名关键字参数

       在定义形参之前加入独立的一个*,要求传递参数的时候必须以正确的形参等于实参的形式进行传参

       def say_hello(*,name,age):

            print("hello %s is %d years old "%(name,age))

 

say_hello("while",19)

       Traceback (most recent call last):

      File "C:/Users/bian/Desktop/python_function.py", line 56, in <module>

            say_hello("while",19)

TypeError: say_hello() takes 0 positional arguments but 2 were given

 

       def say_hello(*,name,age):

            print("hello %s is %d years old "%(name,age))

 

say_hello(name="while",age=18)

      

hello while is 18 years old

>>> 

1.5.3 python函数划分

按照定义划分

                   传统函数

                     匿名函数

                            lambda

                            a = lambda x,y:print(x+y)

a(1,2)

                            3

>>> 

按照返回值划分

返回型函数  有返回值

计算型函数  没有返回值

                         return

                            def num():

                                 print("this is num")

                                 print(1)

 

def num_v1():

                                 print("this is num1")

                                 return 1

 

return 将函数当中的值返回出来

        def num_v1():

             return 1

   

print(num_v1()+3)

 

4

>>> 

return 结束,函数结束,函数当中return下面的语句不会执行

        def num_v1():

             return 1

             print(2)

 

num_v1()

 

return 只能返回一个值

                            def num_v1():

                                 return 1,2,3

 

print(num_v1())

 

(1, 2, 3)

>>> 

def num_v1():

    return 1,2,3

    return 1

print(num_v1())

 

(1, 2, 3)

>>> 

posted @ 2018-09-06 11:39  蘑菇慢慢  阅读(95)  评论(0)    收藏  举报