Python 函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python 函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6212字,纯文字阅读大概需要9分钟。
内容图文
![Python 函数](/upload/InfoBanner/zyjiaocheng/1083/4ef29c75ee46483ea26c7ec788c01833.jpg)
函数
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。
函数能提高应用的模块性,和代码的重复利用率。
函数需要先定义再使用
函数类别
Python提供了许多 内置函数,比如print(),len()....
# 内置函数:内置到解释器中 >>> len <built-in function len> >>> print <built-in function print>
但你也可以自己创建函数,这被叫做 用户自定义函数。
定义函数的格式:
1 ‘‘‘ 2 def 函数名(arg1,arg2,arg3): 3 ‘描述信息‘ 4 函数体 5 return 6 ‘‘‘
1 def foo(): 2 print(‘from the foo‘) 3 4 5def bar(x,y): 6print(‘from bar‘) 7 res=x+y 8return res 91011# foo() #函数调用的语句形式12# res=bar(1,2) #函数调用的表达式形式13# res1=bar(1,2)*10 #函数调用的表达式形式14# print(res1)151617 res2=bar(bar(1,2),3) #函数的调用作为另外一个函数的参数1819print(res2)
- 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 ()
- 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数。
- 函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。
- 函数内容以冒号起始,并且缩进。
- return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的return相当于返回 None。
![技术分享](/upload/getfiles/default/2022/11/9/20221109064356273.jpg)
![技术分享](/img/jian.gif)
1 # 用函数输出以下内容: 2 ‘‘‘ 3 ############# 4 落霞与孤鹜齐飞 5 ############# 6 ‘‘‘ 7 8 def print_line(): 9 print(‘#‘*13) 1011def print_msg(): 12print(‘落霞与孤鹜齐飞‘) 1314def print_line(): 15print(‘#‘*13) 1617print_line() 18print_msg() 19 print_line()
函数的参数
A.
1.形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。因此,形参只在函数内部有效。函数调用结束返回主调用函数后则不能再使用该形参变量
def foo(x,y): print (x) print (y) foo(1,2) # foo(y=2,x=1) #只是与上面的 输入方式不同,结果同
2.实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。因此应预先用赋值,输入等办法使参数获得确定值
3.位置参数和关键字(标准调用:实参与形参位置一一对应;关键字调用:位置无需固定)
1 # ------在实参的角度------ 2 3 def foo(x,y): 4 print (x) 5 print (y) 6 7 # 第一种:按照位置传值 8 foo(1,2) 9 foo(2,1) 1011# 12# 第二种:按照关键字传值13 foo(x=1,y=2) 14 foo(y=2,x=1) 1516# 第三种:混着用17 foo(1,y=2) 18# 问题一:按位置传值必须在按关键字传值的前面19 foo(y=2,1) #报错20# 问题一:对于一个形参只能赋值一次21 foo(1,y=2)
4.默认参数
1 # 默认参数:变化比较小的,经常使用。不用传值(非传也行) 2 3 def foo(x,y=1): # y=1 为 默认参数 4print(x) 5print(y) 6 7 foo(1) 8 9#结果10 1 11 1
默认参数必须注意的问题是:默认参数必须放到位置参数的后面
5.参数组
B. 有参函数与无参函数的角度
无参函数:通常 不需有 返回值。它内部通常只是些语句
有参函数:通常要 有 返回值
1 def foo(): #无参函数 2print(‘from the foo‘) 3 4def bar(x,y): #有参函数 5print(‘from bar‘) 6 res = x+y 7return res #相当于 return 3 8 9 bar(1,2) 10 n = bar(1,2) #n=3,此处的 n,相当于 res所代表的 311print(n)
*args 与 **kwargs的使用(其实是 *与** 的使用)
1.*args
1 # 例一 2 # *args 3 def foo(x,*args): 4print(x) 5print(args) 6 7 foo(1,2,3,4,5) 8 9#输出结果10 1 11 (2, 3, 4, 5) 121314##例二: *args与位置参数和默认参数的混用15def foo(x,*args,y=1): 16print(x) 17print(args) 18print(y) 1920 foo(1,2,3,4,5,y=333333) 2122# #输出结果23 1 24 (2, 3, 4, 5) 25 333333
2.**kwargs
1 # **kwargs 2 def foo(x,*args,**kwargs): 3print(x) 4print(args) 5print(kwargs) 6 7 foo(1,y=1,z=2) 8 9#输出结果10 1 11 () #元组形式12 {‘y‘: 1, ‘z‘: 2}
return语句
return [表达式] 语句用于退出函数,选择性地向调用方返回一个表达式。
1.没有 return
1 def foo(): 2 print(‘from the foo‘) 34 res = foo() 5print(res) 67#返回值8from the foo 9 None #None,也是返回值
2.有 return ,返回一个值
1 # 有return:返回一个值 2 def my_max(x,y): 3 res = x if x > y else y 4return res 5 6 res1 = my_max(1,2) 7print(res1) 8 910#返回结果11 2
2.有 return ,返回多个值
1 # 有return:返多个值 2 3 def bar(x,y): 4 return 333,666,111,[11,22],{‘a‘:3} #可返回多个值,任意类型的值(会以 元组形式出现) 5 6 res2 = bar(1,2) 7print(res2) 8 9#返回结果1011 (333, 666, 111, [11, 22], {‘a‘: 3}) #元组
嵌套函数和作用域
1.嵌套定义
1 # ####例一 2 3 x = 33333333333333 4def f1(): 5 x = 1 6print(‘------>f1 ‘,x) 7def f2(): 8 x=2 9print(‘---->f2 ‘,x) 10def f3(): 11 x=3 12print(‘-->f3 ‘,x) 13 f3() 14 f2() 15f1() 1617#运行结果1819 ------>f1 1 20 ---->f2 2 21 -->f3 3 222324252627######## 例二2829 x = 33333333333333 30def f1(): 31#x = 1 #此表达式 选择 不执行32print(‘------>f1 ‘,x) 33def f2(): 34#x=2 #此表达式 选择 不执行35print(‘---->f2 ‘,x) 36def f3(): 37#x=3 #此表达式 选择 不执行38print(‘-->f3 ‘,x) 39 f3() 40 f2() 41f1() 4243#运行结果44 ------>f1 33333333333333 45 ---->f2 33333333333333 46 -->f3 33333333333333
2,嵌套调用
1 def my_max(x,y): 2 res=x if x > y else y 3return res 4print(my_max(10,100)) 6 7 8def my_max4(a,b,c,d): 9 res1 = my_max(a,b) 10 res2 = my_max(res1,c) 11 res3 = my_max(res2,d) 12return res3 13print(my_max4(1,20,3,4)) 141516#输出的结果17 100 #print(my_max(10,100))的结果18 20 #print(my_max4(1,20,3,4))的结果
作业:
1.对 haproxy.conf文件进行,增删改查
![技术分享](/upload/getfiles/default/2022/11/9/20221109064356273.jpg)
![技术分享](/img/jian.gif)
global
log 127.0.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
defaults
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull
listen stats :8888
stats enable
stats uri /admin
stats auth admin:1234
frontend oldboy.org
bind 0.0.0.0:80
option httplog
option httpclose
option forwardfor
log global
acl www hdr_reg(host) -i www.oldboy.org
use_backend www.oldboy.org if www
backend www.oldboy1.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
data=input(): {"backend":"www.oldboy1.org","record":{server:...,weight:...,maxconn:...
作业内容
![技术分享](/upload/getfiles/default/2022/11/9/20221109064356273.jpg)
![技术分享](/img/jian.gif)
1 def check(backend): 2 ‘‘‘ 核查功能 ‘‘‘ 3 flag = False 4 check_list = [] 5 with open(‘haproxy.conf‘) as f: 6for line in f: 7if line.startswith(‘backend‘): 8if backend == line.strip().split()[1]: 9 flag = True 10continue11if flag and line,strip().startswith(‘backend‘): 12break13if flag and line.strip(): 14 check_list.append(line.strip()) 15return check_list 161718def fun_edit(user_input,tag = False): 19‘‘‘本函数完成的功能是修改功能‘‘‘20 with open(filename,encoding="utf-8") as f_source,open("haproxy.dst",mode="w+",encoding="utf-8") as f_dst: 21for line in f_source: 22if line.startswith("backend") and user_input in line: 23 tag = True 24continue25if tag and line.startswith("backend"): 26break27if tag: 28 list_search.append(line.strip()) 29 f_source.seek(0) 30 tag = False 31while True: 32for i,l in enumerate(list_search,1): 33print(i,l) 34 choice = input("请输入需要修改内容的编号:") 35if choice.isdigit(): 36 choice = int(choice)-1 37if choice < len(list_search) and choice >=0: 38 old_record = list_search[choice].strip() 39print("您要修改的内容为:%s"%old_record) 40 new_record = input("请您输入新的内容:").strip() 41for line in f_source: 42if line.startswith("backend") and user_input in line: 43 tag = True 44 f_dst.write(line) 45continue46if tag and line.strip() == old_record: 47 f_dst.write(" %s\n"%new_record) 48continue49 f_dst.write(line) 50 f_dst.flush() 51else: 52break53else: 54print("您输入的内容有误,请重新输入!!") 55continue56 fun_file_bak(filename)怎山
原文:http://www.cnblogs.com/zhaochangbo/p/6673872.html
内容总结
以上是互联网集市为您收集整理的Python 函数全部内容,希望文章能够帮你解决Python 函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。