1.函數(shù)
Python內(nèi)置了很多有用的函數(shù),我們可以直接調(diào)用。
要調(diào)用一個函數(shù),需要知道函數(shù)的名稱和參數(shù),比如求絕對值的函數(shù)abs
,只有一個參數(shù)??梢灾苯訌腜ython的官方網(wǎng)站查看文檔:
http://docs.python.org/3/library/functions.html#abs
調(diào)用函數(shù)的時候,如果傳入的參數(shù)數(shù)量不對,會報TypeError的錯誤,并且Python會明確地告訴你:abs()有且僅有1個參數(shù),但給出了兩個:
>>> abs(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abs() takes exactly one argument (2 given)
如果傳入的參數(shù)數(shù)量是對的,但參數(shù)類型不能被函數(shù)所接受,也會報TypeError的錯誤,并且給出錯誤信息:str是錯誤的參數(shù)類型:
>>> abs('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
而max函數(shù)max()可以接收任意多個參數(shù),并返回最大的那個:
>>> max(1, 2)
2
>>> max(2, 3, 1, -5)
3
Python內(nèi)置的常用函數(shù)還包括數(shù)據(jù)類型轉(zhuǎn)換函數(shù),比如int()函數(shù)可以把其他數(shù)據(jù)類型轉(zhuǎn)換為整數(shù):
>>> int('123')
123
>>> int(12.34)
12
>>> float('12.34')
12.34
>>> str(1.23)
'1.23'
>>> str(100)
'100'
>>> bool(1)
True
>>> bool('')
False
函數(shù)名其實就是指向一個函數(shù)對象的引用,完全可以把函數(shù)名賦給一個變量,相當于給這個函數(shù)起了一個“別名”:
>>> a = abs # 變量a指向abs函數(shù)
>>> a(-1) # 所以也可以通過a調(diào)用abs函數(shù)
1
1.1.定義函數(shù)
在Python中,定義一個函數(shù)要使用def語句,依次寫出函數(shù)名、括號、括號中的參數(shù)和冒號:,然后,在縮進塊中編寫函數(shù)體,函數(shù)的返回值用return語句返回。
我們以自定義一個求絕對值的my_abs函數(shù)為例:
def my_abs(x):
if x >= 0:
return x
else:
return -x
1.2 空函數(shù)
如果想定義一個什么事也不做的空函數(shù),可以用pass語句:
def nop():
pass
pass語句什么都不做,那有什么用?實際上pass可以用來作為占位符,比如現(xiàn)在還沒想好怎么寫函數(shù)的代碼,就可以先放一個pass,讓代碼能運行起來。
pass還可以用在其他語句里,比如:
if age >= 18:
pass
缺少了pass,代碼運行就會有語法錯誤
1.3 參數(shù)檢查
調(diào)用函數(shù)時,如果參數(shù)個數(shù)不對,Python解釋器會自動檢查出來,并拋出TypeError:
>>> my_abs(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: my_abs() takes 1 positional argument but 2 were given
但是如果參數(shù)類型不對,Python解釋器就無法幫我們檢查。試試my_abs和內(nèi)置函數(shù)abs的差別:
>>> my_abs('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in my_abs
TypeError: unorderable types: str() >= int()
>>> abs('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
當傳入了不恰當?shù)膮?shù)時,內(nèi)置函數(shù)abs會檢查出參數(shù)錯誤,而我們定義的my_abs沒有參數(shù)檢查,會導致if語句出錯,出錯信息和abs不一樣。所以,這個函數(shù)定義不夠完善。
讓我們修改一下my_abs的定義,對參數(shù)類型做檢查,只允許整數(shù)和浮點數(shù)類型的參數(shù)。數(shù)據(jù)類型檢查可以用內(nèi)置函數(shù)isinstance()實現(xiàn):
def my_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x
添加了參數(shù)檢查后,如果傳入錯誤的參數(shù)類型,函數(shù)就可以拋出一個錯誤:
>>> my_abs('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in my_abs
TypeError: bad operand type
1.4 返回多個值
函數(shù)可以返回多個值嗎?答案是肯定的。
比如在游戲中經(jīng)常需要從一個點移動到另一個點,給出坐標、位移和角度,就可以計算出新的新的坐標:
import math
def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny
import math語句表示導入math包,并允許后續(xù)代碼引用math包里的sin、cos等函數(shù)。
然后,我們就可以同時獲得返回值:
>>> x, y = move(100, 100, 60, math.pi / 6)
>>> print(x, y)
151.96152422706632 70.0 # 這里是2個數(shù),中間有個空格
但其實這只是一種假象,Python函數(shù)返回的仍然是單一值:
>>> r = move(100, 100, 60, math.pi / 6)
>>> print(r)
(151.96152422706632, 70.0)
原來返回值是一個tuple!但是,在語法上,返回一個tuple可以省略括號,而多個變量可以同時接收一個tuple,按位置賦給對應(yīng)的值,所以,Python的函數(shù)返回多值其實就是返回一個tuple,但寫起來更方便。
小結(jié)
定義函數(shù)時,需要確定函數(shù)名和參數(shù)個數(shù);
如果有必要,可以先對參數(shù)的數(shù)據(jù)類型做檢查;
函數(shù)體內(nèi)部可以用return隨時返回函數(shù)結(jié)果;
函數(shù)執(zhí)行完畢也沒有return語句時,自動return None。
函數(shù)可以同時返回多個值,但其實就是一個tuple。
附錄:
這里寫了一個小代碼實現(xiàn) ax**2 + bx + c = 0,求x 值
import math
def quadratic(a,b,c):
# ax**2 + bx + c == 0
# 參數(shù)檢查
if not isinstance(a,(int,float)):
raise TypeError('bad operand type')
if not isinstance(b,(int,float)):
raise TypeError('bad operand type')
if not isinstance(c,(int,float)):
raise TypeError('bad operand type')
if b*b - 4*a*c < 0:
print('error!,you have a mistake')
else:
if a == 0 and b != 0:
x = c/b
return x
if a != 0 and b == 0:
x = math.sqrt(-c/a)
return x
print("當b為0,結(jié)果應(yīng)該2個數(shù),一正一負")
if a != 0 and b != 0:
y = (b**2 - 4*a*c)/(4*a*a)
x1 = -math.sqrt(y) -b/(2*a)
x2 = math.sqrt(y) -b/(2*a)
return x1,x2
print('此時也該2個值,a b 均不等于0')
print(quadratic(1, 0, -1)) #