python学习笔记函数与过程(三)

定义函数Function

使用def关键字定义函数,函数不含参数,也可包含参数,定义函数是的参数叫做形参;调用函数时,传入的参数叫实参。可在函数定义时,设置默认值,当调用函数不传值时,参数为默认值;

>>> def saySome(name,words):
	print(name + '->' + words)
>>> saySome('特朗普','武汉病毒')
特朗普->武汉病毒
>>> def saySome(name='sully',words='你好啊'):
	print(name + '->' + words)
>>> saySome()
sully->你好啊
>>> saySome(words = '武汉病毒',name = '特朗普')
特朗普->武汉病毒

不定长参数的函数

>>> def test(*params):
	print('参数的长度是:',len(params))
	print('第二个参数是:',params[1])

	
>>> test(1,'小甲鱼',2,4,6,8)
参数的长度是: 6
第二个参数是: 小甲鱼

函数返回值

python的函数都有返回值,当没有指定返回值时,返回的值为None

>>> def hello():
	print('hello world!')
>>> temp = hello()
hello world!
>>> print(temp)
None
>>> type(temp)
<class 'NoneType'>

python使用list可返回多个值,如下

>>> def back():
	return [1,'sully',3,14]
>>> back()
[1, 'sully', 3, 14]

全局变量和局部变量

局部变量

示例中的final_price为局部变量,在函数外调用会报错

def discounts(price,rate):
    final_price = price * rate
    #可以打印old_price全局变量
    print('这里试图打印全局变量old_price的值',old_price)
    return final_price

old_price = float(input('请输入原件:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('打折后价格是:',new_price)
#会报错,无法打印局部变量
print('final_price的值是:',final_price)
--------------------以上为源码,以下为执行结果----------------------------
执行后报错:
请输入原件:100
请输入折扣率:0.8
这里试图打印全局变量old_price的值 100.0
打折后价格是: 80.0
Traceback (most recent call last):
  File "D:/sources/python/python/xiaojiayu_rumen/01function.py", line 12, in <module>
    print('final_price的值是:',final_price)
NameError: name 'final_price' is not defined

全局变量

全局变量在函数内可以调用,但是在函数内修改全局变量不会改变全局变量的值

def discounts(price,rate):
    final_price = price * rate
    old_price = 50
    print('修改后的old_price的值是:',old_price)
    return final_price

old_price = float(input('请输入原件:'))
rate = float(input('请输入折扣率:'))
new_price = discounts(old_price,rate)
print('修改后的old_price的值是:',old_price)
print('打折后价格是:',new_price)

--------------------以上为源码,以下为执行结果----------------------------
请输入原件:100
请输入折扣率:0.7
修改后的old_price的值是: 50
修改后的old_price的值是: 100.0
打折后价格是: 70.0

使用global关键字可以修改全局变量,示例如下:

>>> count = 5
>>> def myFun():
	count = 10
	print(10)

#先调用myFun,看是否会修改全局变量count
>>> myFun()
10
>>> print(count)
5
>>> def myFun():
	global count
	count = 10
	print(10)

#先调用myFun,看是否会修改全局变量count	
>>> myFun()
10
>>> print(count)
10

 

内嵌函数和闭包

内嵌函数示例如下:

>>> def fun1():
	print('fun1正在被调用。。。')
	def fun2():
		print('fun2正在被调用。。。')
	fun2()

	
>>> fun1()
fun1正在被调用。。。
fun2正在被调用。。。

闭包示例如下:

>>> def funX(x):
	def funY(y):
		return x * y
	return funY

>>> i = funX(8)
>>> i
<function funX.<locals>.funY at 0x0408B150>
>>> type(i)
<class 'function'>
>>> i(5)
40
>>> funX(8)(5)
40
>>> funY(5)
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    funY(5)
NameError: name 'funY' is not defined

说明:不能直接调用内部函数
>>> def fun1():
	x = 5
	def fun2():
		x *=x
		return x
	return fun2()

>>> fun1
<function fun1 at 0x0408B1E0>
>>> fun1()
Traceback (most recent call last):
  File "<pyshell#55>", line 1, in <module>
    fun1()
  File "<pyshell#53>", line 6, in fun1
    return fun2()
  File "<pyshell#53>", line 4, in fun2
    x *=x
UnboundLocalError: local variable 'x' referenced before assignment
>>> def fun1():
	x = 5
	def fun2():
		nonlocal x
		x *=x
		return x
	return fun2()

>>> fun1()
25

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值