位置参数
文中的'geroge'和'peppa'就是位置参数
第四行和第六行代码是根据顺序传递参数
第八行代码指定了参数的值,就不用去理会参数的位置
1 def toy(geroge,peppa): 2 print('乔治喜欢:'+geroge,end = ' ') 3 print('佩奇喜欢:'+peppa) 4 toy('恐龙','泰迪') 5 #>>乔治喜欢:恐龙 佩奇喜欢:泰迪 6 toy('泰迪','恐龙') 7 #>>乔治喜欢:泰迪 佩奇喜欢:恐龙 8 toy(peppa = '泰迪',geroge = '恐龙') 9 #>>乔治喜欢:恐龙 佩奇喜欢:泰迪
默认参数
如果一个参数的值是相对固定的,那么就可以设定一个默认参数。当调用函数时,默认参数可填可不填。
要注意的是:默认参数必须放在位置参数之后
def toy(geroge,peppa,both_like='踩泥坑'): print('乔治喜欢:'+geroge,end = ' ') print('佩奇喜欢:'+peppa,end = ' ') print('他们都喜欢:'+both_like) toy('恐龙','泰迪') #>>乔治喜欢:恐龙 佩奇喜欢:泰迪 他们都喜欢:踩泥坑 toy('恐龙','泰迪','吃蛋糕') #>>乔治喜欢:恐龙 佩奇喜欢:泰迪 他们都喜欢:吃蛋糕
常见的print函数中的end参数就是一个默认参数,默认值为\n。关于end参数的介绍,可以看https://www.cnblogs.com/gancai/p/11914615.html