1.定义函数,传递实参
def greet_user(username): #定义函数 print("Hello, " + username.title() + "!") greet_user("bigbeat") #传递信息 #传递实参 def describe_pet(animal_type,pet_name="duranter"): #定义函数的是形参,pet_name="duranter:默认值,必须是最后一个形参设置默认值 print("\nI have a " + animal_type +".") print("My " + animal_type + "'s name is " + pet_name.title() + ".") describe_pet("dog","happy") #调用的是实参 describe_pet("cat","hary") describe_pet(animal_type= "rabbit",pet_name= "kury") ** #关键字实参,关键字要与形参对应** describe_pet("ox" ) #因为有默认值 print("\n")
2.#返回值
def get_formatted_name(first_name,last_name,middle_name=""): if middle_name: #非空字符串读作Ture full_name = first_name + " " + middle_name + " " + last_name else: full_name = first_name + " " + last_name return full_name.title() ** #return:将结果返回函数调用行** classmate = get_formatted_name("jia","zheng") print(classmate) classmate = get_formatted_name("jia","zheng","ying") print(classmate) print("\n") #返回字典 def build_person(first_name,last_name,age=""): ** #空字符读作Flase** person = {"first":first_name.title(),"last":last_name.title()} #创建字典 if age: ** #判断是否写入age** person["age"]=age ** #字典添加键-值** return person classmate = build_person("zei","hou") print(classmate) classmate = build_person("zei","hou","22") print(classmate) print("\n") #结合使用函数和while循环 while True: #无限循环要设置跳出条件 print("\nPlese tell me your name or enter 'quit' to exit") first = input("Enter your first name: ") if first == "quit": break #符合跳出 else: last = input("Enter your last name: ") full = get_formatted_name(first,last) print("Hello " + full + "!") print("\n")
3.#传递列表
def greet_users(names): for name in names: ** #遍历列表for** msg = "Hello " + name.title() + "!" print(msg) usernames = ["xiong","jia","zei","an"] greet_users(usernames) ** #调用时注意函数拼写** print("\n") #在函数中修改列表 def print_model(unprint_designs,completed_models): while unprint_designs: ** #非空字符表示Ture** current_design = unprint_designs.pop() #pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 print("Printing model: " + current_design) completed_models.append(current_design) #append()添加列表元素 def show_complete_models(completed_models): print("\nThe following models have been printed: ") for completed_model in completed_models: #for遍历列表 print(completed_model) unprint_designs = ["iphone case","huawei case","robot pendent"] complete_models = [] print_model(unprint_designs,complete_models) show_complete_models(complete_models) print("\n") #禁止函数修改列表 #funtiion_name(list_name[:]) : 将列表副本传递给函数 print_model(unprint_designs[:],complete_models) show_complete_models(complete_models) print(unprint_designs) ** #上面已经清空** print("\n")
4.#传递任意数量的实参 #结合使用位置实参和任意数量实参
def make_pizze(size,toppings): ** #星号创建一个元组()* print("Making a " +str(size) + "-inch pizza with the following topping:") for topping in toppings: ** #遍历元组** print("- " + topping) make_pizze(16,"mushrooms","fish","peg") print("\n") #使用任意数量的关键字实参 def build_profile(first,last,* user_info): ** #:双号创建一个空字典** profile={} profile["first_name"]=first profile["last_name"]=last for key,value in user_info.items(): ** #items():分别复制键-值** profile[key]=value return profile user_profile = build_profile("bigbeat", "wu", location = "school", ** #创建的空列表将收到的所有名称-值对都封装到字典中** field = "learn") print(user_profile) print("\n")
5.#将函数存储在模块中 #将函数存储在独立文件中,可与其他程序员共享这些文件而不是整个程序
-------model_name:make_pizza.py def make_pizze(size,toppings): #星号创建一个元组() print("Making a " +str(size) + "-inch pizza with the following topping:") for topping in toppings: ** #遍历元组* print("- " + topping)
#将函数存储在模块中
import make_pizza #导入:import make_pizza.make_pizze(12,"mushroom","fish","peg") ** #import句式调用:module_name.function_name(),模块名和文件名** make_pizza.make_pizze(13,"vegetable","fruit") from make_pizza import make_pizze #from....import ... : 调用时就不用模块名 make_pizze(44,"fruit","fish") from make_pizza import make_pizze as mp #as:给函数取别名 mp(88,"Durian","fish") import make_pizza as p #as:给模块取别名 p.make_pizze(55,"apple","orange") from make_pizza import * #*:导入模块中的所有函数,最好不要用此方法,因为导入的函数名有可能与自己定义的函数名相同 make_pizze(56,"cucumber","tomato")