溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python數(shù)字類型有哪些

發(fā)布時間:2021-11-22 14:43:33 來源:億速云 閱讀:188 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“Python數(shù)字類型有哪些”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“Python數(shù)字類型有哪些”吧!

一、數(shù)字

Python中數(shù)字類型包括:

  • 整數(shù)

  • 浮點數(shù)

  • 復(fù)數(shù)

  • 固定精度的十進制數(shù)

  • 有理分數(shù)

  • 集合

  • 布爾類型

  • 無窮的整數(shù)精度

  • 各種數(shù)字內(nèi)置函數(shù)和模塊

二、內(nèi)置數(shù)學(xué)工具

表達式操作符:

操作符描述
yield生成器函數(shù)發(fā)送協(xié)議
lambda args:expression生成匿名函數(shù)
x if y else z三元表達式
x or y邏輯或(存在短路算法)
x and y邏輯與(存在短路算法)
not x邏輯非
x in y , x not in y成員關(guān)系
x is y ,x is not y對象實體測試
x<y,x<=y,x>y,x>=y,x==y,x!=y比較大小
x|y位或,集合并集
x^y位異或,集合對稱差
x&y位與,集合交集
x<<y,x>>y左移或者右移y位
x+y,x-y加減法、合并刪除
x*y,x%y,x/y,x//y乘,取余數(shù),除,地板除
-x,+x一元減法
~x按位求補(取反)
x**y冪運算
x[i]索引,函數(shù)調(diào)用
x[i:j:k]分片
x(...)調(diào)用函數(shù)
x.attr調(diào)用屬性
()元組,表達式,生成器
[]列表,列表解析
{}字典,集合,集合和字典解析

