向函数传递列表
将列表传递给函数后,函数就能直接访问其内容,这种列表包含的可能是名字,数字或更复杂的对象(如字典)
def greeting_students(names):
for name in names:
print("Welcome to our university " + name.title() + "!")
student_names = ["mengxian", "mengyun", "xinzhu"]
greeting_students(student_names)
# 输出结果为:
Welcome to our university Mengxian!
Welcome to our university Mengyun!
Welcome to our university Xinzhu!
在函数中修改列表
将列表传递给函数后,函数就可对其进行修改,在函数中对这个列表所做的任何修改都是永久性的
def task_models(unfinished_tasks, finished_tasks):
while unfinished_tasks:
current_task = unfinished_tasks.pop()
print("The current task that you are going to finish is: " + current_task)
finished_tasks.append(current_task)
def finished_task_models(finished_tasks):
print("\nThe following tasks have been finished: ")
for finished_task in finished_tasks:
print(finished_task)
unfinished_tasks = ["reading", "exercise", "cooking", "learning_python"]
finished_tasks = []
task_models(unfinished_tasks, finished_tasks)
finished_task_models(finished_tasks)
# 输出结果为:
he current task that you are going to finish is: learning_python
The current task that you are going to finish is: cooking
The current task that you are going to finish is: exercise
The current task that you are going to finish is: reading
The following tasks have been finished:
learning_python
cooking
exercise
reading
向函数传递列表副本
向函数传递列表时,为了不影响原来的列表,可以只向列表传递列表的副本而不是原件,这样函数所做的任何修改都只影响副本,而不会影响原件
切片表示法 [:] 创建列表的副本。如在上面的例子中,如果不想清空unfinished_tasks的列表,可像下面这样调用函数task_models(unfinished_tasks[:], finished_tasks)
传递任意数量的实参
"""形参名*students中的星号,让python创建一个名为students的空元组,并将收到的任何值都封装到这个元组中,不管调用语句时提供了多少实参,这个形参都将它统统收入"""
def registered_students(*students):
print("Here is the list of the registered students: ")
for student in students:
print(student)
"""调用函数时,只提供一个实参"""
registered_students("lee")
"""调用函数时,提供了3个实参"""
registered_students("lee", "yi", "zhao")
//输出结果为:
Here is the list of the registered students:
lee
Here is the list of the registered students:
lee
yi
zhao
结合使用位置实参和任意数量的实参
如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。python先匹配位置实参和关键字实参,再将余下的实参都收集到最后一个形参中。
"""定义了一个函数course,基于以下函数的定义,python将收到的第一个值存储在形参number中,并将其他的所有值都存储在元组students中"""
def course(number, *students):
print("There are " + str(number) + " students have registered this course, here is the list: ")
for student in students:
print(student)
course(1, "lee")
course(3, "lee", "yi", "zhao")
//输出结果为:
There are 1 students have registered this course, here is the list:
lee
There are 3 students have registered this course, here is the list:
lee
yi
zhao
使用任何数量的键-值对实参
有时候,可将函数编写成能够接受任意数量的键-值对,调用语句提供了多少就接受多少
"""定义了一个函数build_profile,基于函数的定义要求提供姓,名,同时允许用户根据需要提供任意数量的名称-值对。形参**student-info 中的两个星号让python创建一个名为student_info的空字典,并将收到的所有名称-值对都封装到这个字典中"""
def build_profile(first, last, **student_info):
profile = dict()
profile["first_name"] = first
profile["last_name"] = last
for key, value in student_info.items():
profile[key] = value
return profile
student_profile = build_profile('xin', 'li', major='ecology', year=2019, province='sichuang')
print(student_profile)
//输出结果为:
{'first_name': 'xin', 'last_name': 'li', 'major': 'ecology', 'year': 2019, 'province': 'sichuang'}