1 定义函数
1.1 向函数传递信息(在所定义的函数括号中,添加变量,使之为函数传递信息)
example 1
输出如下
语言
Hello,Jerry
1.2 实参和形参
变量username是一个形参-----完成其工作所需的一项信息
变量Jerry是一个实参-----调用函数时传递给函数的信息
2 传递实参
2.1 位置实参
将函数调用的每个实参都关联到函数定义的一个形参,关联方式是基于实参的顺序,即位置实参。
2.2 关键词实参
关键词实参是传递给函数的名称-值对。在调用函数时,直接使用关键词指明实参对应的形参。
2.3 默认值
在编写函数时,可给每个形参指定默认值。
example 2
python
def describe_pet(pet_name,animal_type='dog'):
print("\nI have a"+animal_type+".")
print("My" +animal_type+"'s name is "+pet_name.title()+".")
describle_pet(pet_name="willie")
调用这个函数时,如果没有给animal_type指定值,Python将把这个形参设置为"dog"
而如果给animal-type提供了实参,因此python将忽略这个形参的默认值。
3 返回值
3.1 返回简单值
函数并非总是直接显示输出,可使用return语句将返回到调用函数的代码行。
example 3
python
def get_formatted_name(first_name,last_name):
full_name=first_name+' '+last_name
return full_name.title()
musician = get_formatted_name('jimi','hendrix')
print(musician)
在调用函数时,需要提供一个变量(full_name),用于存储返回的值。
3.2 让实参变成可选的
提供额外信息,使得使用默认值让实参变成可选的。
3.3 返回字典
将接收到的形参封装到字典中去,将其返回。
3.4 结合函数和while 循环
python
def get_formatted_name(first_name,last_name):
full_name=first_name+' '+last_name
return full_name.title()
while True:
print("\nPlease tell me your name:")
print("(enter 'q' at any time to quit)")
f_name=input("First_time: ")
if f_name=='q':
break
l_name=input("Last_time: ")
if l_name=='q':
break
formatted_name=get_formatted_name(f_name,l_name)
print("\nHello,"+formatted_name+"!")
4 传递列表
向函数传递列表很有用,这种列表包含的是可能是名字,数字或复杂的对象(如字典)。将列表传递给函数后,函数就能直接访问其内容。
4.1 在函数中修改列表
将列表传递给函数后,函数就能对其进行修改。在函数中对这个列表所做的任何修改都是永久性的,这让你能够高效地处理大量的数据。
python
unprinted_designs=['iphone case','robot pendent','dodecahedron']
complecated_models=[]
while unprinted_designs:
current_design=unprinted_designs.pop()
print("Printing model:"+current_design)
completed_models.append(current_design)
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
重新组织代码(使用函数)
python
def print_models(unprinted_designs,completed_models):
while unprinted_design:
current_design =unprinted_designs.pop()
print("Printing model:"+current_design)
completed_models.append(current_design)
def show_completed_models(completed_models):
print("\nThe following models have been printed:")
for completed_model in completed_models:
print(completed_model)
unprinted_designs=['iphone case','robot pendent','dodecahedron']
complecated_models=[]
print_models(unprinted_models,completed_models)
show-completed_models(completed_models)
5 传递任意数量的实参
5.1 结合使用位置参数和任意数量的数量参数
5.2 使用任意数量的关键词实参
可将函数编成成能够接受任意数量的键-值对————调用语句提供了多少就接受多少。
python
def build_profile(first,last,**user_info):
profile={}
profile['first_name']=first
profile['last_name']=last
for key,value in user_info.items():
profile[key]=value
return profile
user_profile=build_profile('albert','einstein',lcocation='princeton',field='physics')
print(user_profile)
形参**user_info中的两个星号让python创建一个名为user_info的空字典,并将收到的所有键-值对都封装到这个字典中。
6 将函数存储在模块中(使用import导入模块)
6.1 导入整个模块
import module_name.py的整个模块
6.2 导入特定的函数
from module_name import function_name
6.3 使用as给函数指定别名
from module_name import function_name as fn
6.4 使用as给模块指定别名
import module_name as mn
6.5 导入模块中的所有函数
import module_name import *