Python之路,第十篇:Python入门与基础10

python3 函数

函数(function)

       什么是函数:

              函数是可以重复执行的代码块,可以重复使用;

        作用: 定义用户级的函数;实现了一个代码块的封装;

        语法:

        def   函数名(参数列表):

                语句块(代码块)......

        语法说明:

               函数名是语句块的名称;

               函数名的命名规则和变量名相同(标识符);

               函数的名字是一个变量,是绑定代码块的名称;

               函数有自己的名字空间,要让函数处理外部数据需要用参数给函数传入一些数据,如果不需要传入参数,参数列表可以为空;

               语句块部分不能为空,如果为空填充pass语句; 

定义函数

1 def my_test():
2     print("hello, this is line one.")
3     print("hello, this is line two.")
4     print("hello, this is line three.")
5     
6 def my_test_2(a,b):
7     print("%s and %s" % (a,b))
View Code

 

   

        函数的调用:

               函数名(实际参数)

              调用说明: 1,函数调用是一个表达式;(表达式用于语句中,表达式通常会返回一个值;语句可以是表达式,也可以是表达式组合;)

                                2,如果没有return语句, 函数执行完毕返回None值对象;

                                3, 如果函数需要返回其他的值对象函数需要return语句;

 1 def my_test():
 2     print("hello, this is line one.")
 3     print("hello, this is line two.")
 4     print("hello, this is line three.")
 5 
 6 def my_test_2(a,b):
 7     print("%s and %s" % (a,b))
 8 
 9 my_test()  #函数的调用
10 my_test()
11 my_test_2(100,200)
12 
13 hello, this is line one.
14 hello, this is line two.
15 hello, this is line three.
16 hello, this is line one.
17 hello, this is line two.
18 hello, this is line three.
19 100 and 200
View Code
 1 def my_test():
 2     print("hello, this is line one.")
 3     print("hello, this is line two.")
 4     print("hello, this is line three.")
 5 
 6 
 7 def my_test_2(a,b):
 8     print("%s and %s" % (a,b))
 9     sum2 = a + b
10     print("sum2 = " , sum2)
11 
12 
13 my_test()  #函数的调用
14 my_test()
15 my_test_2(100,200)
16 my_test_2(300,400)
17 #print(sum2)  出错,sum2 不存在
18 hello, this is line one.
19 hello, this is line two.
20 hello, this is line three.
21 hello, this is line one.
22 hello, this is line two.
23 hello, this is line three.
24 100 and 200
25 sum2 =  300
26 300 and 400
27 sum2 =  700
28 Traceback (most recent call last):
29 
30     print(sum2)
31 NameError: name 'sum2' is not defined
View Code
1 def doNothing():
2     pass
3 
4 
5 doNothing()
View Code
 1 def  my_test():
 2     print("hello")
 3 
 4 
 5 #print( my_test() ) # 相当于以下3条语句
 6 _temp = my_test()
 7 print(_temp)
 8 del _temp
 9 
10 ============
11 hello
12 None
View Code

 

         return 语句:

                 语法:  return [表达式]    ;([ ]  代表可以省略))

                 作用: 结束当前函数的执行,返回到调用该函数的地方,同时返回一个值对象的引用关系;

 1 def my_test():
 2     print("hello")
 3 
 4 
 5 r = my_test()
 6 print(r)   #结果None
 7 #执行结果
 8 #hello
 9 #None
10 #================================
11 def my_test():
12     print("hello")
13     return None
14 
15 
16 r = my_test()
17 print(r)  # 结果None
18 # 执行结果
19 # hello
20 # None
21 #===============================
22 def my_test():
23     print("hello")
24     return 123
25 
26 r = my_test()
27 print(r)  # 结果None
28 # 执行结果
29 # hello 函数被调用
30 # 123
View Code

 

                  语法说明:1 ,return后跟的表达式可以省略,省略后相当于return None

                                    2, 如果函数内没有return语句,则函数执行完最后一条语句后,返回None (相当于加了一条return None语句);

                                    3, 函数的调用可以返回一个值或者一组值;

补充:return 语句就是讲结果返回到调用的地方,并把程序的控制权一起返回 ;

程序运行到所遇到的第一个return即返回(退出def块),不会再运行第二个return。

要返回两个数值,写成一行即可

