函数的构造方法
>>> def searchVowels():
... vowels = set('aeiou')
... word = input("Provide a word to search for vowels: ")
... found = vowels.intersection(set(word))
... for vowel in found:
... print (vowel)
为参数指定默认值
Python函数的任何参数都可以指定一个默认值。如果调用这个函数的代码没有提供其他值,就会自动使用这个默认值。
位置赋值与关键字赋值
位置赋值
>>> searchLetters('galaxy','xyz')
{'y', 'x'}
上面的调用中,两个字符串根据它们的位置赋给phrase和letters参数。这成为位置赋值。
关键字赋值
在Python中,还可以按参数名来引用参数,如果是这样,就不再按照位置顺序来赋值,这称为关键字赋值。
>>> searchLetters(letters = 'xyz', phrase = 'galaxy')
{'y', 'x'}