Python Learning note 17: Mathematical correlation of the standard library (math package, random package)

Source: Internet
Author: User
Tags constant math shuffle square root

The first few sections are really tired, so let's start with something simple and easy to understand.

A math package math package mainly deals with mathematical-related operations.

Constant MATH.E # Natural constant E
Math.PI # pi Pi

Arithmetic function Math.ceil (x) # Rounding up the x, such as x=1.2, returns 2
Math.floor (x) # rounding x down, such as x=1.2, returns 1
Math.pow (x, y) # exponential operation, get x y-square
Math.log (x) # logarithm, the default base is E. You can use the base parameter to change the base of the logarithm. such as Math.log (100,base=10)
MATH.SQRT (x) # square root


Trigonometric function

Math.sin (x), Math.Cos (x), Math.tan (x), Math.asin (x), Math.acos (x), Math.atan (x)
These functions receive an X in radians (radian) as a parameter.


Angle and Radian Interchange

Math.degrees (x), Math.radians (x)


Hyperbolic functions

Math.sinh (x), Math.cosh (x), Math.tanh (x), Math.asinh (x), Math.acosh (x), Math.atanh (x)


Special functions

Math.erf (x), Math.gamma (x)


Two random packets if you already know the principle of pseudo-random number (Psudo-random numbers), then you can use the following:
Random.seed (x)
To change the seeds seed of the random number generator. If you don't know how it works, you don't have to specifically set Seed,python to help you choose Seed.


Randomly pick and sort random.choice (seq) # Randomly pick an element from the elements of the sequence, such as Random.choice (range (10)), and randomly pick an integer from 0 to 9.
Random.sample (seq,k) # random selection of k elements from a sequence
Random.shuffle (seq) # Randomly sorts all elements of a sequence


The real numbers generated under the random generated real numbers conform to the uniform distribution (uniform distribution), meaning that each number in a range is equal in probability:
Random.random () # randomly generates the next real number, which is within the range of [0,1].
Random.uniform (A, B) # randomly generates the next real number, which is within the range of [A, b].


The resulting real numbers correspond to other distributions (you can refer to some statistical books to understand these distributions):
Random.gauss (MU,SIGMA) # randomly generates a random number that conforms to the Gaussian distribution, and the Mu,sigma is two parameters of the Gaussian distribution.
Random.expovariate (LAMBD) # randomly generates a random number that conforms to the exponential distribution, LAMBD is the parameter of the exponential distribution.
There are also logarithmic distributions, normal distribution, Pareto distributions, Weibull distributions.


Suppose we have a group of people in a dance competition, in order to be fair, we have to randomly arrange their appearances. Below we use the random package to implement:

Import randomall_people = [' Tom ', ' Vivian ', ' Paul ', ' Liya ', ' Manu ', ' Daniel ', ' Shawn ']random.shuffle (all_people) for I, Name in Enumerate (all_people):    print (i, ': ' +name)


Python Learning note 17: Mathematical correlation of the standard library (math package, random package)

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.