【Python】学习Python常用函数作用和用法

1.函数

​ 函数就是把程序进行打包,封装到一个包中,使用时可以直接进行调用

1.创建函数和调用函数:
>>> def test():
	pass

>>> test()
>>> def test():
	for i in range(3):
		print("I love curry")

		
调用函数
>>> test
<function test at 0x000001B617CCF3A0>
>>> test()
I love curry
I love curry
I love curry
2.创建传参函数
>>> def test(times,name):
	for i in range(times):
		print(f"I love {
      
      name}")

		
>>> test(5,"Python")
I love Python
I love Python
I love Python
I love Python
I love Python
3.函数的返回值

​ return:直接返回值,不在理会后面的所有的代码

>>> def test(x,y):
	return x/y

>>> test(4,2)
2.0
>>> def test(x,y):
	if y == 0:
		return "不能为0"
	return x/y

>>> test(10,2)
5.0
4.位置参数

在使用传参函数中有两种关键称呼:

​ 形式参数(形参):在创建函数时,预留的变量名被称为形参

​ 实际参数(实参):在调用函数时,给于的参数被称为实参

>>> def test(a,b,c):
	return "".join((c,b,a))

位置参数传参
>>> test("我","爱","你")
'你爱我'

关键参数传参
>>> test(c="我",b="爱",a="你")
'我爱你'

​ 默认参数:

>>> def test(a,b,c="她"):
	return "".join((c,b,a))

>>> test("我","爱")
'她爱我'
5.冷知识
>>> help(sum)
Help on built-in function sum in module builtins:

sum(iterable, /, start=0)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers
    
    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.

​ 可以看到,使用格式中出现了一个 / 反斜杠,他是代表在它之前是不允许使用关键字参数的,只可以使用位置参数

​ 在函数中也一样,但是我们可以使用*星号来代替,这样第一位可以为位置参数或关键参数,但是后面都要为关键字参数

>>> def test(a,*,b,c):
	return "".join((c,b,a))
>>> test(a="我",b="爱",c="她")
'她爱我'
>>> test("我",b="爱",c="她")
'她爱我'
>>> test("我","爱","她")
Traceback (most recent call last):
  File "<pyshell#181>", line 1, in <module>
    test("我","爱","她")
TypeError: test() takes 1 positional argument but 3 were given
6.多参参数

​ 传入多个参数,是以元组的形式来进行打包的,只需要在变量名前面多加一个*星号即可

​ 第二种理解:传入一个元组类型的变量,将多个值放到一个元组中

>>> def test(*args):
	print(f"一共多少个参数{
      
      len(args)}")
	print(f"第2个参数是什么:{
      
      args[1]}")

	
>>> test(1,2,3,4,5)
一共多少个参数52个参数是什么:2

​ 如果使用了多参赋值,后面的其他变量,必须使用关键字参数来进行赋值了

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

	
>>> test(1,2,3,4,5,a=6,b=7)
(1, 2, 3, 4, 5) 6 7
字典函数

​ 在声明一个函数中,可以同时声明一个字典类型的变量来存储数据

>>> def test(**args):
	print(args)

	
>>> test(a=1,b=2,c=3)
{
    
    'a': 1, 'b': 2, 'c': 3}
>>> def test(a,*b,**args):
	print(a,b,args)

>>> test(1,2,3,4,5,d=1,b=2,c=3)
1 (2, 3, 4, 5) {
    
    'd': 1, 'b': 2, 'c': 3}
解包参数

​ 元组的解包

>>> args=(1,2,3,4)
>>> def test(a,b,c,d):
	print(a,b,c,d)
	
>>> test(*args)
1 2 3 4

​ 字典的解包

>>> args={
    
    'a':1,'b':2,'c':3,'d':4}
>>> test(**args)
1 2 3 4
7.作用域
1.局部作用域
>>> def test():
	x=520
	return x

>>> test()
520
>>> print (x)
Traceback (most recent call last):
  File "<pyshell#266>", line 1, in <module>
    print (x)
NameError: name 'x' is not defined
2.全局作用域
>>> x = 888
>>> def test():
	return x

>>> test()
888

​ 如果在函数的局部变量中与全局变量的名称相同,那么局部变量会代替全局变量,仅限于在当前函数中使用

>>> x = 888
>>> def test():
	return x

>>> test()
888
>>> def test():
	x = 666
	return x

