Python Beginner's Math module/random Module/decimal Module/fractions module __python

Source: Internet
Author: User
Tags hypot pow

1.math Module



>>> dir (Math)

[' __doc__ ', ' __name__ ', ' __package__ ', ' ACOs ', ' acosh ', ' asin ', ' Asinh ', ' atan ', ' atan2 ', ' Atanh ', ' ceil ', ' copysign ', ' Co S ', ' 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 ', ' sq RT ', ' tan ', ' tanh ', ' trunc ']


Math.e

Math.PI

Math.copysign (X,y): Returns the X value of the same number as Y.

Math.ceil (x); Returns the smallest integer of <=x

Math.floor (x): Returns the largest integer of >=x

Math.hypot (x,y): Back to sqrt (x*x + y*y)

MATH.MODF (x); Returns the fractional part of X and the whole number of parts

Math.trunc (x); Returns the integer portion of x

Math.pow (x,y): Back to X**y

Math.log (X,a): Returns the logarithm that returns the base x of a, if parameter A is not specified, the default is E

MATH.LOG10 (x)



2.random Module

Random.random (): Generates a 0<=x,1 random floating-point number

Random.uniform (); Generates a random floating-point number within a specified range

Random.randint (A,B): Generates an integer between a<=x<=b

Random.randrange (a,b,step): Generate a random number

Random.choice (Sequence): Gets a random element from the sequence, a list tuple string

Random.shuffle (list): Scrambling lists

Random.sample (sequence,k): Randomly get fragments of a specified length of k from a sequence.



3.decimal Module

GetContext (). Prec, set the precision of the decimal point.

>>> 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 Module

Used to perform and process fractions

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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.