1.函數(shù)
? 函數(shù)就是把程序進(jìn)行打包,封裝到一個包中,使用時(shí)可以直接進(jìn)行調(diào)用
1.創(chuàng)建函數(shù)和調(diào)用函數(shù):
>>> def test():
pass
>>> test()
>>> def test():
for i in range(3):
print("I love curry")
調(diào)用函數(shù)
>>> test
<function test at 0x000001B617CCF3A0>
>>> test()
I love curry
I love curry
I love curry
2.創(chuàng)建傳參函數(shù)
>>> 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.函數(shù)的返回值
? 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.位置參數(shù)
在使用傳參函數(shù)中有兩種關(guān)鍵稱呼:
? 形式參數(shù)(形參):在創(chuàng)建函數(shù)時(shí),預(yù)留的變量名被稱為形參
? 實(shí)際參數(shù)(實(shí)參):在調(diào)用函數(shù)時(shí),給于的參數(shù)被稱為實(shí)參
>>> def test(a,b,c):
return "".join((c,b,a))
位置參數(shù)傳參
>>> test("我","愛","你")
'你愛我'
關(guān)鍵參數(shù)傳參
>>> test(c="我",b="愛",a="你")
'我愛你'
? 默認(rèn)參數(shù):
>>> 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.
? 可以看到,使用格式中出現(xiàn)了一個 / 反斜杠,他是代表在它之前是不允許使用關(guān)鍵字參數(shù)的,只可以使用位置參數(shù)
? 在函數(shù)中也一樣,但是我們可以使用*星號來代替,這樣第一位可以為位置參數(shù)或關(guān)鍵參數(shù),但是后面都要為關(guān)鍵字參數(shù)
>>> 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.多參參數(shù)
? 傳入多個參數(shù),是以元組的形式來進(jìn)行打包的,只需要在變量名前面多加一個*星號即可
? 第二種理解:傳入一個元組類型的變量,將多個值放到一個元組中
>>> def test(*args):
print(f"一共多少個參數(shù){len(args)}")
print(f"第2個參數(shù)是什么:{args[1]}")
>>> test(1,2,3,4,5)
一共多少個參數(shù)5
第2個參數(shù)是什么:2
? 如果使用了多參賦值,后面的其他變量,必須使用關(guān)鍵字參數(shù)來進(jìn)行賦值了
>>> 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
字典函數(shù)
? 在聲明一個函數(shù)中,可以同時(shí)聲明一個字典類型的變量來存儲數(shù)據(jù)
>>> 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}
解包參數(shù)
? 元組的解包
>>> 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
? 如果在函數(shù)的局部變量中與全局變量的名稱相同,那么局部變量會代替全局變量,僅限于在當(dāng)前函數(shù)中使用
>>> x = 888
>>> def test():
return x
>>> test()
888
>>> def test():
x = 666
return x
>>> test()
666
3.global語句
? 使用global來進(jìn)行聲明變量為全局變量
>>> x = 888
>>> def test():
global x
x = 520
return x
>>> x
888
>>> test()
520
>>> x
520
4.嵌套函數(shù)
>>> 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規(guī)則
L:local(局部作用域)E:enclosed(嵌套函數(shù)的外層函數(shù)作用域)G:global(全局作用域)B:build-ln(內(nèi)置作用yu)
? 當(dāng)一個局部變量與全局變量相同時(shí),這個局部變量就會代替全局變量來進(jìn)行被調(diào)用使用文章來源:http://www.zghlxwxcb.cn/news/detail-557902.html
>>> 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'
? 注意:為了避免這種情況發(fā)生,在聲明變量時(shí),盡量不要出現(xiàn)與python中的內(nèi)置函數(shù)名沖突
1,2,3,4,5,a=6,b=7)
(1, 2, 3, 4, 5) 6 7
###### 字典函數(shù)
? 在聲明一個函數(shù)中,可以同時(shí)聲明一個字典類型的變量來存儲數(shù)據(jù)
```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}
解包參數(shù)
? 元組的解包
>>> 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
? 如果在函數(shù)的局部變量中與全局變量的名稱相同,那么局部變量會代替全局變量,僅限于在當(dāng)前函數(shù)中使用
>>> x = 888
>>> def test():
return x
>>> test()
888
>>> def test():
x = 666
return x
>>> test()
666
3.global語句
? 使用global來進(jìn)行聲明變量為全局變量
>>> x = 888
>>> def test():
global x
x = 520
return x
>>> x
888
>>> test()
520
>>> x
520
4.嵌套函數(shù)
>>> 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規(guī)則
L:local(局部作用域)E:enclosed(嵌套函數(shù)的外層函數(shù)作用域)G:global(全局作用域)B:build-ln(內(nèi)置作用yu)
? 當(dāng)一個局部變量與全局變量相同時(shí),這個局部變量就會代替全局變量來進(jìn)行被調(diào)用使用
>>> 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'
? 注意:為了避免這種情況發(fā)生,在聲明變量時(shí),盡量不要出現(xiàn)與python中的內(nèi)置函數(shù)名沖突文章來源地址http://www.zghlxwxcb.cn/news/detail-557902.html
到了這里,關(guān)于【Python】學(xué)習(xí)Python常用函數(shù)作用和用法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!