defpet(name, aniaml_type ='dog'):"""寵物的信息"""print("\nMy "+ aniaml_type +"'s name is "+ name +".")
pet('jack')
pet(name='jack')
pet('harry','hamster')
pet(aniaml_type='hamster', name='harry')
pet(name='harry', aniaml_type='hamster')
#運行結果
My dog's name is jack.
My dog's name is jack.
My hamster's name is harry.
My hamster's name is harry.
My hamster's name is harry.
返回值
defperson(name, age =''):"""人的信息,返回一個字典"""
person ={'name': name}if age:
person['age']= age
return person
musician = person('jimi')
player = person('lucy', age=21)print(musician)print(player)
#形參名*toppings創建一個空元組,將所有值都裝到這個元組裏defmake_pizza(size,*toppings):"""概述要製作的披薩"""print("\n Making a "+str(size)+"-inch pizza with the following toppings:")for topping in toppings:print("- "+ topping)
make_pizza(12,'mushrooms','green peppers','extra cheese')
#運行結果
Making a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese
#形參名*toppings創建一個空元組,將所有值都裝到這個元組裏defbuild_profile(first,last,**user_info):"""創建一個字典,包含用戶的各種信息"""
profile ={}
profile['first_name']= first
profile['last_name']= last
for key, value in user_info.items():
profile[key]=value
return profile
user_profile = build_profile('albert','einstein',
location='princeton',
field='physics')print(user_profile)