大蟒日志5

十、函数2

  1. 默认参数值
  2. 关键参数值
  3. 可变参数VarArgs
# 2018.3.31
# 1.Default Argument Value
# Note that the default argument value should be a constant.
def say(message, times=1):
    print(message * times)
say('Hello ')
say('Kitty ', 5)
say(-10)
say(5, 3)
# 注意: 参数列表从后向前default, 不可从前往后

# 2.Keyword Argument
def func(a, b=5, c=10):
    print('a is', a, '\nb is', b, '\nc is', c)

func(21,-3.3)
func(35,c=24)
func(c=1, a=2)
'''
输出结果:
a is 21 
b is -3.3 
c is 10
a is 35 
b is 5 
c is 24
a is 2 
b is 5 
c is 1
'''
# 按参数列表中的顺序依次取值
# 若有默认参数值设置, 除默认参数值外, 其他仍然按列表顺序


# 2018.4.1
# 3.VarArgs parameter (VarArgs = Variable number of Arguments)
def total(a=5, *numbers, **phonebook):
    print('a', a)

    for single_item in numbers:
        print('single_item', single_item)

    for first_part, second_part in phonebook.items():
        print(first_part, second_part)

print(total(10,1,2,3, Jack=1123, John=2231, Inge=1560))
'''
显示结果:
a 10
single_item 1
single_item 2
single_item 3
Jack 1123
John 2231
Inge 1560
None
①All the positional arguments from that point till the 
end are collected as a tuple(数组) called 'param' 
②**param all the keyword argument from that point till 
the end are collected as a dictionary called 'param'
③tuple and dictionary, learn them in later chapters
'''
#牵扯到元组和字典,一时理解得不够透彻,后续更新……

【声明】本博文为学习笔记,含有个人修改成分,并非完全依照《a byte of python》,仅供参考。若发现有错误的地方,请不吝赐教,谢谢。同时,我也会不定期回顾,及时纠正。#

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值