参数
我们用函数封装了一个功能,但是希望这个功能可以在不同要求的作用下面得到不同的结果,就需要用到参数。
def f(color):
if color == 'green':
print('They’re green.')
elif color == 'yellow':
print('It\'s yellow')
else:
print('other')
>>> f('green')
'They’re green.'
>>> f('yellow')
"It's yellow."
>>> f('red')
'other'
默认参数
参数的初始值。
def f(color = 'green'): # 默认不传参的情况下 color=green
if color == 'green':
print('They’re green.')
elif color == 'yellow':
print('It\'s yellow.')
else:
print('other')
>>> f()
'They’re green.'
>>> f('yel