>>> test()
666
3.global语句

​ 使用global来进行声明变量为全局变量

>>> x = 888
>>> def test():
	global x
	x = 520
	return x

>>> x
888
>>> test()
520
>>> x
520
4.嵌套函数
>>> def funA():
	x = 520
	def funB():
		x = 666
		print (f"FunB:{
      
      x}")
	funB()
	return f"funA:{
      
      x}"

>>> funA()
FunB:666
'funA:520'
5.nonlocal语句

作用:可以将变量申明为外部变量

>>> def funA():
	x = 520
	def funB():
		nonlocal x
		x = 666
		print (f"FunB:{
      
      x}")
	funB()
	return f"funA:{
      
      x}"

>>> funA()
FunB:666
'funA:666'
6.LEGB规则

L:local(局部作用域)E:enclosed(嵌套函数的外层函数作用域)G:global(全局作用域)B:build-ln(内置作用yu)

​ 当一个局部变量与全局变量相同时,这个局部变量就会代替全局变量来进行被调用使用

>>> str(123)
'123'
>>> str="123"
>>> str(123)
Traceback (most recent call last):
  File "<pyshell#313>", line 1, in <module>
    str(123)
TypeError: 'str' object is not callable
>>> del str
>>> str(123)
'123'

​ 注意:为了避免这种情况发生,在声明变量时,尽量不要出现与python中的内置函数名冲突
1,2,3,4,5,a=6,b=7)
(1, 2, 3, 4, 5) 6 7


###### 	字典函数

​		在声明一个函数中,可以同时声明一个字典类型的变量来存储数据

```py
>>> def test(**args):
	print(args)

	
>>> test(a=1,b=2,c=3)
{'a': 1, 'b': 2, 'c': 3}
>>> def test(a,*b,**args):
	print(a,b,args)

>>> test(1,2,3,4,5,d=1,b=2,c=3)
1 (2, 3, 4, 5) {
    
    'd': 1, 'b': 2, 'c': 3}
解包参数

​ 元组的解包

>>> args=(1,2,3,4)
>>> def test(a,b,c,d):
	print(a,b,c,d)
	
>>> test(*args)
1 2 3 4

​ 字典的解包

>>> args={
    
    'a':1,'b':2,'c':3,'d':4}
>>> test(**args)
1 2 3 4
7.作用域
1.局部作用域
>>> def test():
	x=520
	return x

>>> test()
520
>>> print (x)
Traceback (most recent call last):
  File "<pyshell#266>", line 1, in <module>
    print (x)
NameError: name 'x' is not defined
2.全局作用域
>>> x = 888
>>> def test():
	return x

>>> test()
888

​ 如果在函数的局部变量中与全局变量的名称相同,那么局部变量会代替全局变量,仅限于在当前函数中使用

>>> x = 888
>>> def test():
	return x

>>> test()
888
>>> def test():
	x = 666
	return x

>>> test()
666
3.global语句

​ 使用global来进行声明变量为全局变量

>>> x = 888
>>> def test():
	global x
	x = 520
	return x

>>> x
888
>>> test()
520
>>> x
520
4.嵌套函数
>>> def funA():
	x = 520
	def funB():
		x = 666
		print (f"FunB:{
      
      x}")
	funB()
	return f"funA:{
      
      x}"

>>> funA()
FunB:666
'funA:520'
5.nonlocal语句

作用:可以将变量申明为外部变量

>>> def funA():
	x = 520
	def funB():
		nonlocal x
		x = 666
		print (f"FunB:{
      
      x}")
	funB()
	return f"funA:{
      
      x}"

>>> funA()
FunB:666
'funA:666'
6.LEGB规则

L:local(局部作用域)E:enclosed(嵌套函数的外层函数作用域)G:global(全局作用域)B:build-ln(内置作用yu)

​ 当一个局部变量与全局变量相同时,这个局部变量就会代替全局变量来进行被调用使用

>>> str(123)
'123'
>>> str="123"
>>> str(123)
Traceback (most recent call last):
  File "<pyshell#313>", line 1, in <module>
    str(123)
TypeError: 'str' object is not callable
>>> del str
>>> str(123)
'123'

​ 注意:为了避免这种情况发生,在声明变量时,尽量不要出现与python中的内置函数名冲突

猜你喜欢

转载自blog.csdn.net/weixin_53678904/article/details/131692614