Review ---- python basis 3

This Section

1. Function

  1.1 Definitions and characteristics

  1.2 Parameters

  1.3 Local variables

  1.4 Return Values

2. recursive function

3. anonymous function

4. Higher-order functions

5. Built-in functions

 

 

1. Function

1.1 Definitions and characteristics

The word is derived from a mathematical function, but the "function" concept in programming, and mathematics functions are very different, specific differences, we will talk about later, functional programming in English, there are many different terminologies . It called the subroutine (subroutine or subroutine) in BASIC, a known procedure (process) in Pascal and function , in C only function, called the method in Java.

 

Definition: function refers to a collection of statements by a name (function name) encapsulates, in order to perform this function, simply call it the function name.

characteristic:

  1, to reduce the duplication of code

  2, making the program may be extended

  3, making the program easy to maintain

grammar:

. 1  DEF F ():   # function name 
2      Print ( ' the Hello AV8D ' )
 . 3  
. 4  
. 5 F ()      # calling function 
. 6  #    the Hello AV8D

 

It can also be parameterized

1  # following code 
2 A, B =. 5,. 8
 . 3 C = A ** B
 . 4  Print (C)
 . 5  
. 6  
. 7  # used instead of the write function 
. 8  DEF Calc (X, Y):
 . 9      RES = X ** Y
 10      return RES   # returns the execution result of the function 
. 11  
12 is  
13 is c = Calc (A, B)   # assign the result to a variable c 
14  Print (c)

 

1.2 Parameters

Parameter: variable only allocate memory when the unit is called, at the end of the call, immediately release the memory unit allocated. Thus, the parameter is valid only within the function . After the end of the function call returns to the main calling function you can no longer use the variable parameter.

Argument: can be constants, variables, expressions, functions, etc., regardless of what type of argument that the amount of, during function calls, they must have a certain value, in order to transfer the values to the parameters. It should therefore be determined in advance to obtain the parameter value assigned input and other methods.

 

Parameter must

在定义函数无其他参数形式时,形参与实参的数量需要一致且位置对应,否则会报错或赋值错误。

 1 def stu_register(name,age,country):
 2     print('姓名:',name)
 3     print('年龄:',age)
 4     print('居住地:',country)
 5 
 6 
 7 stu_register('Acool','22','广州')
 8 #输出:
 9 #   姓名: Acool
10 #   年龄: 22
11 #   居住地: 广州
12 
13 stu_register('Asue',21)
14 #   TypeError: stu_register() missing 1 required positional argument: 'country'

 

默认参数

可在定义函数时,直接赋予形参一个实参,此时为默认参数,调用时可再赋予实参。

注意,使用默认参数时,需要在非固定参数之前,在必须参数后;若无其他参数则必须在最后位。

 1 def stu_register(name,age,country='广州'):
 2     print('姓名:',name)
 3     print('年龄:',age)
 4     print('居住地:',country)
 5 
 6 
 7 stu_register('Acool','22','北京')
 8 #输出:
 9 #   姓名: Acool
10 #   年龄: 22
11 #   居住地: 北京
12 
13 stu_register('Asue',21)
14 #输出:
15 # 姓名: Asue
16 # 年龄: 21
17 # 居住地: 广州

 

关键字参数

正常情况下,给函数传参数要按顺序,不想按顺序就可以用关键参数,只需指定参数名即可,但记住一个要求就是,关键参数必须放在位置参数之后。

1 stu_register(country='北京',name='Acool',age='22')
2 #输出:
3 #   姓名: Acool
4 #   年龄: 22
5 #   居住地: 北京

 

非固定参数

若你的函数在定义时不确定用户想传入多少个参数,就可以使用非固定参数

 1 def stu_register(name, age, *args):  # *args 会把多传入的参数变成一个元组形式
 2     print(name, age, args)
 3 
 4 
 5 stu_register("Alex", 22)
 6 # 输出
 7 # Alex 22 () #后面这个()就是args,只是因为没传值,所以为空
 8 
 9 stu_register("Jack", 32, "CN", "Python")
10 # 输出
11 # Jack 32 ('CN', 'Python')

 

还可以有一个**kwargs

 1 def stu_register(name, age, *args, **kwargs):  # *kwargs 会把多传入的参数变成一个dict形式
 2     print(name, age, args, kwargs)
 3 
 4 
 5 stu_register("Alex", 22)
 6 # 输出
 7 # Alex 22 () {}#后面这个{}就是kwargs,只是因为没传值,所以为空
 8 
 9 stu_register("Jack", 32, "CN", "Python", sex="Male", province="ShanDong")
10 # 输出
11 # Jack 32 ('CN', 'Python') {'province': 'ShanDong', 'sex': 'Male'}

可混合使用,但非必要则不用。

 

1.3局部变量

全局与局部变量

在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量。

全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序。

当全局变量与局部变量同名时:在定义局部变量的子程序内,局部变量起作用;在其它地方全局变量起作用。

 

局部、嵌套、全局、内置变量(L_E_G_B原则)
 1 #变量的作用域
 2 
 3 count = 10
 4 def outer():
 5     print(count)  #报错,检查作用域内变量count在执行后面。不执行全局变量count,与全局变量count无关系
 6     count = 5       #报错内容:应将后面定义count放到前面去。如果无定义,则会说明:无定义变量。
 7 
 8 outer()
 9 
10 count = 10
11 def outer():
12     count = 5       #此count是新建的一个局部变量,不是上面的全局变量
13     print(count)    #不报错,执行打印局部变量count
14 
15 print(count)    #打印的是全局变量count
16 outer()         #执行的是函数内部的局部变量count
17 
18 
19 count =10
20 def outer():
21     global count    #声明全局变量count
22     count=5         #修改全局变量count
23     print(count)
24 print(count)        #还未调用函数,打印的是等于10的count
25 outer()             #调用函数,打印已修改的count

 

1.4返回值

要想获取函数的执行结果,就可以用return语句把结果返回。

注意:

  1、函数在执行过程中只要遇到return语句,就会停止执行并返回结果,so也可以理解为return语句代表着函数的结束

  2、如果未在函数中指定return,那这个函数的返回值为None

 

 

2.递归函数
在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
1 def f(n):
2     if n==1:
3         return 1
4     return n * f(n-1)
5 
6 print(f(5))
7 #   120

 

递归特性:

  1、调用自身函数

  2、有一个结束条件

  3、但凡是递归可以写的,循环都可以写

  4、递归的效率在很多时候会很低

 

斐波那契数列

 1 def feibo(n):
 2     if n<=2:
 3         return n-1
 4     # if n==1:
 5     #     return 0
 6     # elif n==2:
 7     #     return 1
 8     return feibo(n-1)+feibo(n-2)
 9 
10 print(feibo(8))   #从头为1开始,第8项
11 #   13

 

 

 

3.匿名函数

匿名函数就是不需要显式的指定函数

1 #这段代码
2 def calc(n):
3     return n**n
4 print(calc(10))
5  
6 #换成匿名函数
7 calc = lambda n:n**n
8 print(calc(10))

匿名函数主要是和其它函数搭配使用的呢,如下

 1 res = map(lambda x:x**2,[1,5,7,4,8])
 2 for i in res:
 3     print(i)
 4 
 5 #输出
 6 1
 7 25
 8 49
 9 16
10 64

 

 

4.高阶函数

变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。

1 def add(x, y, f):
2     return f(x) + f(y)
3 
4 
5 res = add(3, -6, abs)   #   abs为内置函数
6 print(res)
7 #   9

 

 

5.内置函数

 1 #内置函数----filter(只可过滤)
 2 str = ['a','b','c','d']
 3 
 4 def fun1(s):
 5     if s != 'a':
 6         return s
 7 
 8 ret= filter(fun1,str)   #('b','c','d')
 9 print(ret)  #<filter object at 0x00000168F58FEB00>
10 print(list(ret)) #ret是一个迭代器对象
11 
12 #内置函数----map(可添加)
13 str = ['d','v''a','b']
14 def fun2(s):
15     return s + 'alvin'
16 
17 ret = map(fun2,str)  #filter与map的区别
18 print(ret)    #map object的迭代器
19 print(list(ret))    #['dalvin', 'vaalvin', 'balvin']
20 
21 #内置函数----reduce
22 from functools import reduce
23 
24 def add1(x,y):
25     return x + y
26 
27 print(reduce(add1,range(1,10)))  #从1至9相加,x=1 y=2得出来的3重新赋值于x y=3,再相加
28 # 45
29 
30 #内置函数----lambda
31 from functools import reduce
32 
33 print(reduce(lambda x,y:x+y,range(1,10)))
34 # 45
35 
36 squares = map(lambda x :x*x,range(9))
37 print(squares)      #   <map object at 0x0000020A8CFF3B00>
38 print(list(squares))    #   [0, 1, 4, 9, 16, 25, 36, 49, 64]

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/Yan-night/p/11204452.html