筆記帶有個人側(cè)重點,不追求面面俱到。
4 數(shù)字
出處:菜鳥教程 - Python3 數(shù)字(Number)
數(shù)據(jù)類型是不允許改變的,這就意味著如果改變數(shù)字數(shù)據(jù)類型的值,將重新分配內(nèi)存空間。
4.1 Python 數(shù)字運算
注意:
//
得到的并不一定是整數(shù)類型的數(shù),它與分母分子的數(shù)據(jù)類型有關(guān)系。
實例:
>>> 7//2
3
>>> 7.0//2
3.0
>>> 7//2.0
3.0
4.2 數(shù)字函數(shù)
4.2.1 abs() 函數(shù)
描述:
abs() 函數(shù)返回數(shù)字的絕對值。
語法:
abs(x)
參數(shù):
- x – 數(shù)值表達式,可以是整數(shù),浮點數(shù),復(fù)數(shù)。
返回值:文章來源:http://www.zghlxwxcb.cn/news/detail-675138.html
函數(shù)返回 x(數(shù)字)的絕對值,如果參數(shù)是一個復(fù)數(shù),則返回它的大小。
>>>v = Vecter(3, 4)
>>>abs(v)
5.0
fabs() 與 abs() 的區(qū)別:
-
abs()
是一個內(nèi)置函數(shù),而 fabs() 在 math 模塊中定義的; -
fabs()
函數(shù)只適用于 float 和 integer 類型,而 abs() 也適用于復(fù)數(shù); -
abs()
的返回值可以是整數(shù)也可以是浮點數(shù),視輸入而定,fabs()
的返回值總是浮點數(shù)。
4.2.2 ceil() 函數(shù)
描述:
ceil()
函數(shù)返回一個大于或等于 x 的的最小整數(shù)。(向上取整)
語法:
import math
math.ceil(x)
參數(shù):
- x – 數(shù)值表達式。
返回值:
返回一個大于或等于 x 的的最小整數(shù)。
4.2.3 cmp() 函數(shù)(Python 3 已廢棄)
描述:
如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1。
注意: Python 3 已廢棄,使用 (x>y)-(x<y) 替換。
4.2.4 floor() 函數(shù)
描述:
floor() 返回數(shù)字的下舍整數(shù),小于或等于 x。(向下取整)
語法:
import math
math.floor(x)
參數(shù):
- x – 數(shù)值表達式。
返回值:
返回小于或等于 x 的整數(shù)。
4.2.5 max() 函數(shù)
描述:
max() 方法返回給定參數(shù)的最大值,參數(shù)可以為序列。
注意: 入?yún)㈩愋筒荒芑烊耄慈菙?shù)字,要么全是序列。入?yún)⑹切蛄械脑挘?單序列入?yún)?,返回序列中最大的一個數(shù)值。多序列入?yún)? 按索引順序,逐一對比各序列的當(dāng)前索引位的 “值”,直到遇見最大值立即停止對比,并返回最大值所在的序列
>>> max(0, True)
True
>>> max([1,2,3])
3
>>> max([2,4], [3,6])
[3, 6]
>>> max([2,4], [1,5])
[2, 4]
>>> max((1,-1,0), (True,False,2,0),(1, 0, 0, 2))
(True, False, 2, 0)
>>> max((1,-1,0), (True,),(1,))
(1, -1, 0)
>>> max([1,3,2],3,4) #非法入?yún)?/span>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'list'
>>> max((1,2,3), [2,4,1]) #非法入?yún)?/span>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'list' and 'tuple'
4.2.6 modf() 函數(shù)
描述:
modf() 方法返回 x 的整數(shù)部分與小數(shù)部分,兩部分的數(shù)值符號與 x 相同,整數(shù)部分以浮點型表示。
語法:
import math
math.modf(x)
參數(shù):
- x – 數(shù)值表達式。
返回值:
返回x的整數(shù)部分與小數(shù)部分。
實例:
>>> import math
>>> math.modf(3.2)
(0.20000000000000018, 3.0)
>>> math.modf(-0.01)
(-0.01, -0.0)
>>> type(math.modf(-3.2))
<class 'tuple'>
4.2.7 pow() 函數(shù)
描述:
pow() 方法返回 xy(x的y次方) 的值。
語法:
import math
math.pow(x, y)
pow(x, y[, z])
注意:
pow()
通過內(nèi)置的方法直接調(diào)用,內(nèi)置方法會把參數(shù)作為整型,而 math 模塊則會把參數(shù)轉(zhuǎn)換為 float。
參數(shù):
- x – 數(shù)值表達式。
- y – 數(shù)值表達式。
- z – 數(shù)值表達式。
返回值:
返回 xy(x的y次方) 的值。
4.2.8 round() 函數(shù)
描述:
round() 方法返回浮點數(shù) x 的四舍五入值,準確的說保留值將保留到離上一位更近的一端(四舍六入)。精度要求高的,不建議使用該函數(shù)。
語法:
round( x [, n] )
參數(shù):
- x – 數(shù)字表達式。
- n – 表示從小數(shù)點位數(shù),其中 x 需要四舍五入,默認值為 0。
返回值:
返回浮點數(shù)x的四舍五入值。
注意:
round()
保留值將保留到離上一位更近的一端(四舍六入)。如果距離兩邊一樣遠,會保留到偶數(shù)的一邊。比如 round(0.5) 和 round(-0.5) 都會保留到 0,而 round(1.5) 會保留到 2。同時,受浮點數(shù)精度影響,結(jié)果不一定復(fù)合預(yù)期。
參考:python中關(guān)于round函數(shù)的小坑
4.3 隨機數(shù)函數(shù)
4.3.1 choice() 函數(shù)
描述:
choice() 方法返回一個列表,元組或字符串的隨機項。
語法:
import random
random.choice(seq)
參數(shù):
- seq – 可以是一個列表,元組或字符串。
返回值:
返回隨機項。
實例:
random.choice(range(100))
random.choice([1, 2, 3, 5, 9])
random.choice('Runoob')
4.3.2 randrange() 函數(shù)
描述:
randrange() 方法返回指定遞增基數(shù)集合中的一個隨機數(shù),基數(shù)默認值為1。
語法:
import random
random.randrange ([start,] stop [,step])
參數(shù):
- start – 指定范圍內(nèi)的開始值,包含在范圍內(nèi);
- stop – 指定范圍內(nèi)的結(jié)束值,不包含在范圍內(nèi);
- step – 指定遞增基數(shù)。
返回值:
從給定的范圍返回隨機項。
實例:
random.randrange(1, 100, 2)
random.randrange(100)
4.3.3 shuffle() 函數(shù)
描述:
shuffle() 方法將序列的所有元素隨機排序。
語法:
import random
random.shuffle(lst)
參數(shù):
- lst – 列表。
返回值:
返回 None。
4.3.4 uniform() 函數(shù)
描述:
uniform() 方法將隨機生成下一個實數(shù),它在 [x,y] 范圍內(nèi)。
語法:
import random
random.uniform(x, y)
參數(shù):
- x – 隨機數(shù)的最小值,包含該值;
- y – 隨機數(shù)的最大值,包含該值。
返回值:
返回一個浮點數(shù) N,取值范圍為如果 x<y 則 x <= N <= y,如果 y<x 則y <= N <= x。文章來源地址http://www.zghlxwxcb.cn/news/detail-675138.html
到了這里,關(guān)于菜鳥教程 《Python 3 教程》筆記(4):數(shù)字的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!