7.2 函数的参数

1、给 b 变量设定一个默认的值

如果实参传入的时候,指定了 b 的值,那 b 优先选择传入的实参,当 b 没有值时,才会用默认值

1
2
3
4
5
def funcA(a,b=0):
    print(a)
    print(b)
funcA(1)        # b 变量选择默认实参
funcA(10,20)    # b 变量选择传入实参

返回结果:

1

0

10

20


2、参数为 tuple

1
2
3
4
5
6
def funT(a,b,*c):
    print(a)
    print(b)
    print("length of c is : %d" % len(c))
    print(c)
funT(1,2,3,4,5,6)

返回结果:

1

2

length of c is : 4

(3, 4, 5, 6)


3、参数为 dictionary

1
2
3
4
5
6
7
def funD(a,**b):
    print(a)
    for in b:
        print(x + ":" + str(b[x]))
funD(100,x="hello",y="你好")
args={"1":"a","2":"b"}           # 定义一个字典
funD(100,**args)

返回结果:

100

x:hello

y:你好

100

1:a

2:b


 本文转自 归来仍少年 51CTO博客,原文链接:http://blog.51cto.com/shaoniana/1980550

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值