python 函数

#过程:就是没有返回值的函数
def test01():
    msg = 'hello The little green frog'
    print(msg)

def test02():
    msg = 'hello WuDaLang'
    print(msg)
    return msg

def test03():
    msg = 'hello WuDaLang'
    print(msg)
    return msg, (1,2,3), {"name":"ly"}

t1 = test01()
t2 = test02()
t3 = test03()
print(t1)
print(t2)
print(t3)  #实际上只返回一个值,就一个元组

"""
总结:
返回值数=0;返回None
返回值数=1;返回object
返回值数>1;返回tuple
"""

def calc(x,y):    # 形参
    res = x**y
    return res
c = calc(2,3)    #实参
print(c)
a = 10
b = 10
d= calc(a,b)
print(d)

def test(x,y,z):
    print(x)
    print(y)
    print(z)

test(1,2,3)     #位置参数
test(x=1,y=3,z=2)       #关键字参数
test(1,3,z=2)       #位置参数必须在关键字参数左边

def handle(x,type='mysql'):
    print(x)
    print(type)

handle('hello')
handle('hello',type='sqllite')
handle('hello','sqllite')

#参数组**字典  *列表
def test(x,*args):
    print(x)
    print(args)
    print(args[0])

test(1,2,3,4,5,6)
test(1,{'name':'alex'})
test(1,[x,y,z])
test(1,*[x,y,z])      #遍历一遍

def test(x,**kwargs):
    print(x)
    print(kwargs)

test(1,y=2,z=3)   # 字典

# 一起使用,*args必须在前面,不能违背大原则
def test(x,*args,**kwargs):
    print(x)
    print(args)
    print(kwargs)

test(1,2,k=2)

def test(x,*args,**kwargs):
    print(x)
    print(args,args[-1])
    print(kwargs,kwargs.get('y'))  # 字典获取元素

test(1,1,2,1,1,11,x=1,y=2,z=3) # 报错
test(1,1,1,2,4,11,y=2,z=3)
test(1,*[1,2,3],**{'y':1})

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值