1 def fun1():
2     return
3     print("此行不会打印")
4 
5 
6 r = fun1()
7 print("r = ", r)
8 #================
9 r =  None
View Code

 

1 def fun2():
2     #return 123
3     return [1,2,3]
4 
5 
6 x,y,z = fun2()
7 print("x =", x,"y =",y,"z =",z)
8 #==========================
9 x = 1 y = 2 z = 3
View Code

 

         练习:

 1 def sum3(a,b,c):
 2     print(sum([a,b,c]))
 3     return sum([a,b,c])
 4 
 5 def pow3(x):
 6     print(pow(x, 3))
 7     return pow(x, 3)
 8 
 9 
10 #立方的和
11 p1 = pow3(1)
12 p2 = pow3(2)
13 p3 = pow3(3)
14 s = sum3(p1,p2,p3)
15 print("立方的和",s)
16 
17 #和的立方
18 r = sum3(1,2,3)
19 p = pow3(r)
20 print("3个数和的立方:",p)
21 #====================
22 1
23 8
24 27
25 36
26 立方的和 36
27 6
28 216
29 3个数和的立方: 216
View Code

         函数的参数传递:

传递方式:1,位置传参;  2, * 序列传参;    3, ** 关键字传参

位置传参: 实参对应关系与形参对应关系是以位置来依次对应的;

       说明: 1, 实参和形参能过位置进行对应和传递; 2,实参和形参的个数必须完全相同;

序列传参:序列的元素个数必须与列表的个数相同;

关键字传参:1, 关键字传参是指传参时,按着形参的名字给形参赋值;  2, 实参和形参按名称进行匹配;

          说明: 1,字典传参的键名和形参名必须一致; 2,键名必须为字符串;  3,键名要在形参中存在;

综合传参:以上3种传递方式混合使用;

 1 def myfunc(a, b, c):
 2     print("a-->", a)
 3     print("b-->", b)
 4     print("c-->", c)
 5 
 6 #位置参数
 7 myfunc(10,20,30)
 8 #序列参数
 9 s1 = [11, 12, 13]
10 myfunc(*s1)  #等同于myfunc(s1[0],s1[1],s[2])
11 
12 s2 = (11.1, 12.1, 13.1)
13 myfunc(*s2)
14 
15 s3 = "ABC"
16 myfunc(*s3)
17 
18 #关键字传参
19 myfunc(a=20, b=21, c=22)
20 myfunc(c=101, a=99, b=100)
21 d1 = {'c':31 , 'b':30 , 'a':29} #字典关键字传参
22 myfunc(**d1)
23 
24 #混合传参
25 myfunc(10, *(20,30))
26 myfunc(*[100,200], 300)
27 myfunc(*[100],200,*(300,))
28 myfunc(100,a=200, c=300)
View Code

 

    

             函数的定义:

                    创建函数,函数形参

函数的缺省参数:

语法: 

         def    函数名(形参1=默认参数1, 形参2=默认参数2,....):

                 语句....

缺省参数说明:1, 缺省参数必须自右向左依次存在,如果一个参数有缺省值,则其右侧所有的参数必须要有缺省参数(缺省值); 

                         2, 缺省参数可以有0 个或多个,甚至全部都有缺省参数;

 

 1 def sum4(a, b, c=0, d=0):
 2     return a+b+c+d
 3 
 4 
 5 print(sum4(1, 2))
 6 print(sum4(1.1, 2.2, 3.3))
 7 print(sum4(100,200,300,400))
 8 ===============
 9 3
10 6.6
11 1000
View Code

 

函数的不定长参数:

        有两种:1 , 单星号元组传参;  2, 双星号字典传参;

 单星号元组传参:

        语法:  def    函数名(*元组形参名):

                            语句...

 1 函数不定长形参
 2 def myfunc(*args):
 3     print("形参的个数:", len(args))
 4     print(args)
 5     i = 1
 6     for x in args:
 7         print("",i ,"个参数是:", x)
 8         i += 1
 9 
10 
11 myfunc(1, 2)
12 print("-" * 100)
13 myfunc("one", 20, "three", 50)
14 =================================
15 形参的个数: 2
16 (1, 2)
17 第 1 个参数是: 1
18 第 2 个参数是: 2
19 ----------------------------------------------------------------------------------------------------
20 形参的个数: 4
21 ('one', 20, 'three', 50)
22 第 1 个参数是: one
23 第 2 个参数是: 20
24 第 3 个参数是: three
25 第 4 个参数是: 50
View Code

 

