网站盈利方法,没有服务器 怎么做网站,海外打开网站慢,阳江招聘网最新招聘压铸工python3 函数函数(function)什么是函数#xff1a;函数是可以重复执行的代码块#xff0c;可以重复使用#xff1b;作用#xff1a; 定义用户级的函数#xff1b;实现了一个代码块的封装#xff1b;语法#xff1a;def 函数名(参数列表)#xff1a;语句块(代码块)...…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.)56 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.)56 defmy_test_2(a,b):7 print(%s and %s %(a,b))89 my_test() #函数的调用10 my_test()11 my_test_2(100,200)1213 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 200View Code1 defmy_test():2 print(hello, this is line one.)3 print(hello, this is line two.)4 print(hello, this is line three.)567 defmy_test_2(a,b):8 print(%s and %s %(a,b))9 sum2 a b10 print(sum2 , sum2)111213 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 20025 sum2 30026 300 and 40027 sum2 70028 Traceback (most recent call last):2930 print(sum2)31 NameError: name sum2 is not definedView Code1 defdoNothing():2 pass345 doNothing()View Code1 defmy_test():2 print(hello)345 #print( my_test() ) # 相当于以下3条语句6 _temp my_test()7 print(_temp)8 del_temp910 11 hello12 NoneView Codereturn 语句语法 return [表达式] ([ ] 代表可以省略))作用 结束当前函数的执行返回到调用该函数的地方同时返回一个值对象的引用关系1 defmy_test():2 print(hello)345 r my_test()6 print(r) #结果None7 #执行结果8 #hello9 #None10 #11 defmy_test():12 print(hello)13 returnNone141516 r my_test()17 print(r) #结果None18 #执行结果19 #hello20 #None21 #22 defmy_test():23 print(hello)24 return 1232526 r my_test()27 print(r) #结果None28 #执行结果29 #hello 函数被调用30 #123View Code语法说明1 return后跟的表达式可以省略省略后相当于return None2 如果函数内没有return语句则函数执行完最后一条语句后返回None (相当于加了一条return None语句)3 函数的调用可以返回一个值或者一组值补充return 语句就是讲结果返回到调用的地方并把程序的控制权一起返回 程序运行到所遇到的第一个return即返回(退出def块)不会再运行第二个return。要返回两个数值写成一行即可1 deffun1():2 return3 print(此行不会打印)456 r fun1()7 print(r , r)8 #9 r NoneView Code1 deffun2():2 #return 1233 return [1,2,3]456 x,y,z fun2()7 print(x , x,y ,y,z ,z)8 #9 x 1 y 2 z 3View Code练习1 defsum3(a,b,c):2 print(sum([a,b,c]))3 returnsum([a,b,c])45 defpow3(x):6 print(pow(x, 3))7 return pow(x, 3)8910 #立方的和11 p1 pow3(1)12 p2 pow3(2)13 p3 pow3(3)14 s sum3(p1,p2,p3)15 print(立方的和,s)1617 #和的立方18 r sum3(1,2,3)19 p pow3(r)20 print(3个数和的立方,p)21 #22 123 824 2725 3626 立方的和 3627 628 21629 3个数和的立方 216View 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)56 #位置参数7 myfunc(10,20,30)8 #序列参数9 s1 [11, 12, 13]10 myfunc(*s1) #等同于myfunc(s1[0],s1[1],s[2])1112 s2 (11.1, 12.1, 13.1)13 myfunc(*s2)1415 s3 ABC16 myfunc(*s3)1718 #关键字传参19 myfunc(a20, b21, c22)20 myfunc(c101, a99, b100)21 d1 {c:31 , b:30 , a:29} #字典关键字传参22 myfunc(**d1)2324 #混合传参25 myfunc(10, *(20,30))26 myfunc(*[100,200], 300)27 myfunc(*[100],200,*(300,))28 myfunc(100a200, c300)View Code函数的定义创建函数函数形参函数的缺省参数语法def 函数名(形参1默认参数1 形参2默认参数2....)语句....缺省参数说明1 缺省参数必须自右向左依次存在如果一个参数有缺省值则其右侧所有的参数必须要有缺省参数(缺省值)2 缺省参数可以有0 个或多个甚至全部都有缺省参数1 def sum4(a, b, c0, d0):2 return abcd345 print(sum4(1, 2))6 print(sum4(1.1, 2.2, 3.3))7 print(sum4(100,200,300,400))8 9 310 6.611 1000View Code函数的不定长参数有两种1 单星号元组传参 2 双星号字典传参单星号元组传参语法 def 函数名(*元组形参名)语句...1 函数不定长形参2 def myfunc(*args):3 print(形参的个数, len(args))4 print(args)5 i 16 for x inargs:7 print(第,i ,个参数是, x)8 i 191011 myfunc(1, 2)12 print(- * 100)13 myfunc(one, 20, three, 50)14 15 形参的个数 216 (1, 2)17 第 1 个参数是 118 第 2 个参数是 219 ----------------------------------------------------------------------------------------------------20 形参的个数 421 (one, 20, three, 50)22 第 1个参数是 one23 第 2 个参数是 2024 第 3个参数是 three25 第 4 个参数是 50View Code命名关键字形参(named)语法 def 函数名(* 命名关键字形参名)语句块...或语法 def 函数名(*args 命名关键字形参名)语句块...1 def myfunc(a, b, *, c): #c 为命名关键字形参2 print(a, b, c)345 myfunc(1,3,c5) #对的6 #myfunc(11,22,33) #错的78 def myfunc2(a, *args, b, c): #b ,c 为命名关键字形参9 print(a, b, c, args)101112 #myfunc2(1, 2, 3, 4) #错的13 myfunc2(1,2, b3, c5) #对的14 myfunc2(1,2,3,4,5,6, b3, c5) #对的15 myfunc2(1, b3, c5) #对的16 17 #执行结果18 #1 3 519 #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)678 #调用9 myfunc(namexiaoming, age20)10 myfunc(a10, bBB, c[1,2,3,4], dTrue)11 #myfunc(1,2,3) 错的12 13 参数的个数 214 age -- 2015 name --xiaoming16 {age: 20, name: xiaoming}17 参数的个数 418 c -- [1, 2, 3, 4]19 b --BB20 d --True21 a -- 1022 {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]1415 return(min_v,max_v)161718 x,y minmax(11,22,31,14,25,36)19 print(最小值,x)20 print(最大值:,y)View Code函数的参数列表顺序位置形参 缺省参数 单星号元组形参 双星号字典形参命令关键字参数 都可以混合使用。参数自左至右的顺序为位置形参 单星号元组形参命令关键字参数 双星号字典形参例子 def func( a, b, *args, c, **kwargs ) :passfunc( 100,200, 300, 400, cC , dD , eE )详细语法help(def)练习1 defisprime(x):2 if x 1: returnFalse3 for i in range(2, x):4 if x % i 0:5 returnFalse6 returnTrue789 print(isprime(1))10 print(isprime(2))11 print(isprime(3))12 print(isprime(5))13 print(isprime(6))14 #1516 defprime_m2n(m,n):17 L []18 for x in range(m, n 1):19 ifisprime(x):20 L.append(x)21 returnL222324 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)313233 L primes(100)34 print(L)3536 #结果37 #False38 #None39 #True40 #True41 #False42 #[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)456 func(L)7 print(L)8 #L的值是10910 #栗子211 D {}12 deffunc2(x):13 x[name] xiaoming141516 func2(D)17 print(D) #{name: xiaoming}View Code区别 不可变的类型的数据作为函数参数传入时函数内部不会改变变量的源数据值是安全的可变类型的数据作为函数参数传入时函数内部可变原数据多用来返回更多数据结果1 L []2 def func(x[]):3 whileTrue:4 names input(请输入学生姓名)5 if notnames:6 break7 x.append(names)8 returnx91011 #方式112 r func()13 print(r)14 #方式215 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)345 f1 fn6 f1() #等同于调用函数fn()View Code1 def fn_outer(): #外部函数2 print(外部函数被调用)3 deffn_inner():4 print(fn_inner被调用)5 fn_inner()6 fn_inner()7 print(外部函数被调用结束)8910 fn_outer()11 12 外部函数被调用13 fn_inner被调用14 fn_inner被调用15 外部函数被调用结束View Code函数作为函数的返回值1 deffunc1():23 deffunc2():4 print(hello)5 returnfunc267 fn func1()8 fn() #返回helloView 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 returnmymul101112 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:319 请输入操作方式20 Please enter the second number:421 Result: 722 Please enter the first number:323 请输入操作方式*24 Please enter the second number:625 Result: 18View Code函数作为函数的参数传递1 deftab(x, y):2 return | x.center(12) | y.center(12) |34 defstring(x, y):5 return names : x ages : y67 defmyprint(fn, x, y):8 s fn(x, y)9 print(s)101112 myprint(tab, xiaoming, 18)13 myprint(string, xiaoli, 19)14 #结果15 #| xiaoming | 18 |16 #names : xiaoliages : 19View Code1 defgoodbye(L):2 for x inL:3 print(886:, x)45 defhello(L1):6 for x inL1:7 print(hello:, x)89 defoperator(fn, M):10 fn(M)111213 operator(goodbye, (xiaoming,xiaoli))14 operator(goodbye, [xiaoming,xiaoli])15 operator(hello, (xiaoming,xiaoli))16 operator(hello, [xiaoming,xiaoli])17 #结果18 #886: xiaoming19 #886: xiaoli20 #886: xiaoming21 #886: xiaoli22 #hello: xiaoming23 #hello: xiaoli24 #hello: xiaoming25 #hello: xiaoliView Code全局变量和局部变量局部变量 定义在函数内部的变量(包含函数参数)全局变量 定义在函数外部模块内部的变量1 v 100 #此为全局变量23 deffn():4 v 200 #此为局部变量5 print(v)678 fn()9 print(v)10 #结果11 #20012 #100View Codepython作用域作用域 也叫名字空间是变量访问的时候查找变量名的范围空间python 四个作用域局部作用域(函数内) Local L外部嵌套函数作用域 Encloseing function locals E函数定义所在模块(文件)的作用域 Global (module) Gpython内置模块的作用域 Builtin(Python) B变量名的查找规则在访问变量时 先查找本地变量 然后是包裹此函数的外部函数的函数内部的变量 之后是全局变量最后是内置变量。字母顺序 L -- E --- G --- B1 v 10023 deffun1():4 v 2005 print(fun1_v :, v)6 deffun2():7 v 3008 print(fun2_v :,v)9 fun2()1011 fun1()12 print(v, v)13 #结果14 #fun1_v : 20015 #fun2_v : 30016 #v 100View Code在默认情况下变量名赋值会创建或修改本地变量1 v 1002 deffn():3 v 200456 fn()7 print(v) #100View Codeglobal 语句作用告诉解释器global语句声明的一个或多个变量这些变量的作用域为模块级的作用域也称作全局变量对全局声明(global)的变量赋值将映射到模块文件的内部作用域语法 global 变量名1 变量名2 ....1 v 1002 deffn():3 global v #声明全局变量4 v 200567 fn()8 print(v) #200View Codeglobal 说明1 全局变量如果要在函数内部被赋值 则必须经过全局声明否则 被认为是局部变量2 全局变量在函数内部不经过声明就可以直接访问(前提是变量已经存在)3 不能先声明局部变量再用global声明为全局变量此做法不符合语法规则4 global 变量列表里的变量名不能出现在此作用域的参数列表里for 循环控制目标类定义函数定义及import导入名字中1 deffn2():2 v 2003 global v #错的45 fn2()6 print(3:,v)View Code1 #4global 变量列表里的变量名不能出现在此作用域的参数列表里23 deffn3(v):4 globalv5 v 300678 fn3(11)9 print(v) #SyntaxError: name v is parameter and globalView Codenonlocal语句作用 告诉解释器nonlocal声明的变量不是局部变量也不是全局变量而是外部嵌套函数内的变量。语法 nonlocal 变量名1 变量名 .....nonlocal在嵌套函数里的内层函数里加载说明1nonlocal 语句只能在被嵌套函数的内层使用2 访问nonlocal 变量 将对外部嵌套函数 的作用域内的变量进行操作3 嵌套函数有两层或者两层以上时访问nonlocal 变量只对最近一层的变量操作4nonlocal语句的变量列表里的变量名不能出现在此作用域的参数列表里1 vars 10023 defoutter():4 var 2005 definner():6 nonlocal var #指定var为外层嵌套函数作用域7 var 1 #么有nonlocal var此行会出错UnboundLocalError: local variable var referenced before assignment8 print(inner_var:,var)9 inner()10 print(outter_var:,var)111213 outter()14 15 #inner_var: 20116 #outter_var: 201View Code1 #shuoming32 deff1():3 v 1004 deff2():5 v 2006 deff3():7 nonlocal v8 v 4009 print(f3:,v)10 f3()11 print(f2:,v)12 f2()13 print(f1:,v)14 f1()15 16 40017 40018 100View Code