您的位置:首页 > 编程语言 > Python开发

python 笔记 之 函数的使用

2018-04-15 00:00 260 查看
'''
函数的关键字
def  定义函数
return 返回值
pass  滤过
exit(1) 直接退出

函数的参数
*args    tuple参数,对应赋值
**kwargs  dict参数,对应赋值
fun(*args,**kwargs)
fun(1,2,3,4,5,a=10,b=40)

args = (1,2,3,4,5)
kwargs = dict(a=10,b=40)

匿名函数
add = lambda x,y:x+y

'''

# add1 直接打印结果
def add1(x, y):
print(x + y)

# add2将结果返回
def add2(x, y):
return x + y

# hello1 中的pass 为忽略当前行  进入下一行
def hello1():
pass
print("hell1")

# hello2中的exit将退出函数  后面的不在执行
def hello2():
exit(1)
print("hell2")

def test(m, *args, **kwargs):
print("m = {0}".format(m))
print("args = {0}".format(args))     # 一个list列表
print("kwargs = {0}".format(kwargs)) # 一个dict字典

# 函数的调用
test(10, args=(1,11,12))
test(10, (1,11,12))
test(10, 1,11,12)

add1(1, 3)
re = add2(4, 5)
print(re)
hello1()
hello2()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: