8.2传递实参

位置实参

def describe_pet(animal_type, pet_name):
"""显示宠物信息"""
print("I have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")

describe_pet('hamster', 'harry')
describe_pet('pig', 'mike')


image.png

关键字实参

def describe_pet(animal_type, pet_name):
"""显示宠物信息"""
print("I have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")

describe_pet(animal_type='hamster', pet_name='harry')


image.png

默认值

def describe_pet(pet_name, animal_type='dog'):
"""显示宠物信息"""
print("I have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")

describe_pet('willie')


image.png

T恤

def make_shirt(size, message):
"""显示T恤尺码和字样"""
print("\n\nI'll make a " + size + " t-shirt")
print('It will say,"' + message + '"')

make_shirt('large','I love Python')
make_shirt(message="It's a dog",size="medium")


image.png

大号T恤

def make_shirt(size, message='I love Python'):
"""显示尺码和字样"""
print("\nI'll make a " + size + " t-shirt")
print('It will say,"' + message + '"')

make_shirt('large')
make_shirt('medium')
make_shirt(size='s',message='Just do it')


image.png

城市

def describe_city(name,country='China'):
"""显示城市的名字及所属国家"""
print(name.title() + " is in " + country)

describe_city('yunnan')
describe_city('anhui')
describe_city(name='niuyue',country='America')


image.png

你可能感兴趣的:(8.2传递实参)