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)