Python Study Notes 17: Mathematical Correlation of the standard library (math package, random package)
The previous sections are really tired. Now let's take a look at simple and easy-to-understand content.
A math package mainly deals with mathematical operations.
Constant math. e # natural constant e
Math. pi # pi
Calculation function math. ceil (x) # rounded up x, for example, x = 1.2, returns 2
Math. floor (x) # Round down x. For example, if x = 1.2, 1 is returned.
Math. pow (x, y) # exponential operation to obtain the power y of x
Math. log (x) # logarithm. The default base is e. You can use the base parameter to change the base of the logarithm. For example, 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)
All these functions receive an x in radians 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. acossh (x), math. atanh (x)
Special Functions
Math. erf (x), math. gamma (x)
If you already know the principles of a pseudo-random number (psudo-random number) in the binary random package, you can use the following:
Random. seed (x)
To change the seed of the random number generator. If you do not understand the principle, you do not need to set seed. Python will help you select seed.
Random selection and sorting of random. choice (seq) # randomly selects an element from the element of the sequence, such as random. choice (range (10), a random integer from 0 to 9.
Random. sample (seq, k) # randomly selects k elements from the sequence
Random. shuffle (seq) # random sorting of all elements in the sequence
The real numbers generated under the random generation of real numbers conform to the uniform distribution (uniform distribution), meaning that each number in a range has the same probability:
Random. random () # randomly generates the next real number 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 real numbers generated below conform to other distributions (you can refer to some statistical books to learn about these distributions ):
Random. gauss (mu, sigma) # randomly generate random numbers that match the Gaussian distribution. mu and sigma are two parameters of Gaussian distribution.
Random. expovariate (lambd) # randomly generate random numbers that match the exponential distribution. lambd is the exponential distribution parameter.
In addition, there are also logarithm distribution, normal distribution, exclusive distribution, and Weibo distribution.
Suppose we have a group of people participating in dance competitions. To be fair, we need to randomly arrange their playing order. We use the random package below:
import randomall_people = ['Tom', 'Vivian', 'Paul', 'Liya', 'Manu', 'Daniel', 'Shawn']random.shuffle(all_people)for i,name in enumerate(all_people): print(i,':'+name)