---def test(x):   # def:定义函数的关键字,test:函数名, x相当于以前函数中的自变量
使用函数的好处:
1.代码重用
2.保持一致性,易于维护
3.可扩展性
def test(x):   # def:定义函数的关键字,test:函数名, x相当于以前函数中的自变量
'''
2*x+1
:param x:整形数字
:return: 返回计算结果
'''
y=2*x+1
return y def test():
'''
2*x+1
:param x:整形数字
:return: 返回计算结果
'''
x=3
y=2*x+1
return y
a=test()
print(a)
---过程:就是没有返回值的函数
总结:
# 返回值数=0,返回None
# 返回值数=1,返回object(对象原本是什么,返回什么)
# 返回值数>1,返回tuple
def test01():
msg = 'test01'
print(msg) def test02():
msg = 'test02'
print(msg)
return msg def test03():
msg = 'test03'
print(msg)
return 1,2,3,4,'a',['alex'],{'name':'alex'},None def test04():
msg = 'test03'
print(msg)
return {'name':'alex'}
t1=test01()
t2=test02()
t3=test03()
t4=test04()
print(t1)
print(t2)
print(t3)
print(t4)
# test01
# test02
# test03
# test03
# None
# test02
# (1, 2, 3, 4, 'a', ['alex'], {'name': 'alex'}, None)
# {'name': 'alex'}
---函数参数
# 1.形参变量只有在被调用时才分配内存单元,调用结束时,即刻释放所分配的内存单元,(相当于门牌号)
# 因此,形参只在函数内部有效,函数,函数调用结束返回主调用函数后则不能再使用该形参变量
# 2.实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,他们都必须有确定的值,(基本数据类型)
# 以便把这些值传送给形参,因此应预先用赋值,输入的方法使参数获得确定值
def calc(x,y): #x=2,y=3
res=x**y
return x
return y
res=calc(2,3) # 实参:常量
# print(x)
# print(y)
print(res)
# a=10
# b=10
# calc(a,b) # 实参:变量 def test(x,y,z):#x=1,y=2,z=3
print(x)
print(y)
print(z)
#3.位置参数,必须一一对应,缺一不行多一也不行
# test(1,2,3) #4.关键字参数,无须一一对应,缺一不行多一也不行
# test(y=1,x=3,z=4) #位置参数必须在关键字参数左边
# test(1,y=2,3)#报错
# test(1,3,y=2)#报错
# test(1,3,z=2)
# test(1,3,z=2,y=4)#报错
# test(z=2,1,3)#报错
# type='mysql' 默认参数
# def handle(x,type='mysql'):
# print(x)
# print(type)
# handle('hello')
# handle('hello',type='sqlite')
# handle('hello','sqlite') # def install(func1=False,func2=True,func3=True):
# pass
#参数组:**字典 *列表
def test(x,*args):
print(x)
print(args) test(1)
#
# ()
test(1,2,3,4,5)
#
# (2, 3, 4, 5)
test(1,{'name':'alex'})
#
# ({'name': 'alex'},)
test(1,['x','y','z'])
#
# (['x', 'y', 'z'],)
test(1,*['x','y','z'])
#
# ('x', 'y', 'z')
test(1,*('x','y','z'))
#
# ('x', 'y', 'z') # def test(x,**kwargs):
# print(x)
# print(kwargs)
# test(1,y=2,z=3)
# test(1,1,2,2,2,2,2,y=2,z=3)
# test(1,y=2,z=3,z=3)#会报错 :一个参数不能传两个值 def test(x,*args,**kwargs):
print(x)
print(args,args[-1])
print(kwargs,kwargs.get('y'))
# test(1,1,2,1,1,11,1,x=1,y=2,z=3) #报错
test(1,1,2,1,1,11,1,y=2,z=3)
#
# (1, 2, 1, 1, 11, 1) 1
# {'y': 2, 'z': 3} 2 # test(1,*[1,2,3],**{'y':1})

最新文章

  1. JavaScript 框架设计
  2. SharePoint 2013 User Profile Services之跨场设置
  3. nodejs 使用Google浏览器进行可视化调试——Node Inspector工具
  4. js取整函数
  5. SPSS数据分析—协方差分析
  6. 熟悉 Objective-C
  7. MySQL之重设密码(忘记密码)讲解
  8. java中数据流的简单介绍
  9. java多线程12设计模式
  10. tcpdump的源码分析
  11. iframe 自适应内容高度
  12. 我的Java设计模式-观察者模式
  13. java IO流全面总结
  14. Ubuntu下创建新用户后,不能使用管理员用户下安装的Anaconda
  15. Android SDK 环境搭建
  16. VMware虚拟机中CentOS设置固定IP
  17. 各大浏览器相继发布声明将停止支持 TLS 1.0 和 TLS 1.1 !
  18. Dual-voltage regulator meets USB-power needs
  19. 由「Metaspace容量不足触发CMS GC」从而引发的思考
  20. 看过自会理解, Photon Server 常见概念分析.

热门文章

  1. selenium3与Python3实战 web自动化测试框架 ☝☝☝
  2. Linux之常用命令I
  3. VMware15.5版本下安装Windows_Server_2008_R2
  4. angular之跨域
  5. 渗透测试=基于白名单执行payload--Ftp
  6. luoguP1006 传纸条
  7. [网络流 24 题] luoguP4016 负载平衡问题
  8. Math中ceil中为什么会有负零
  9. pycharm中常见错误提示
  10. insert into select 引起的 "子查询返回的值不止一个。当子查询跟随在**之后,或子查询用作表达式时,这种情况是不允许的"