實例講解Python中函數(shù)的調(diào)用與定義
調(diào)用函數(shù):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 函數(shù)調(diào)用
>>> abs(100)
100
>>> abs(-110)
110
>>> abs(12.34)
12.34
>>> abs(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: abs() takes exactly one argument (2 given)
>>> abs('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: bad operand type for abs(): 'str'
>>> max(1, 2)
2
>>> max(2, 3, 1, -5)
3
>>> int('123')
123
>>> int(12.34)
12
>>> str(1.23)
'1.23'
>>> str(100)
'100'
>>> bool(1)
True
>>> bool('')
False
>>> a = abs # 變量a指向abs函數(shù),相當(dāng)于引用
>>> a(-1) # 所以也可以通過a調(diào)用abs函數(shù)
1
>>> n1 = 255
>>> n2 = 1000
>>> print(hex(n1))
0xff
>>> print(hex(n2))
0x3e8
定義函數(shù):
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #函數(shù)定義 def myAbs(x): if x >= 0: return x else: return -x a = 10 myAbs(a) def nop(): # 空函數(shù) pass
pass語句什么都不做 。
實際上pass可以用來作為占位符,比如現(xiàn)在還沒想好怎么寫函數(shù)代碼,就可以先寫一個pass,讓代碼運行起來。
if age >= 18:
pass
#缺少了pass,代碼就會有語法錯誤
>>> if age >= 18:
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>> myAbs(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: myAbs() takes 1 positional argument but 2 were given
>>> myAbs('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in myAbs
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'
def myAbs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x
>>> myAbs('A')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in myAbs
TypeError: bad operand type
返回兩個值?
import math def move(x, y, step, angle = 0): nx = x + step * math.cos(angle) ny = y - step * math.sin(angle) return nx, ny >>> x, y = move(100, 100, 60, math.pi / 6) >>> print(x, y) 151.96152422706632 70.0
其實上面只是一種假象,Python函數(shù)返回的仍然是單一值 。
>>> r = move(100, 100, 60, math.pi / 6) >>> print(r) (151.96152422706632, 70.0)
實際上返回的是一個tuple!
但是,語法上,返回一個tuple可以省略括號, 而多個變量可以同時接受一個tuple,按位置賦給對應(yīng)的值。
所以,Python的函數(shù)返回多值實際就是返回一個tuple,但是寫起來更方便。
函數(shù)執(zhí)行完畢也沒有return語句時,自動return None。
練習(xí) :
import math def quadratic(a, b, c): x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a) x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a) return x1, x2 x1, x2 = quadratic(2, 5, 1) print(x1, x2) >>> import math >>> def quadratic(a, b, c): ... x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a) ... x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a) ... return x1, x2 ... >>> x1, x2 = quadratic(2, 5, 1) >>> print(x1, x2) -0.21922359359558485 -2.2807764064044154
相關(guān)文章
selenium+Chrome滑動驗證碼破解二(某某網(wǎng)站)
這篇文章主要介紹了selenium+Chrome滑動驗證碼破解二(某某網(wǎng)站),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
python+numpy按行求一個二維數(shù)組的最大值方法
今天小編就為大家分享一篇python+numpy按行求一個二維數(shù)組的最大值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
PyQt5.6+pycharm配置以及pyinstaller生成exe(小白教程)
這篇文章主要介紹了PyQt5.6+pycharm配置以及pyinstaller生成exe,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
python使用tkinter實現(xiàn)透明窗體上繪制隨機出現(xiàn)的小球(實例代碼)
今天教大家怎么實現(xiàn)Tkinter透明窗體,在上篇文章給大家介紹過透明窗體上繪制小球,今天接著通過實例代碼給大家分享python使用tkinter實現(xiàn)透明窗體上繪制隨機出現(xiàn)的小球的實例代碼,感興趣的朋友跟隨小編一起看看吧2021-05-05