內(nèi)置數(shù)學(xué)函數(shù)

  • math

    函數(shù)|常量說明實例
    e自然常數(shù)emath.e
    2.718281828459045
    inf默認值:inf
    nan默認值:nan
    pi圓周率πmath.pi
    3.141592653589793
    tau6.283185307179586
    acos(x)返回x的反三角余弦值math.acos(math.sqrt(2)/2)
    0.7853981633974483
    acosh(x)返回x的反雙曲余弦函數(shù)
    asin(x)返回x的反三角正弦值math.asin(0.5)
    0.5235987755982989
    asinh(x)返回x的反雙曲正弦函數(shù)
    atan(x)返回x的反三角正切值math.atan(1.7320508075688767)
    1.0471975511965976
    atan2(y, x)返回x/y的反三角正切值math.atan2(2,1)
    1.1071487177940904
    atanh(x)返回x的反雙曲正切函數(shù)
    ceil(x)這個方法對i向上取整math.ceil(5.2)
    6.0
    copysign(x, y)若y<0,返回-1乘以x的絕對值;
    否則,返回x的絕對值
    math.copysign(5.2, -1)
    -5.2
    cos(x)返回x(弧度)的三角余弦值math.cos(math.radians(45))
    0.7071067811865476
    cosh(x)返回x的雙曲余弦函數(shù)
    degrees(x)弧度轉(zhuǎn)度math.degrees(math.pi)
    180.0
    erf(x)返回x的誤差函數(shù)
    erfc(x)返回x的余誤差函數(shù)
    exp(x)返回e的x次方math.exp(2)
    7.38905609893065
    expm1(x)返回e的x次方減1math.expm1(2)
    6.38905609893065
    fabs(x)返回x的絕對值math.fabs(-5)
    5.0
    factorial(x)返回x的階乘math.factorial(5)
    120
    floor(x)向下取整。math.floor(5.8)
    5.0
    fmod(x, y)返回x%y(取余)math.fmod(5,2)
    1.0
    frexp(x)返回m和i,滿足m乘以2的i次方math.frexp(3)
    (0.75, 2)
    fsum(iterable)返回?zé)o損精度的和
    gamma(x)返回x的伽瑪函數(shù)
    hypot(x, y)返回以x和y為直角邊的斜邊長math.hypot(3,4)
    5.0
    isinf(x)若x為無窮大,返回True;否則,返回False
    isnan(x)若x不是數(shù)字,返回True;否則,返回Falsemath.isnan(1.2e3)
    False
    ldexp(x, i)返回x乘以2的i次方math.ldexp(0.75, 2)
    3.0
    lgamma(x)返回x的絕對值的自然對數(shù)的伽瑪函數(shù)
    log(x, base=None)返回x的以base為底的對數(shù),base默認為e
    log10(x)返回x的以10為底的對數(shù)math.log10(2)
    0.30102999566398114
    log1p(x)返回1+x的自然對數(shù)(以e為底)math.log1p(math.e-1)
    1.0
    log2(x)

    modf(x)返回x的小數(shù)和整數(shù)math.modf(5.2)
    (0.20000000000000018, 5.0)
    pow(x, y)返回x的y次方math.pow(5,3)
    125.0
    radians(x)度轉(zhuǎn)弧度math.radians(45)
    0.7853981633974483
    sin(x)返回x(弧度)的三角正弦值math.sin(math.radians(30))
    0.49999999999999994
    sinh(x)返回x的雙曲正弦函數(shù)
    sqrt(x)返回x的平方根math.sqrt(3)
    1.7320508075688772
    tan(x)返回x(弧度)的三角正切值math.tan(math.radians(60))
    1.7320508075688767
    tanh(x)返回x的雙曲正切函數(shù)
    trunc(x)返回x的整數(shù)部分math.trunc(5.8)
    5
    • math.log(math.e)
      1.0

    • math.log(2, 10)
      0.30102999566398114

    • math.isinf(1.0e+308)
      False

    • math.isinf(1.0e+309)
      True

    • 0.1+0.2+0.3
      0.6000000000000001

    • math.fsum([0.1, 0.2, 0.3])
      0.6

  • random

    函數(shù)|常量說明實例
    random用于生成一個0到1的隨機符點數(shù): 0 <= n < 1.0
    uniformrandom.uniform的函數(shù)原型為:random.uniform(a, b),用于生成一個指定范圍內(nèi)的隨機符點數(shù),兩個參數(shù)其中一個是上限,一個是下限。如果a > b,則生成的隨機數(shù)n: a <= n <= b。如果 a <b, 則 b <= n <= a。print random.uniform(10, 20)
    print random.uniform(20, 10)
    #---- 結(jié)果(不同機器上的結(jié)果不一樣)
    #18.7356606526
    #12.5798298022
    randintrandom.randint()的函數(shù)原型為:random.randint(a, b),用于生成一個指定范圍內(nèi)的整數(shù)。其中參數(shù)a是下限,參數(shù)b是上限,生成的隨機數(shù)n: a <= n <= bprint random.randint(12, 20)  #生成的隨機數(shù)n: 12 <= n <= 20
    print random.randint(20, 20)  #結(jié)果永遠是20
    #print random.randint(20, 10)  #該語句是錯誤的。下限必須小于上限。 
    randrangerandom.randrange的函數(shù)原型為:random.randrange([start], stop[, step]),從指定范圍內(nèi),按指定基數(shù)遞增的集合中 獲取一個隨機數(shù)。random.randrange(10, 100, 2)
    結(jié)果相當于從[10, 12, 14, 16, ... 96, 98]序列中獲取一個隨機數(shù)。
    在結(jié)果上與 random.choice(range(10, 100, 2) 等效。
    choicerandom.choice從序列中獲取一個隨機元素。其函數(shù)原型為:random.choice(sequence)。參數(shù)sequence表示一個有序類型。這里要說明 一下:sequence在python不是一種特定的類型,而是泛指一系列的類型。list, tuple, 字符串都屬于sequence。print random.choice("學(xué)習(xí)Python")
    print random.choice(["JGood", "is", "a", "handsome", "boy"])
    print random.choice(("Tuple", "List", "Dict"))
    shufflerandom.shuffle的函數(shù)原型為:random.shuffle(x[, random]),用于將一個列表中的元素打亂。p = ["Python", "is", "powerful", "simple", "and so on..."]
    random.shuffle(p)
    print p
    #---- 結(jié)果(不同機器上的結(jié)果可能不一樣。)
    #['powerful', 'simple', 'is', 'Python', 'and so on...'] 
    samplerandom.sample的函數(shù)原型為:random.sample(sequence, k),從指定序列中隨機獲取指定長度的片斷。sample函數(shù)不會修改原有序列。list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    slice = random.sample(list, 5)  #從list中隨機獲取5個元素,作為一個片斷返回
    print slice
    print list #原有序列并沒有改變。

三、分數(shù)

from fractions import Fraction

x = Fraction(1, 3)

1/3

到此,相信大家對“Python數(shù)字類型有哪些”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI