python3 函数

函数(function)

什么是函数:

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

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

语法:

def 函数名(参数列表):

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

语法说明:

函数名是语句块的名称;

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

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

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

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

定义函数

1 defmy_test():2 print("hello, this is line one.")3 print("hello, this is line two.")4 print("hello, this is line three.")5

6 defmy_test_2(a,b):7 print("%s and %s" % (a,b))

View Code

函数的调用:

函数名(实际参数)

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

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

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

1 defmy_test():2 print("hello, this is line one.")3 print("hello, this is line two.")4 print("hello, this is line three.")5

6 defmy_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 isline one.14 hello, this isline two.15 hello, this isline three.16 hello, this isline one.17 hello, this isline two.18 hello, this isline three.19 100 and 200

View Code

1 defmy_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 defmy_test_2(a,b):8 print("%s and %s" %(a,b))9 sum2 = a +b10 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 isline one.19 hello, this isline two.20 hello, this isline three.21 hello, this isline one.22 hello, this isline two.23 hello, this isline 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 defdoNothing():2 pass

3

4

5 doNothing()

View Code

1 defmy_test():2 print("hello")3

4

5 #print( my_test() ) # 相当于以下3条语句

6 _temp =my_test()7 print(_temp)8 del_temp9

10 ============

11 hello12 None

View Code

return 语句:

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

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

1 defmy_test():2 print("hello")3

4

5 r =my_test()6 print(r) #结果None

7 #执行结果

8 #hello

9 #None

10 #================================

11 defmy_test():12 print("hello")13 returnNone14

15

16 r =my_test()17 print(r) #结果None

18 #执行结果

19 #hello

20 #None

21 #===============================

22 defmy_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 deffun1():2 return

3 print("此行不会打印")4

5

6 r =fun1()7 print("r =", r)8 #================

9 r = None

View Code

1 deffun2():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 defsum3(a,b,c):2 print(sum([a,b,c]))3 returnsum([a,b,c])4

