Python python 函数参数:参数组合

'''在Python中定义函数,可以用必选参数、默认参数、可变参数和关键字参数,
    这4种参数都可以一起使用,或者只用其中某些
    参数定义的顺序必须是:必选参数、默认参数、可变参数和关键字参数
'''
def func(name,sex,city='shanghai',*scores,**hobbies):
    print('name:',name)
    print('sex:',sex)
    print('city:',city)
    sum = 0
    for ele in scores:
        if(type(ele) == int or type(ele)== float):
            sum = sum + ele
    print('scores:',sum)
    print('others:',hobbies)

func('Tom','male','nanjing',98,12,23,sport='basketball',art='movies')
# name: Tom
# sex: male
# city: nanjing
# scores: 133
# others: {'sport': 'basketball', 'art': 'movies'}
func('Tom','male','nanjing',*(98,12,23),sport='basketball',art='movies')

 

posted @ 2019-05-28 13:15  Chris,Cai  阅读(245)  评论(0编辑  收藏  举报