def describe_pet(animal_type,pet_name): print(f"i have a {animal_type}") print(f"my {animal_type}'s name is {pet_name}.") describe_pet('小黑子','真下头') describe_pet('汪汪队','立大功')
1 2 3 4
i have a 小黑子 my 小黑子's name is 真下头. i have a 汪汪队 my 汪汪队's name is 立大功.
关键字实参
1 2 3 4 5 6 7 8 9 10
from builtins import print
def describe_pet(animal_type, pet_name): print(f"i have a {animal_type}") print(f"my {animal_type}'s name is {pet_name}.")
def name_in(first_name,last_name): person = {'ffirst':first_name,'last':last_name} return person musician = name_in('奥拓','阿波卡利斯') print(musician)
1
{'ffirst': '奥拓', 'last': '阿波卡利斯'}
结合使用函数以及while循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
from builtins import print
def name_in(first_name,last_name): full_name = f"{first_name}{last_name}" return full_name.title() while True: print('Hello everybody') print('please input your first name and last name') f_name = input("Please input your first name:") if f_name == 'quit': break l_name = input("Please input your last name:") if l_name == 'quit': break name_out=name_in(f_name,l_name) print(name_out)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Hello everybody please input your first name and last name Please input your first name:a Please input your last name:a Aa Hello everybody please input your first name and last name Please input your first name:a Please input your last name:_a A_A Hello everybody please input your first name and last name Please input your first name:a Please input your last name:quit
传递列表
1 2 3 4 5 6
def greet_user(names): for name in names: message = f"hello {name.title()}" print(message) username = ['haah','xixi','wangwang'] greet_user(username)
while unprint_Designed: current_design = unprint_Designed.pop() print(f"printing model:{current_design}") print_designeds.append(current_design) print("the following models have been printed:") for print_designed in print_designeds: print(print_designed)
1 2 3 4 5 6 7
printing model:dodecahedron printing model:robot printing model:phone the following models have been printed: dodecahedron robot phone
使用函数来进行编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14
def print_model(unprinted_designeds,completed_models): while unprinted_designeds: current_print = unprinted_designeds.pop() print(f"printing models:{current_print}") completed_models.append(current_print) def show_completed(completed_models): print("the following models have been printed:") for completed_model in completed_models: print(completed_model) unprint_designs = ['phone','computer','robot'] complete_models = []
printing models:robot printing models:computer printing models:phone the following models have been printed: robot computer phone ['phone', 'computer', 'robot']
这个就是在下面调用函数的时候建立一个切片来进行运算,这个切片的话当做中间件就好
传递任意数量的实参
1 2 3 4 5 6 7
def making_pizza(*toppomgs): print("\nmaking a pizza whit the ffollowing topping") for topping in toppomgs: print(f"-{topping}") making_pizza('pepperoni') making_pizza('mushrooms','green peppers','extra cheese')
1 2 3 4 5 6 7 8
making a pizza whit the ffollowing topping -pepperoni
making a pizza whit the ffollowing topping -mushrooms -green peppers -extra cheese
def making_pizza(size,*toppomgs): print(f"making a {size} pizza whit the ffollowing topping") for topping in toppomgs: print(f"-{topping}") making_pizza(12,'pepperoni') making_pizza(16,'mushrooms','green peppers','extra cheese')
1 2 3 4 5 6
making a 12 pizza whit the ffollowing topping -pepperoni making a 16 pizza whit the ffollowing topping -mushrooms -green peppers -extra cheese
使用任意数量的关键字实参
**可以创建一个空字典
1 2 3 4 5 6 7 8
def build_profit(first,last,**user_name): user_name['first_name'] = first user_name['last_name'] = last return user_name user_profile = build_profit('齐格飞','卡斯兰娜', location = '圣芙蕾雅学园', says = '我将发动一次nb的攻击') print(user_profile)