命名关键字形参(named):

语法:      def    函数名(* , 命名关键字形参名):

                         语句块...

或语法:    def    函数名(*args , 命名关键字形参名):

                         语句块... 

 1 def  myfunc(a, b, *, c):  #c 为命名关键字形参
 2     print(a, b, c)
 3 
 4 
 5 myfunc(1,3,c=5)  #对的
 6 #myfunc(11,22,33)  #错的
 7 
 8 def myfunc2(a, *args, b, c): #b ,c 为命名关键字形参
 9     print(a, b, c, args)
10 
11 
12 #myfunc2(1, 2, 3, 4)  #错的
13 myfunc2(1,2, b=3, c=5)  #对的
14 myfunc2(1,2,3,4,5,6, b=3, c=5) #对的
15 myfunc2(1, b=3, c=5)   #对的
16 ==================
17 #执行结果
18 #1 3 5
19 #1 3 5 (2,)
20 #1 3 5 (2, 3, 4, 5, 6)
21 #1 3 5 ()  空元组
View Code

 

双星号字典传参:

        语法:  def    函数名(**字典形参名):

                            语句...

 1 def myfunc(**kwargs):
 2     print("参数的个数:",len(kwargs))
 3     for k,v in kwargs.items():
 4         print(k,"-->",v)
 5     print(kwargs)
 6     
 7 
 8 #调用
 9 myfunc(name="xiaoming", age=20)
10 myfunc(a=10, b="BB", c=[1,2,3,4], d=True)
11 #myfunc(1,2,3) 错的
12 =================
13 参数的个数: 2
14 age --> 20
15 name --> xiaoming
16 {'age': 20, 'name': 'xiaoming'}
17 参数的个数: 4
18 c --> [1, 2, 3, 4]
19 b --> BB
20 d --> True
21 a --> 10
22 {'c': [1, 2, 3, 4], 'b': 'BB', 'd': True, 'a': 10}
View Code

     练习:

 

 1 def minmax(*args):
 2     if len(args) < 2:
 3         print("参数量太少。")
 4     #求最小值
 5     min_v = args[0]
 6     for i in range(1,len(args)):
 7         if min_v > args[i]:
 8             min_v = args[i]
 9     #求最大值
10     max_v = args[0]
11     for i in range(1,len(args)):
12         if args[i] > max_v:
13             max_v = args[i]
14 
15     return (min_v,max_v)
16 
17 
18 x,y = minmax(11,22,31,14,25,36)
19 print("最小值",x)
20 print("最大值:",y)
View Code

 

函数的参数列表顺序:

         位置形参, 缺省参数, 单星号元组形参, 双星号字典形参,命令关键字参数 都可以混合使用。

参数自左至右的顺序为:

        位置形参, 单星号元组形参,命令关键字参数, 双星号字典形参,

 例子:  def    func( a,  b,  *args,  c,  **kwargs ) :

                    pass

             func( 100,200, 300, 400, c='C' ,  d='D' , e='E' )

详细语法:help(def)

练习:

 1 def isprime(x):
 2     if x <= 1: return False
 3     for i in range(2, x):
 4         if x % i == 0:
 5             return False
 6         return True
 7 
 8 
 9 print(isprime(1))
10 print(isprime(2))
11 print(isprime(3))
12 print(isprime(5))
13 print(isprime(6))
14 #
15 
16 def prime_m2n(m,n):
17     L = []
18     for x in range(m, n + 1):
19         if isprime(x):
20             L.append(x)
21     return L
22 
23 
24 prime_m2n(1,10)
25 prime_m2n(1,20)
26 M = prime_m2n(1,50)
27 print(M)
28 #
29 def primes(n):
30     return prime_m2n(1, n)
31 
32 
33 L = primes(100)
34 print(L) 
35 
36 #结果
37 #False
38 #None
39 #True
40 #True
41 #False
42 #[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
43 #[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
View Code

 

可变和不可变类型 的实参的参数传递的区别:

可变:list,  set,  dict

不可变:frozenset , tuple, str, float,  int....

