栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python - 基础函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python - 基础函数

文章目录
    • 1 创建和调用 - def
    • 2 函数的返回值 - return
    • 3 函数的参数
      • 3.1 位置参数
      • 3.2 关键字参数
      • 3.3 默认参数
    • 4 收集参数 - ‘*’
      • 4.1 收集参数除可将元素打包为元祖外,还可将其打包为字典 - ‘**’
      • 4.2 解包
    • 5 变量
      • 5.1 作用域(局部变量)
      • 5.2 全局作用域(全局变量)
      • 5.3 函数内修改全局变量 - global
    • 6 嵌套函数
      • 6.1 内部函数修改外部函数 - nonlocal语句
      • 6.2 LEGB规则

1 创建和调用 - def
>>> def test():         #def用来定义函数,函数的名称为test
	    pass            #pass为函数体(代码块),每次调用test函数,都会执行代码块
//举例
>>> def test():
	    for i in range(3):
		    print("python")

>>> test()
python
python
python

//函数的个性化定制
>>> def test(name):
	    for i in range(3):
		    print(f"hello {name}")

>>> test("zhangsan")
hello zhangsan
hello zhangsan
hello zhangsan

>>> def test(name,count):           #此处的name,count为形参
	    for i in range(count):
		    print(f"hello {name}")

		
>>> test("wangwu", 5)               #此处的wangwu,5为实参,对应了上面的形参
hello wangwu
hello wangwu
hello wangwu
hello wangwu
hello wangwu
2 函数的返回值 - return
>>> def div(x,y):
	    z = x / y
	    return z

>>> div(4,2)
2.0

>>> def div(x,y):
	    return x / y        #可以省略z = x / y

>>> div(4,2)
2.0

以除法为例,因为除数不能为0,因此可对函数做限制,方法如下

>>> def div(x,y):
	    if y == 0:
		    return "除数不能为0"
	    else:
		    return x / y

	
>>> div(4,2)
2.0
>>> div(2,0)
'除数不能为0'

return语句一旦执行会立刻返回结果。不会在执行剩余的代码。
如果一段代码中没有return语句,那么会默认返回一个None。

>>> def div(x,y):               #return执行一次
	    if y == 0:
		    return "除数不能为0"
	    return x / y
	    print(range(3))

>>> div(4,0)
'除数不能为0'


>>> def div():                  #没有return返回None
    	pass

>>> print(div())
None
3 函数的参数 3.1 位置参数
>>> def test(s, vt, o):         #在定义参数的时候已经将参数的位置确认,此类位置固定的参数就为位数参数
	    return "".join((o,vt,s))

>>> test("A", "B", "C")
'CBA'
>>> test("C", "B", "A")
'ABC'
3.2 关键字参数
>>> test(o="A", vt="C", s="O")
'ACO'
3.3 默认参数
>>> def test(s, vt, o="test"):      #当设定参数的默认值时,如果参数没有被赋值将使用默认参数
	    return "".join((o,vt,s))

>>> test("A", "B")
'testBA'

//指定的默认值一定要在形参之后。
4 收集参数 - ‘*’
>>> def test(*args):
    	print(type(args))
    	print(args)
    	print("有{}个参数".format(len(args)))
    	print("第二个参数是:{}".format(args[1]))

>>> test("A", "B", "C", "D")

('A', 'B', 'C', 'D')
有4个参数
第二个参数是:B
>>> 

‘*’就像打包的操作,将后面的元素打包到一个元祖中。
如果参数中除了*args外还有其他参数A、B,那么在实参赋值时需要对A、B用关键字的方式指定,或用默认参数对A、B进行赋值。
4.1 收集参数除可将元素打包为元祖外,还可将其打包为字典 - ‘**’
>>> def kw(**test):
    	print(test)

>>> kw(a=1, b=2, c=3)
{'a': 1, 'b': 2, 'c': 3}

把他们结合到一起可以这么写

>>> def test(a, *b, **c):
    	print(a, b, c)

>>> test(1, 2, 3, 4, c=5, d=6, e=7)
1 (2, 3, 4) {'c': 5, 'd': 6, 'e': 7}
4.2 解包
//元祖方式
>>> test = (1,2,3,4)
>>> def youren(a, b, c, d):
    	print(a, b, c, d)

>>> youren(*test)       #需要加‘*’符号
1 2 3 4

//字典键值对方式
>>> test = {'a':1, 'b':2, 'c':3, 'd':4}
>>> youren(**test)
1 2 3 4
5 变量 5.1 作用域(局部变量)
>>> def test():
    	x = 200
    	print(x)

>>> test()      #x在test中定义,所以他的作用仅限于test函数
200
5.2 全局作用域(全局变量)
>>> x = 500
>>> def test():
    	print(x)

>>> x
500

而局部变量会覆盖同名的全局变量

>>> x = 500
>>> def test():
    	x = 200
    	print(x)

>>> test()
200
>>> x
500
5.3 函数内修改全局变量 - global
>>> x = 500
>>> def test():
    	global x
    	x = 200
    	print(x)

>>> test()
200
>>> x
200
6 嵌套函数
>>> def testA():
    	x = 200
    	def testB():
    		x = 500
    		print("testB, x=", x)
    	testB()                     #无法直接在外面调用嵌套函数,需要添加此行调用
    	print("testA, x=", x)

>>> testA()
testB, x= 500
testA, x= 200
6.1 内部函数修改外部函数 - nonlocal语句
>>> def testA():
    	x = 200
    	def testB():
    		nonlocal x
    		x = 500
    		print("testB, x=", x)
    	testB()
    	print("testA, x=", x)

>>> testA()
testB, x= 500
testA, x= 500
6.2 LEGB规则
L = local       #局部作用域
E = Enclosed    #嵌套函数的外层函数作用域
G = global      #全局作用域
B = Build-In    #内置作用域
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1005416.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号