Python基础学习9——函数

基本概念

函数是一种能够完成某项任务的封装工具。在数学中,函数是自变量到因变量的一种映射,通过某种方式能够使自变量的值变成因变量的值。其实本质上也是实现了某种值的转换的任务。

函数的定义

在python中,函数是利用def来进行定义:

def Lego_pricediscount():
	"""用来计算打折以后的lego价格"""
	print("You are making a day dream")
  • 第一行def Lego_pricediscount():是用来定义函数名和传递参数的。对于本例来说,函数名就是Lego_pricediscount,见名知意。括号内可以用来传递参数,本例中的函数无需参数,但是很多情况下函数是需要得到一些输入才能运行的,后面会介绍

  • 第二行"""用来计算打折以后的lego价格"""是注释,用以表述函数的功能。这一部分不是必须要写的内容,但是如果写了可以大大提高的函数可读性

  • 第三行print("You are making a day dream")是函数的主体,是函数功能实现的具体代码

  • 如果函数有返回值,末尾还需要加上return XXX。本例为无返回值函数,后面会介绍

函数的调用

函数的调用比较简单,如果调用上面例子中的函数,直接:

Lego_pricediscount()

如果函数有参数,那么调用的时候应该相应的在括号内添上参数

参数

基本概念

实参:实际参数,具有具体值的参数

Add(100,200) # 100,200是实参

形参:形式参数,仅代表一个参数形式不代表具体值的参数

def Add(a,b) # a,b是形参

实参的传递

位置实参

对于位置实参而言,函数调用的参数顺序要和函数定义的参数顺序一致,否则会发生错误。

def Lego_pricediscount(price, discount):
	"""用来计算打折以后的lego价格"""
	print(f"The price is {price*discount}.")

正确的调用方式:

lego_price = 398
lego_discount = 0.88
Lego_pricediscount(lego_price, lego_discount)

错误的调用方式:

lego_price = 398
lego_discount = 0.88
Lego_pricediscount(lego_discount, lego_price)

虽然这样做也能正确的运行,甚至对于本题来说结果一样(因为加法有交换率),但是参数的意义就不同了。对于这种错误的方式,乐高的价格变成了0.88,乐高的折扣变成了398,这是没有意义的。

关键字实参

对于关键字实参而言,函数调用的参数顺序要和函数定义的参数顺序可以不一致,因为关键字实参将名称与参数联系在了一起

def Lego_pricediscount(price, discount):
	"""用来计算打折以后的lego价格"""
	print(f"The price is {price*discount}.")
    
lego_price = 398
lego_discount = 0.88
Lego_pricediscount(discount='lego_discount', price='lego_price')
Lego_pricediscount(price='lego_price', discount='lego_discount')

上图中无论哪种调用方式都是正确的,因为discount='lego_discount', price='lego_price'这种写法相当于告诉了原函数哪一个参数对应的是哪个,不需要再考虑顺序。

默认实参

很多时候我们需要跟函数指定一下参数的默认值,因为我们有的时候传递的参数并不全,而对于那些没有传入值的参数,如果我们不指定默认值的话很有可能会发生报错或者无法正常运行。

def Lego_pricediscount(price, discount='1.0'):
	"""用来计算打折以后的lego价格"""
	print(f"The price is {price*discount}.")
    
lego_price = 398
lego_discount = 0.88
Lego_pricediscount(discount=lego_discount,price=lego_price)
Lego_pricediscount( price=lego_price)

输出结果

The price is 350.24.
The price is 398.
可选实参

可选实参指的是可以选择输入的参数

def Lego_pricediscount(price, discount=''):
	"""用来计算打折以后的lego价格"""
    if discount:
		print(f"The price is {price*discount}.")
        return price*discount
    else:
    	print(f"The price is {price}.")
        return price
    
lego_price = 398
lego_discount = 0.88
Lego_pricediscount(discount=lego_discount,price=lego_price)
Lego_pricediscount(price=lego_price)

输出结果

The price is 350.24.
The price is 398.

这里的discount没有输入也没有默认值,也可以正常输出结果

不限数量实参

有些时候我们不知道函数需要接收多少个实参,这时候我们定义函数时就需要不限数量的实参都能传入。

def Lego_pricediscount(*Lego):
    for lego in Lego:
        print(f"I want to buy {lego}.")

Lego_pricediscount("LegoCity", )
Lego_pricediscount("LegoCity", "LegoMarvel")
Lego_pricediscount("LegoCity", "LegoMarvel", "LegoHouse")

输出结果

I want to buy LegoCity.
I want to buy LegoCity.
I want to buy LegoMarvel.
I want to buy LegoCity.
I want to buy LegoMarvel.
I want to buy LegoHouse.

*Lego本质上是创建了一个空元组。

其实,**Lego在python中也有对应的意义,相当于创建一个空字典。

**Lego作为函数参数,可以传入键值对

返回值

有的函数可以返回一些生成结果等内容,我们称其为返回值,我们可以用变量去接受这些返回值,相当于一个赋值的过程。返回值不仅仅局限于数值和字符串,也可以返回列表、字典等。

def Lego_pricediscount(price, discount='1.0'):
	"""用来计算打折以后的lego价格"""
	# print(f"The price is {price*discount}.")
    return price*discount
    
lego_price = 398
lego_discount = 0.88
p=Lego_pricediscount(discount=lego_discount,price=lego_price)
print(p)

输出结果

350.24

函数与列表

函数中传入的参数可以是一个列表,我们可以利用函数对列表进行一系列的处理。

def Lego_pricediscount(Lego):
    for lego in Lego:
		print(f"I want to buy {lego}.")
        
Lego = ["LegoCity", "LegoMarvel", "LegoHouse"]
Lego_pricediscount(Lego)

输出结果

I want to buy LegoCity.
I want to buy LegoMarvel.
I want to buy LegoHouse.

我们还可以在函数中修改列表等。这里需要注意的是,如果我们不希望对原列表进行修改但是能照常完成输出,我们需要用到切片:

def Lego_pricediscount(Lego):
    for lego in Lego[:]:
        lego = lego + '_lego'
        print(f"I want to buy {lego}.")

Lego = ["LegoCity", "LegoMarvel", "LegoHouse"]
Lego_pricediscount(Lego[:])
print(Lego)

输出结果

I want to buy LegoCity_lego.
I want to buy LegoMarvel_lego.
I want to buy LegoHouse_lego.
['LegoCity', 'LegoMarvel', 'LegoHouse']

我们会发现原列表并没有被改变。这里的切片实际上对列表的副本进行操作

函数模块化

在项目工程量比较大的时候,我们常常会将函数单独放在一个文件夹。多个函数组成一个模块。这样我们只要导入这个模块,就可以任意使用模块中的相关函数。具体的导入格式如下:

导入整个模块:
# import 模块名
import Lego

我们还可以给模块名重命名,这样对于比较长的模块名我们就可以简化操作

import Lego as lg

之后在本python文件中我们可以直接用lg来代替Lego模块

导入特定函数:
# from 模块名 import 函数名
from Lego import Lego_Price
# from Lego import * 导入模块所有函数,不过不建议使用这种方法

类似的我们也可以重命名函数

from Lego import Lego_Price as lp
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值