1 關(guān)鍵字參數(shù)
所用情形:參數(shù)太多時,防止參數(shù)順序?qū)瘮?shù)的影響,傳參時指定參數(shù)對應(yīng)的形參名
>>> def test(brand,slogan):
? ? ? ? ? ? ? ?print(brand + "'s slogan is " + slogan)
>>>test(brand='Nike',slogan='just do it')
Nike's slogan is just do it
2 默認(rèn)參數(shù)
>>> def test(brand='Nike',slogan='just do it'):
print(brand + "'s slogan is " + slogan)
>>> test()
Nike's slogan is just do it
3 可變參數(shù) 參數(shù)前加 *
>>> def test(*params):
print('參數(shù)的長度是:',len(params) )
>>> test(1,2,3,4) ? #實現(xiàn)形式參數(shù)打包成元組
參數(shù)的長度是: 4