1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| def greet_user(): """显示简单的问候语""" print("Hello!") greet_user()
def greet_user(username): """显示简单的问候语""" print(f"Hello, {username.title()}!") greet_user('jerry')
def describe_pet(animal_type, pet_name): """显示宠物信息""" print(f"\nI have a {animal_type}.") print(f"My {animal_type}'s name is {pet_name.title()}.") describe_pet('hamster', 'harry') describe_pet('dog', 'willie')
def describe_pet(animal_type, pet_name): """显示宠物信息""" print(f"\nI have a {animal_type}.") print(f"My {animal_type}'s name is {pet_name.title()}.") describe_pet(animal_type='hamster', pet_name='harry') describe_pet(pet_name='harry', animal_type='hamster')
def describe_pet(pet_name, animal_type="dog"): """显示宠物信息""" print(f"\nI have a {animal_type}.") print(f"My {animal_type}'s name is {pet_name.title()}.") describe_pet(pet_name='harry') describe_pet(pet_name='harry', animal_type='hamster')
def get_formatted_name(first_name, last_name): """返回整洁的姓名""" full_name = f"{first_name} {last_name}" return full_name.title() musician = get_formatted_name('jimi', 'hendrix') print(musician)
def get_formatted_name(first_name, last_name, middle_name=''): """返回整洁的姓名""" if middle_name: full_name = f"{first_name} {middle_name} {last_name}" else: full_name = f"{first_name} {last_name}" return full_name.title() musician = get_formatted_name('jimi', 'hendrix') print(musician) musician = get_formatted_name('john', 'hooker', 'lee') print(musician)
def build_person(first_name, last_name, age=None): """返回一个字典,其中包含有关一个人的信息""" person = {'first': first_name, 'last': last_name} if age: person['age'] = age return person musician_01 = build_person('jimi', 'hendrix', age=24) musician_02 = build_person('jerry', 'hendrix') print(musician_01) print(musician_02)
def greet_users(names): """向列表中的每位用户发出简单的问候""" for name in names: msg = f"Hello, {name.title()}!" print(msg) usernames = ['hannah', 'ty', 'margot'] greet_users(usernames)
def input_msg(): return input("input msg: ") def print_msg(): print(input_msg()) print_msg()
function_name(list_name[:])
def make_pizza(size, *toppings): """概述要制作的比萨""" print(f"\nMaking a {size}-inch pizza with the following toppings: ") print(topping) make_pizza(16, 'pepperoni') make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
def build_profile(first, last, **user_info): """创建一个字典,其中包含我们知道的有关用户的一切""" user_info['first_name'] = first user_info['last_name'] = last return user_info user_profile = build_profile('albert', 'einstein', location='princeton', field='physics') print(user_profile)
|