5 defpow3(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 defmyfunc(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+d3

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 inargs: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个参数是: one23 第 2 个参数是: 20

24 第 3个参数是: three25 第 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 inkwargs.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 -->xiaoming16 {'age': 20, 'name': 'xiaoming'}17 参数的个数: 4

18 c --> [1, 2, 3, 4]19 b -->BB20 d -->True21 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 defisprime(x):2 if x <= 1: returnFalse3 for i in range(2, x):4 if x % i ==0:5 returnFalse6 returnTrue7

8

9 print(isprime(1))10 print(isprime(2))11 print(isprime(3))12 print(isprime(5))13 print(isprime(6))14 #15

16 defprime_m2n(m,n):17 L =[]18 for x in range(m, n + 1):19 ifisprime(x):20 L.append(x)21 returnL22

23

24 prime_m2n(1,10)25 prime_m2n(1,20)26 M = prime_m2n(1,50)27 print(M)28 #29 defprimes(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 deffunc(x):3 x.append(10)4

5

6 func(L)7 print(L)8 #L的值是10

9

10 #栗子2

11 D ={}12 deffunc2(x):13 x["name"] = "xiaoming"

14

15

16 func2(D)17 print(D) #{'name': 'xiaoming'}

View Code

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

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

1 L =[]2 def func(x=[]):3 whileTrue:4 names = input("请输入学生姓名:")5 if notnames:6 break

7 x.append(names)8 returnx9

10

11 #方式1,

12 r =func()13 print(r)14 #方式2,

15 func(L)16 print(L)17 ===============

18 请输入学生姓名:aa19 请输入学生姓名:bb20 请输入学生姓名:21 ['aa', 'bb']22 请输入学生姓名:cc23 请输入学生姓名:dd24 请输入学生姓名:25 ['cc', 'dd']

View Code

函数嵌套

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

函数变量

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

1 deffn():2 print("hello")3

4

5 f1 =fn6 f1() #等同于调用函数fn()

View Code

1 def fn_outer(): #外部函数

2 print("外部函数被调用")3 deffn_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 deffunc1():2

3 deffunc2():4 print("hello")5 returnfunc26

7 fn =func1()8 fn() #返回hello

View Code

练习: 加乘法

1 defget_op(op):2 if op == '+':3 defmyadd(x,y):4 return x +y5 returnmyadd6 elif op == '*':7 defmymul(x,y):8 return x *y9 returnmymul10

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 deftab(x, y):2 return "|" + x.center(12) + "|" + y.center(12) + "|"

3

4 defstring(x, y):5 return "names :" + x + "ages :" +y6

7 defmyprint(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 defgoodbye(L):2 for x inL:3 print("886:", x)4

5 defhello(L1):6 for x inL1:7 print("hello:", x)8

9 defoperator(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 deffn():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 deffun1():4 v = 200

5 print("fun1_v :", v)6 deffun2():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 deffn():3 v = 200

4

5

6 fn()7 print(v) #100

View Code

global 语句

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

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

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

1 v = 100

2 deffn():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 deffn2():2 v = 200

3 global v #错的

4

5 fn2()6 print("3:",v)

View Code

1 #4global 变量列表里的变量名不能出现在此作用域的参数列表里

2

3 deffn3(v):4 globalv5 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 defoutter():4 var = 200

5 definner():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 deff1():3 v = 100

4 deff2():5 v = 200

6 deff3():7 nonlocal v8 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

第十篇学会编写python代码_Python之路,第十篇:Python入门与基础10相关推荐

  1. 浪漫的python代码_七夕,最浪漫的Python代码送给陷入爱河的Python男同胞

    七夕,最浪漫的Python代码送给陷入爱河的Python男同胞 image.gif ​ 三句话情书,我们听过.如何用python写一段浪漫的代码?可以是简短有韵味的情书式, 也欢迎冗长而效果拔群的万行 ...

  2. 编写五子棋的完整python代码_python实现五子棋小程序

    本文实例为大家分享了python实现五子棋小程序的具体代码,供大家参考,具体内容如下 一.结合书上例子,分三段编写: wuziqi.py #coding:utf-8 from win_notwin i ...

  3. python编写圣诞树代码_python 实现圣诞树

    #python代码实现圣诞树 import turtle screen = turtle.Screen() screen.setup(800,600) circle = turtle.Turtle() ...

  4. Nature论文爆出千行Python代码Bug,或影响百篇学术论文

    你的论文借鉴了 Nature 文章的代码?对不起,论文可能要重新写了. 在 10 月 8 日发表于<ACS Publication>期刊的一篇论文中,来自夏威夷大学的 Jayanti Bh ...

  5. 初学者怎样看懂python代码_Python零基础入门-(如何让人读懂你的代码)文档注释

    目标注释的作用 单行注释(行注释) 多行注释(块注释) 01. 注释的作用使用用自己熟悉的语言,在程序中对某些代码进行标注说明,增强程序的可读性 02. 单行注释(行注释)以 # 开头,# 右边的所有 ...

  6. pycharm怎么编写python代码_如何设置PyCharm中的Python代码模版(推荐)

    在MacOs运行的PyCharm中,执行python文件,如果不指定python文件字符编码会报错: SyntaxError: Non-ASCII character '\xe6' in file / ...

  7. 初学者怎样看懂python代码_Python 初学者,如何快速的入门

    随着疫情的到来,不少人开启了退休之前最长的一次度假,唯一不同的是,这次的度假是在家里完成的.同时还面临着低薪,高额的房租房贷卡贷等. 所以就有了油然而生的想法,何不在这唯一一次长时间的假期来学习一项技 ...

  8. 一段有趣的python代码_Python 五个有趣的彩蛋,你都知道吗?

    阅读文本大概需要 6.6 分钟. 当一门编程语言是开源的时候,往往会有产生一些搞笑和有趣的东西.通常,这意味着社区的贡献者会为该语言添加一些有趣和特别的彩蛋以及隐藏的特性(当然前提是不会增加在生产环境 ...

  9. 拉格朗日插值python代码_Python实现的拉格朗日插值法示例

    本文实例讲述了Python实现的拉格朗日插值法.分享给大家供大家参考,具体如下: 拉格朗日插值简单介绍 拉格朗日插值法是以法国十八世纪数学家约瑟夫·拉格朗日命名的一种多项式插值方法. 许多实际问题中都 ...

最新文章

  1. 百度智能小程序开源联盟正式成立 小程序开发创业又一新风向标
  2. 三极管形象经典的理解
  3. iOS内存管理编程指南
  4. [iphone]XML 解析 之 TBXML 介绍
  5. K8S部署工具:KubeOperator集群规划-自动模式
  6. 温故知新-[CPP]-Eclipse-CPP中一个Project多个Main
  7. 前端学习(796):字符串不可变
  8. python zip函数转制_Python zip函数及用法
  9. Deep Learning 【Nature review】
  10. python中request方法_如何使用python语言中的request模块获取代码
  11. rmmod无法卸载驱动_从hello world到LED驱动
  12. C# Winform编程ListBox之DrawItem事件
  13. 英语字帖电子版可打印_一年级英语字母专项练习:含26个英文字母书写教学 可下载电子版...
  14. VB基础知识之Do...Loop循环
  15. 李宏毅机器学习2022-hw2
  16. 灵感之源之十多年技术人生的经验与心得
  17. 用java做出长方体的表面积_Android Studio实现长方体表面积计算器
  18. java算法一个岛屿上一个说真话 一个说假话_面试常问智力题40道(逻辑题)+ 参考答案...
  19. 程序员转行可以做什么?
  20. Face Super-Resolution Guided by 3D Facial Priors(ECCV2020)论文解读

热门文章

  1. R语言循环中break函数和next函数使用实战
  2. R语言生成对数线性间隔数据序列(Log-linearly Spaced Sequences)
  3. 【转载】Python的运行原理(编译过程及执行原理)
  4. 短序列拼接软件velvet简介
  5. Keras Bug 解决方法 Exception ignored in: bound method BaseSession.__del__ of
  6. python 播放 wav 文件
  7. unbutu18.04.4 截图工具 flameshot
  8. Chrome与chromedriver版本对应
  9. linux对文件的描述,对Linux文件系统的简单理解
  10. centos上安装zookeeper