Python语法基础刻意练习:Task05(函数与Lambda表达式)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_45138411/article/details/102760253

函数

函数是带名字的代码块,用于完成具体的工作。要执行函数定义的特殊任务,可调用该函数。需要在程序中多次执行同一项任务时,你无需反复编写完成该任务的代码,而只需调用执行该任务的函数,让python运行其中的代码。

定义函数

# -*- coding: GBK -*-
def greet_user():             (1)
	"""显示简单的问候语"""     (2)
	print('hello!')           (3)
greet_user()                  (4)

这是一个打印问候语的简单函数。(1)处的代码行使用关键字def定义了一个名为greet_user的函数,它不需要任何信息就能完成工作,因此括号是空的,定义以冒号结尾。紧跟在定义后的缩进行构成了函数体:(2)处的文本是被称为文档字符串的注释,描述了函数是做什么的,文档字符串用三引号括起。代码行(3)是函数体内的唯一一行代码。(4)处实现调用该函数,要调用函数,可依次指定函数名以及括号引起的必要信息,由于这个函数不需要任何信息,只需输入greet_user()即可。

向函数传递信息

# -*- coding: GBK -*-
def greet_user(username):
	"""显示简单的问候语"""
	print('hello,'+username.title())
greet_user("jesse")       #hello,Jesse

将上面的程序稍作修改。就可以让函数不仅打印hello,还可以指定用户的名字。为此,可在括号内添加username,并在调用函数时指定一个值,将此值传递给username。同样,将值指定为其他名字时,得到相同的结果。

# -*- coding: GBK -*-
def greet_user(username):
	"""显示简单的问候语"""
	print('hello,'+username.title())
greet_user("sarah")       #hello,Sarah

在这两个例子中,函数greet_user的定义中username是一个形参(函数完成其工作所需的一项信息),在代码greet_user(“jesse”)中,值"jesse"是一个实参 ,实参是调用函数时传递给函数的信息。在上面,我们将实参"jesse"传递给函数greet_user(),这个值被存储在形参username中。

传递实参
鉴于函数定义中可能包含多个形参,因此函数调用中也可能包含多个实参。向函数传递实参的方式很多,可使用位置实参,这要求实参的顺序与形参相同;也可以使用关键字实参,其中每个变量都由变量名和值组成;还可使用列表和字典。

位置实参
你调用函数时,python必须将函数调用中的每个实参都关联到函数定义中的一个形参,为此,最简单的关联方式是基于实参的顺序。这种关联方式被称为位置实参。

# -*- coding: GBK -*-
def describe_pet(animal_type,pet_name):
	"""显示宠物信息"""
	print("\nI have a "+animal_type+".")
	print("My "+animal_type+"'s name is "+pet_name.title()+'.')
describe_pet("hamster","harry")          
#I have a hamster.
#My hamster's name is Harry.

还可调用函数多次

# -*- coding: GBK -*-
def describe_pet(animal_type,pet_name):
	"""显示宠物信息"""
	print("\nI have a "+animal_type+".")
	print("My "+animal_type+"'s name is "+pet_name.title()+'.')
describe_pet("hamster","harry") 
describe_pet("dog","willie")         
#I have a hamster.
#My hamster's name is Harry.

#I have a dog.
#My dog's name is Willie.

使用位置实参调用函数时,实参的顺序很重要如果实参的顺序不正确,可能会出现这样的情况:

# -*- coding: GBK -*-
def describe_pet(animal_type,pet_name):
	"""显示宠物信息"""
	print("\nI have a "+animal_type+".")
	print("My "+animal_type+"'s name is "+pet_name.title()+'.')
describe_pet("harry","hamster") 
       
#I have a harry.
#My harry's name is Hamster.

这就不是我们想要的结果了。

关键字实参
关键字实参是传递给函数的名称——值对。你直接在实参中将名称和值关联起来了,因此向函数传递实参时不会混淆(不会得到名为Hamster的harry)。

# -*- coding: GBK -*-
def describe_pet(animal_type,pet_name):
	"""显示宠物信息"""
	print("\nI have a "+animal_type+".")
	print("My "+animal_type+"'s name is "+pet_name.title()+'.')
describe_pet(animal_type="hamster",pet_name="harry") 
       
#I have a hamaster.
#My hamaster's name is Harry.

默认值
编写函数时,可给每个形参指定默认值。在调用函数中给形参提供了实参时,python将使用指定的实参值;否则,将使用形参的默认值。

# -*- coding: GBK -*-
def describe_pet(pet_name,animal_type='dog'):
	"""显示宠物信息"""
	print("\nI have a "+animal_type+".")
	print("My "+animal_type+"'s name is "+pet_name.title()+'.')
describe_pet(animal_type="hamster",pet_name="harry") 
describe_pet(pet_name="willie")        
#I have a hamaster.
#My hamaster's name is Harry.

#I have a dog.
#My dog's name is Willie.

请注意,在这个函数定义中,修改了形参的排列顺序。由于给animal_type指定了默认值,无需通过实参来指定动物类型,因此函数调用中只包含一个实参——宠物的名字。然而,python仍将这个实参视为位置实参,因此如果函数调用中只包含宠物的名字,这个实参将关联到函数定义中的第一个形参。这就是需要将pet_name放在形参列表开头的原因。

返回值

# -*- coding: GBK -*-
def describe_pet(pet_name):
	"""显示宠物信息"""
	return pet_name.title()
print(describe_pet("harry"))  #Harry

传递任意数量的实参

# -*- coding: GBK -*-
def make_pizza(*toppings):
	"""打印顾客点的所有配料"""
	print(toppings)
make_pizza('pepperoni')                               #('pepperoni')
make_pizza('pepperoni','mushroom','green peppers')    #('pepperoni','mushroom','green peppers')

猜你喜欢

转载自blog.csdn.net/weixin_45138411/article/details/102760253