《Python编程从入门到实践》_第八章_函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Python编程从入门到实践》_第八章_函数相关的知识,希望对你有一定的参考价值。
一个简单的函数
先看一个简单的函数
def say_hello(): ‘‘‘打印hello‘‘‘ print("Hello!") say_hello() #运行结果 Hello!
def为函数的关键字,say_hello为你定义的函数的名称,还可能在括号内指出函数为完成其任务需要什么样的信息,即便括号是空的,也是必不可少的,最后以冒号结尾。
向函数传递信息
def say_hello(name): ‘‘‘打印hello‘‘‘ print("Hello! " + name) say_hello(‘Frank‘) #运行结果 Hello! Frank
这里在括号里面加了一个变量name,然后将‘Frank‘传递给了name,输出。
实参和形参
在函数say_hello的定义中,变量name是一个形参——函数完成其工作所需的一项信息。在代码say_hello(‘Frank‘)中,其‘Frank‘就是一个实参,实参是调用函数时传递给函数的信息。
传递实参
位置实参
你调用函数时,Python必须将函数调用中的每个实参都关联到函数定义的一个形参。为此,最简单的关联方式是基于实参的顺序,这种关联方式被称为位置实参,可以多次调用。
def introduce(name,age): ‘‘‘自我介绍‘‘‘ print("Hello! my name is " + name +" !" + "I‘am " + age + " years old !") introduce(‘Frank‘,‘18‘) #运行结果 Hello! my name is Frank !I‘am 18 years old !
关键字实参
可以直接将形参和实参关联起来,这样就不必要在意顺序了。
def introduce(name,age): ‘‘‘自我介绍‘‘‘ print("Hello! my name is " + name +" !" + "I‘am " + age + " years old !") introduce(age=‘18‘,name=‘Candy‘) #运行结果 Hello! my name is Candy !I‘am 18 years old !
默认值
可以给函数的形参指定一个默认值,当你给形参重新指定值的时候,会覆盖默认值。
def introduce(name,age=‘20‘): ‘‘‘自我介绍‘‘‘ print("Hello! my name is " + name +" !" + "I‘am " + age + " years old !") introduce(name=‘Candy‘) introduce(age=‘23‘,name=‘Frank‘) #运行结果 Hello! my name is Candy !I‘am 20 years old ! Hello! my name is Frank !I‘am 23 years old !
返回简单值
函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值。函数返回的值被称为返回值。
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) #运行结果 Jimi Hendrix
让实参变为可选的
有的时候,我们不需要每个形参都有实参,但是没有实参会导致输出出错,可以看一下下面的例子:
def get_formatted_name(first_name,last_name,middle_name=‘‘): ‘‘‘返回整洁的姓名‘‘‘ if middle_name: full_name = first_name + ‘ ‘ + middle_name + ‘ ‘ + last_name else: full_name = first_name + ‘ ‘ + last_name return full_name.title() musician = get_formatted_name(‘jimi‘,‘hendrix‘) print(musician) musician = get_formatted_name(‘jimi‘,‘hendrix‘,‘dork‘) print(musician) #运行结果 Jimi Hendrix Jimi Dork Hendrix
看上面的例子,并非所有的人都中间名,所有我们在指定实参的时候,middle_name不一定要有,这里我们用‘‘空字符串来指定middle_name的默认值(空字符串为False),再用if判断用那种full_name的格式输出。
返回字典
def build_person(first_name,last_name,age=‘‘): ‘‘‘返回一个值,其中包含一个人的信息‘‘‘ person = {"first":first_name,"last":last_name} if age: person[‘age‘] = age return(person) musician = build_person(‘jimi‘,‘hendrix‘,‘27‘) print(musician) #运行结果 {‘first‘: ‘jimi‘, ‘last‘: ‘hendrix‘, ‘age‘: ‘27‘}
函数可返回任何类型的值,包括列表和字典等较复杂的数据结构。
结合使用while循环和函数
def get_formatted_name(first_name,last_name): ‘‘‘返回整洁的姓名‘‘‘ full_name = first_name + " " + last_name return full_name.title() while True: f_name = input("Please input your first name:") l_name = input("Please input your last name:") name = get_formatted_name(f_name,l_name) print("hello! " + name) #运行结果 Please input your first name:bin Please input your last name:liu hello! Bin Liu Please input your first name:
函数括号里面也可以是被赋值的变量名。
传递列表
可以将一个列表传递给形参
def greet_users(names): ‘‘‘遍历列表,打招呼‘‘‘ for name in names: print("Hello, " + name + ‘!‘) usernames = ["Frank","May","Caroline"] greet_users(usernames) #运行结果 Hello, Frank! Hello, May! Hello, Caroline!
在函数中修改列表
def print_models(upprint,completed): ‘‘‘弹出已打印的给完成的列表,并打印‘‘‘ while upprint: current = upprint.pop() print("print:",current) completed.append(current) def show_models(completed): ‘‘‘显示已打印的‘‘‘ print("The following models have been print:") for c in completed: print(c) upprint = ["apple","book","shirt"] completed = [] print_models(upprint,completed) show_models(completed) #运行结果 print: shirt print: book print: apple The following models have been print: shirt book apple
在这个例子中,函数执行结束后,upprint列表就空了,为了解决这个问题,可向函数传递列表的副本而不是原件,这样函数所做的任何修改都只影响副本,而不会影响原件。
def print_models(upprint[:],completed):
传递任意数量的实参
def make_pizza(*toppings): ‘‘‘概述要制作的pizza‘‘‘ print("\nMaking a pizza with the following toppings:") for topping in toppings: print("--",topping) make_pizza(‘pepperoni‘) make_pizza(‘mushrooms‘,‘grenn peppers‘,‘extra cheese‘) #运行结果 Making a pizza with the following toppings: -- pepperoni Making a pizza with the following toppings: -- mushrooms -- grenn peppers -- extra cheese
形参名*toppings的星号让Python创建一个名为toppings的空元祖,并将收到的所有值都封装想到这个元祖中。
结合使用位置实参和任意数量实参
如果要让函数接受不同类型的实参,必须在函数定义中将接纳任意数量实参的形参放在最后。Python会先匹配实参和关键字实参,再将余下的实参都收集到最后一个形参中。
def make_pizza(size,*toppings): ‘‘‘概述要制作的pizza‘‘‘ print("\nMaking a " + str(size) + "-inch pizza with the following toppings:") for topping in toppings: print("--",topping) make_pizza(16,‘pepperoni‘) make_pizza(12,‘mushrooms‘,‘grenn peppers‘,‘extra cheese‘) #运行结果 Making a 16-inch pizza with the following toppings: -- pepperoni Making a 12-inch pizza with the following toppings: -- mushrooms -- grenn peppers -- extra cheese
使用任意数量的关键字实参
def build_profile(first_name,last_name,**info): ‘‘‘创建一个字典,其中包含我们知道的有关用户的一切‘‘‘ profile={} profile["first_name"]=first_name profile["last_name"]=last_name for k,v in info.items(): profile[k]=v return profile user_profile = build_profile("bin","liu",location="princeton",field="physics") print(user_profile) #运行结果 {‘first_name‘: ‘bin‘, ‘last_name‘: ‘liu‘, ‘location‘: ‘princeton‘, ‘field‘: ‘physics‘}
将函数存储在模块中
#pizza打印方式模块 def make_pizza1(size,*toppings): ‘‘‘概述要制作的pizza‘‘‘ print("\n Making a " + str(size) + "-inch pizza with the following toppings:") for topping in toppings: print("----" + topping) def make_pizza2(size,*toppings): ‘‘‘概述要制作的pizza‘‘‘ print("\nsize:",size) print("toppings:") for topping in toppings: print("====",topping)
在同一目录下创建:pizza.py
#pizza import make_pizza make_pizza.make_pizza1(16,‘pepperoni‘,‘mushrooms‘) make_pizza.make_pizza2(16,‘pepperoni‘,‘mushrooms‘)
运行结果:
Making a 16-inch pizza with the following toppings: ----pepperoni ----mushrooms size: 16 toppings: ==== pepperoni ==== mushrooms
导入特定的函数
#pizza打印方式模块 文件make_pizza.py def make_pizza1(size,*toppings): ‘‘‘概述要制作的pizza‘‘‘ print("\n Making a " + str(size) + "-inch pizza with the following toppings:") for topping in toppings: print("----" + topping) def make_pizza2(size,*toppings): ‘‘‘概述要制作的pizza‘‘‘ print("\nsize:",size) print("toppings:") for topping in toppings: print("====",topping) #pizza 文件pizza.py from make_pizza import make_pizza1,make_pizza2 make_pizza1(16,‘pepperoni‘,‘mushrooms‘) make_pizza2(16,‘pepperoni‘,‘mushrooms‘) #运行结果 Making a 16-inch pizza with the following toppings: ----pepperoni ----mushrooms size: 16 toppings: ==== pepperoni ==== mushrooms
使用as给函数定义别名
#pizza from make_pizza import make_pizza1 as pz1,make_pizza2 as pz2 pz1(16,‘pepperoni‘,‘mushrooms‘) pz2(16,‘pepperoni‘,‘mushrooms‘) #运行结果 Making a 16-inch pizza with the following toppings: ----pepperoni ----mushrooms size: 16 toppings: ==== pepperoni ==== mushrooms
语法:from module_name import function_name as fn
使用as给模块定义别名
#pizza import make_pizza as pz pz.make_pizza1(16,‘pepperoni‘,‘mushrooms‘) pz.make_pizza2(16,‘pepperoni‘,‘mushrooms‘) #运行结果 Making a 16-inch pizza with the following toppings: ----pepperoni ----mushrooms size: 16 toppings: ==== pepperoni ==== mushrooms
语法:import module_name as mn
导入模块中所有的函数
#pizza from make_pizza import * make_pizza1(16,‘pepperoni‘,‘mushrooms‘) make_pizza2(16,‘pepperoni‘,‘mushrooms‘) #运行结果 Making a 16-inch pizza with the following toppings: ----pepperoni ----mushrooms size: 16 toppings: ==== pepperoni ==== mushrooms
*可以代表前面指定模块里的所有函数。
以上是关于《Python编程从入门到实践》_第八章_函数的主要内容,如果未能解决你的问题,请参考以下文章