Python 初學之math模組/random模組/decimal模組/fractions模組__Python

來源:互聯網
上載者:User

1.math模組



>>> dir(math)

['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']


math.e

math.pi

math.copysign(x,y):返回與y同號的x值。

math.ceil(x);返回<=x的最小整數

math.floor(x):返回>=x 的最大整數

math.hypot(x,y):返回sqrt(x*x + y*y)

math.modf(x);返回x的小數部分和整數部分

math.trunc(x);返回x的整數部分

math.pow(x,y):返回x**y

math.log(x,a):返回返回以a為底x的對數,若不指定參數a,預設為e

math.log10(x)



2.random模組

random.random():產生一個0<=x,1的隨機浮點數

random.uniform();產生一個指定範圍內的隨機浮點數

random.randint(a,b):產生一個a<=x<=b之間的整數

random.randrange(a,b,step):產生一個隨機數

random.choice(sequence):從序列中擷取一個隨機元素,列表元組字串

random.shuffle(list):打亂列表

random.sample(sequence,k):從序列中隨機擷取指定長度為k的片段。



3.decimal模組

getcontext().prec,設定小數點的精度。

>>> from decimal import Decimal,getcontext>>> print(Decimal('1.0')/Decimal('3.0'))0.3333333333333333333333333333>>> getcontext().prec = 6>>> print(Decimal('1.0')/Decimal('3.0'))0.333333

4.fractions模組

用於表現和處理分數

>>> from fractions import Fraction>>> print Fraction(1,3)1/3




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.