问题:函数只能通过返回值传递参数吗?

 1 L = []
 2 def func(x):
 3     x.append(10)
 4 
 5 
 6 func(L)
 7 print(L)
 8 #L的值是10
 9 
10 #栗子2
11 D = {}
12 def func2(x):
13     x["name"] = "xiaoming"
14     
15 
16 func2(D)
17 print(D) #{'name': 'xiaoming'}
View Code

 

区别: 不可变的类型的数据作为函数参数传入时,函数内部不会改变变量的源数据值,是安全的;

            可变类型的数据作为函数参数传入时,函数内部可变原数据,多用来返回更多数据结果;

 1 L = []
 2 def func(x=[]):
 3     while True:
 4         names = input("请输入学生姓名:")
 5         if not names:
 6             break
 7         x.append(names)
 8     return x
 9 
10 
11 #方式1,
12 r = func()
13 print(r)
14 #方式2,
15 func(L)
16 print(L)
17 ===============
18 请输入学生姓名:aa
19 请输入学生姓名:bb
20 请输入学生姓名:
21 ['aa', 'bb']
22 请输入学生姓名:cc
23 请输入学生姓名:dd
24 请输入学生姓名:
25 ['cc', 'dd']
View Code

 

函数嵌套

       函数嵌套是指一个函数里用语句来创建其他函数的情况;

函数变量

       函数名是变量, 它在创建函数是绑定一个函数

1 def fn():
2     print("hello")
3 
4 
5 f1 = fn
6 f1() #等同于调用函数fn()
View Code

 

 1 def fn_outer(): #外部函数
 2     print("外部函数被调用")
 3     def fn_inner():
 4         print("fn_inner被调用")
 5     fn_inner()
 6     fn_inner()
 7     print("外部函数被调用结束")
 8     
 9 
10 fn_outer()
11 ===================
12 外部函数被调用
13 fn_inner被调用
14 fn_inner被调用
15 外部函数被调用结束
View Code

 函数作为函数的返回值

1 def func1():
2 
3     def func2():
4         print("hello")
5     return func2
6 
7 fn = func1()
8 fn() #返回hello
View Code

 练习: 加乘法

 1 def get_op(op):
 2     if op == '+':
 3         def myadd(x,y):
 4             return x + y
 5         return myadd
 6     elif op == '*':
 7         def mymul(x,y):
 8             return x * y
 9         return mymul
10 
11 
12 a = int(input("Please enter the first number:"))
13 operator = input("请输入操作方式:")
14 b = int(input("Please enter the second number:"))
15 fn = get_op(operator)
16 print("Result:", fn(a, b))
17 =====================================
18 Please enter the first number:3
19 请输入操作方式:+
20 Please enter the second number:4
21 Result: 7
22 Please enter the first number:3
23 请输入操作方式:*
24 Please enter the second number:6
25 Result: 18
View Code

 

函数作为函数的参数传递

 1 def tab(x, y):
 2     return "|" + x.center(12) + "|" + y.center(12) + "|"
 3 
 4 def string(x, y):
 5     return "names : " + x + "ages : " + y
 6 
 7 def myprint(fn, x, y):
 8     s = fn(x, y)
 9     print(s)
10 
11 
12 myprint(tab, "xiaoming", "18")
13 myprint(string, "xiaoli", "19")
14 #结果
15 #|  xiaoming  |     18     |
16 #names : xiaoliages : 19
View Code
 1 def goodbye(L):
 2     for x in L:
 3         print("886:", x)
 4 
 5 def hello(L1):
 6     for x in L1:
 7         print("hello:", x)
 8 
 9 def operator(fn, M):
10     fn(M)
11 
12 
13 operator(goodbye, ("xiaoming","xiaoli"))
14 operator(goodbye, ["xiaoming","xiaoli"])
15 operator(hello, ("xiaoming","xiaoli"))
16 operator(hello, ["xiaoming","xiaoli"])
17 #结果
18 #886: xiaoming
19 #886: xiaoli
20 #886: xiaoming
21 #886: xiaoli
22 #hello: xiaoming
23 #hello: xiaoli
24 #hello: xiaoming
25 #hello: xiaoli
View Code

 

全局变量和局部变量

局部变量: 定义在函数内部的变量(包含函数参数) 

全局变量: 定义在函数外部,模块内部的变量

 1 v = 100  #此为全局变量
 2 
 3 def fn():
 4     v = 200 #此为局部变量
 5     print(v)
 6 
 7 
 8 fn()
 9 print(v)
10 #结果
11 #200
12 #100
View Code

 

python作用域

作用域: 也叫名字空间,是变量访问的时候查找变量名的范围空间;

python 四个作用域:

局部作用域(函数内)                            Local                                           L

外部嵌套函数作用域                               Encloseing  function  locals        E

函数定义所在模块(文件)的作用域     Global (module)                     G

python内置模块的作用域                        Builtin(Python)                        B

 

变量名的查找规则:

在访问变量时, 先查找本地变量, 然后是包裹此函数的外部函数的函数内部的变量, 之后是全局变量,最后是内置变量。

字母顺序:    L  -->   E   --->  G  --->  B

 1 v = 100
 2 
 3 def fun1():
 4     v = 200
 5     print("fun1_v :" , v)
 6     def fun2():
 7         v = 300
 8         print("fun2_v :",v)
 9     fun2()
10 
11 fun1()
12 print("v=", v)
13 #结果
14 #fun1_v : 200
15 #fun2_v : 300
16 #v= 100
View Code

 

在默认情况下,变量名赋值会创建或修改本地变量

1 v = 100
2 def fn():
3     v = 200
4    
5 
6 fn()
7 print(v) #100
View Code

 global 语句

作用:告诉解释器,global语句声明的一个或多个变量,这些变量的作用域为模块级的作用域,也称作全局变量;

           对全局声明(global)的变量赋值将映射到模块文件的内部作用域;

语法: global   变量名1 ,变量名2 ....

1 v = 100
2 def fn():
3     global v #声明全局变量
4     v = 200
5 
6 
7 fn()
8 print(v) #200
View Code

 

global 说明:1 , 全局变量如果要在函数内部被赋值, 则必须经过全局声明,否则 被认为是局部变量;

                      2, 全局变量在函数内部不经过声明就可以直接访问(前提是变量已经存在);

                      3, 不能先声明局部变量,再用global声明为全局变量,此做法不符合语法规则;

                      4, global 变量列表里的变量名不能出现在此作用域的参数列表里,for 循环控制目标,类定义,函数定义及import导入名字中;

1 def fn2():
2     v = 200
3     global v # 错的
4 
5 fn2()
6 print("3:",v)
View Code

 

1 #4global 变量列表里的变量名不能出现在此作用域的参数列表里
2 
3 def fn3(v):
4     global v
5     v = 300
6 
7 
8 fn3(11)
9 print(v) #SyntaxError: name 'v' is parameter and global
View Code

 

nonlocal语句

作用: 告诉解释器,nonlocal声明的变量不是局部变量,也不是全局变量,而是外部嵌套函数内的变量。

语法: nonlocal   变量名1, 变量名 .....

 nonlocal在嵌套函数里的内层函数里加载;

说明:1,nonlocal 语句只能在被嵌套函数的内层使用;

           2, 访问nonlocal 变量 将对外部嵌套函数 的作用域内的变量进行操作;

           3, 嵌套函数有两层或者两层以上时,访问nonlocal 变量只对最近一层的变量操作;

           4,nonlocal语句的变量列表里的变量名,不能出现在此作用域的参数列表里;

 1 vars = 100
 2 
 3 def outter():
 4     var = 200
 5     def inner():
 6         nonlocal var  #指定var为外层嵌套函数作用域
 7         var += 1 #么有nonlocal var,此行会出错UnboundLocalError: local variable 'var' referenced before assignment
 8         print("inner_var:",var)
 9     inner()
10     print("outter_var:",var)
11 
12 
13 outter()
14 =========================
15 #inner_var: 201
16 #outter_var: 201
View Code

 

 

 1 #shuoming3
 2 def f1():
 3     v = 100
 4     def f2():
 5         v = 200
 6         def f3():
 7             nonlocal v
 8             v = 400
 9             print("f3:",v)
10         f3()
11         print("f2:",v)
12     f2()
13     print("f1:",v)
14 f1()
15 =============
16 400
17 400
18 100
View Code

 

 

 

 

 

 

 

 

 

     

 

 

  

      

       

 

posted on 2018-05-06 18:50  微子天明  阅读(228)  评论(0编辑  收藏  举报

导航