- 函数简单的定义:
01 def greet_user(username): 02 """显示简单的问候语""" 03 print("Hello, " + username.title() + "!") 04 greet_user('jesse') >>> Hello, Jesse! |
- 函数中的两种传递函数的方式:
- 位置实参
01 def describe_pet(animal_type, pet_name): 02 """显示宠物的信息""" 03 print("\nI have a " + animal_type + ".") 04 print("My " + animal_type + "'s name is " + pet_name.title() + ".") 05 describe_pet('hamster', 'harry') 06 describe_pet('dog', 'willie') >>> I have a hamster. My hamster's name is Harry.
I have a dog. My dog's name is Willie. |
- 关键词实参
注意:使用关键字实参时, 务必准确地指定函数定义中的形参名。
01 def describe_pet(animal_type, pet_name): 02 """显示宠物的信息""" 03 print("\nI have a " + animal_type + ".") 04 print("My " + animal_type + "'s name is " + pet_name.title() + ".") 05 describe_pet(animal_type='hamster', pet_name='harry') 06 describe_pet(pet_name='harry', animal_type='hamster') >>> I have a hamster. My hamster's name is Harry.
I have a hamster. My hamster's name is Harry. |
- 函数中的默认值
01 def describe_pet(pet_name, animal_type='dog'): 02 """显示宠物的信息""" 03 print("\nI have a " + animal_type + ".") 04 print("My " + animal_type + "'s name is " + pet_name.title() + ".") 05 describe_pet('willie'); 06 describe_pet('harry','hamster') >>> I have a dog. My dog's name is Willie.
I have a hamster. My hamster's name is Harry. |
- 函数中的返回值,并且实参可选:可以根据情况,选择性的输入输出。
01 def get_formatted_name(first_name, last_name, middle_name=''): 02 """返回整洁的姓名""" 03 if middle_name: 04 full_name = first_name + ' ' + middle_name + ' ' + last_name 05 else: 06 full_name = first_name + ' ' + last_name 07 return full_name.title() 08 musician = get_formatted_name('jimi', 'hendrix') 09 print(musician) 10 musician = get_formatted_name('john', 'hooker', 'lee') 11 print(musician) >>> Jimi Hendrix John Lee Hooker |
- 字典的返回:
01 def build_person(first_name, last_name, age=''): 02 """返回一个字典, 其中包含有关一个人的信息""" 03 person = { 'first': first_name, 'last': last_name} 04 if age: 05 person['age'] = age 06 return person 07 musician = build_person('jimi', 'hendrix', age=27) 08 print(musician) >>> {'first': 'jimi', 'last': 'hendrix', 'age': 27} |
- 列表的传递
01 def print_models(unprinted_designs, completed_models): 02 """ 03 模拟打印每个设计, 直到没有未打印的设计为止 04 打印每个设计后, 都将其移到列表completed_models中 05 """ 06 while unprinted_designs: 07 current_design = unprinted_designs.pop() 08 # 模拟根据设计制作3D打印模型的过程 09 print("Printing model: " + current_design) 10 completed_models.append(current_design) 11 def show_completed_models(completed_models): 12 """显示打印好的所有模型""" 13 print("\nThe following models have been printed:") 14 for completed_model in completed_models: 15 print(completed_model) 16 unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron'] 17 completed_models = [] 18 print_models(unprinted_designs, completed_models) 19 show_completed_models(completed_models)
>>> Printing model: dodecahedron Printing model: robot pendant Printing model: iphone case
The following models have been printed: dodecahedron robot pendant iphone case |
- 禁止函数修改列表:利用:利用切片创建列表副本
01 def show_magicians(magicians_names): 02 for name in magicians_names: 03 print(name); 04 05 def make_great(input_names): 06 num=len(input_names); 07 i=0; 08 for name in input_names: 09 input_names[i]='the Great '+name; 10 i+=1; 11 show_magicians(input_names); 12 return input_names 13 14 magicians_list=['Joan Jett','Gun & Roses','Bonjovi']; 15 show_magicians(magicians_list);# 未改变前列表 16 print(''); 17 new_list=make_great(magicians_list[:]);#改变后 18 print(new_list); >>> Joan Jett Gun & Roses Bonjovi
the Great Joan Jett the Great Gun & Roses the Great Bonjovi ['the Great Joan Jett', 'the Great Gun & Roses', 'the Great Bonjovi'] |
- 传递任意数量的实参(位置实参与任意数量实参的联用)
01 def make_pizza(size, *toppings): 02 """概述要制作的比萨""" 03 print("\nMaking a " + str(size) + 04 "-inch pizza with the following toppings:") 05 for topping in toppings: 06 print("- " + topping) 07 make_pizza(16, 'pepperoni') 08 make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese') >>> Making a 16-inch pizza with the following toppings: - pepperoni
Making a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese |
- 使用任意数量的关键字实参
目的:将函数编写成能够接受任意数量的键—值对——调用语句提供了多少就接受多少。
01 def build_profile(first, last, **user_info): 02 """创建一个字典, 其中包含我们知道的有关用户的一切""" 03 profile = {} 04 profile['first_name'] = first 05 profile['last_name'] = last 06 for key, value in user_info.items(): 07 profile[key] = value 08 return profile 09 user_profile = build_profile('albert', 'einstein', 10 location='princeton', 11 field='physics') 12 print(user_profile) >>> {'first_name': 'albert', 'last_name': 'einstein', 'location': 'princeton', 'field': 'physics'} |
- 函数存储在模块中:
最为有效的方法:https://blog.csdn.net/eriol/article/details/83955142
- 使用as给函数和模块指定别名
01 # 使用as给函数指定别名 02 from pizza import make_pizza as mp 03 mp(16, 'pepperoni') 04 mp(12, 'mushrooms', 'green peppers', 'extra cheese') 05 # 使用as给模块指定别名 06 import pizza as p 07 p.make_pizza(16, 'pepperoni') 08 p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese') >>> Making a 16-inch pizza with the following toppings: - pepperoni
Making a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese
Making a 16-inch pizza with the following toppings: - pepperoni
Making a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese |
- 导入模块中的所有函数
import 语句中的星号让Python将模块pizza 中的每个函数都复制到这个程序文件中。 (不推荐)
01 from pizza import * 02 make_pizza(16, 'pepperoni') 03 make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese') >>> Making a 16-inch pizza with the following toppings: - pepperoni
Making a 12-inch pizza with the following toppings: - mushrooms - green peppers - extra cheese |