Python basic tutorial on digital processing of math module
- 2020-04-02 13:30:16
- OfStack
1. The introduction of math
>>> import math
>>>dir(math) # See the list of all the function names
>>>help(math) # See definitions and functions 0 The prototype
2. Common functions
ceil(x) Take the top
floor(x) Take the bottom
fabs(x) Take the absolute value
factorial (x) factorial
hypot(x,y) sqrt(x*x+y*y)
pow(x,y) x the y To the power
sqrt(x) Open square
log(x)
log10(x)
trunc(x) Truncate the integer part
isnan (x) Determine whether NaN(not a number)
degree (x) Radian to Angle
radians(x) Angle to radian
In addition, the module defines two constants:
e = 2.718281828459045
pi = 3.141592653589793
The random
1. Introduction
Random is used to generate random Numbers, we can use it to randomly generate Numbers or select strings
import random
2. Common functions
The random. The random ()
Used to generate a random floating point number: range[0.0,1.0)
>>> import random
>>> random.random()
0.999410896951364
random.uniform(a,b)
Used to generate a random floating point number in a specified range, with a and b as upper and lower bounds
As long as a! If =b, it will generate a floating point number in between. If a=b, it will generate a floating point number
>>> random.uniform(10,20)
13.224754825064881
>>> random.uniform(20,10)
14.104410713376437
>>> random.uniform(10,10)
10.0
The random randint (a, b)
Used to generate a random integer in a specified range, with a as the lower bound and b as the upper bound
<
= n
<
= b;
If a=b, then n=a; If a
>
B, an error
>>> random.uniform(10,10)
10.0
>>> random.randint(10,20)
15
>>> random.randint(10,10)
10
>>> random.randint(20,10)
Traceback (most recent call last):
...
ValueError: empty range for randrange() (20,11, -9)
The random randrange (/ start, stop, [step],)
Gets a random number from a collection that is incremented by the specified cardinality within the specified range. The cardinality defaults to 1
>>> random.randrange(10,100,5)
95
>>> random.randrange(10,100,5)
45
The random choice (sequence)
Get a random element from a sequence. The parameter sequence represents an ordered type, not a specific type, but a list, tuple, string, etc
>>> random.choice([1,2,3,4])
1
>>> random.choice([1,2,3,4])
3
>>> random.choice('hello')
'e'
The random shuffle (x [, random])
Used to shuffle elements in a list
>>> a = [1,2,3,4,5]
>>> random.shuffle(a)
>>> a
[4, 5, 2, 1, 3]
>>> random.shuffle(a)
>>> a
[3, 2, 5, 1, 4]
The random sample (sequence, k)
The sample function does not modify the original sequence by randomly obtaining k elements from the specified sequence and returning them as a fragment
>>> a = [1,2,3,4,5]
>>> random.sample(a,3)
[1, 4, 5]
>>> random.sample(a,3)
[1, 2, 5]
>>> a
[1, 2, 3, 4, 5]
A decimal
1. Introduction
By default, floating point math lacks precision
The decimal module provides a decimal data type for floating point calculations. It helps to implement the type float rather than the built-in binary float
In financial applications and other cases where precise decimal notation is required,
Control accuracy,
Control rounding to meet legal or regulatory requirements,
Ensure decimal digit accuracy, or where the user expects the result to match the manual calculation.
Decimal reproduces manual mathematical operations, which ensures that binary floating-point Numbers cannot be precise enough to ensure the accuracy of any data. Precision enables Decimal to perform modular operations and equivalence tests that cannot be performed for binary floating-point Numbers.
2. Use
>>> from decimal import Decimal
>>> Decimal('0.1') / Decimal('0.3')
Decimal('0.3333333333333333333333333333')
>>> from decimal import getcontext
>>> getcontext().prec = 4 # Set global precision
>>> Decimal('0.1') / Decimal('0.3')
Decimal('0.3333')
fractions
Score type
structure
>>> from fractions import Fraction
>>> Fraction(16, -10) # Molecular denominator
Fraction(-8, 5)
>>> Fraction(123) # molecular
Fraction(123, 1)
>>> Fraction('3/7') # String score
Fraction(3, 7)
>>> Fraction('-.125') # String floating point number
Fraction(-1, 8)
>>> Fraction(2.25) # Floating point Numbers
Fraction(9, 4)
>>> from decimal import Decimal
>>> Fraction(Decimal('1.1')) #Decimal
Fraction(11, 10)
To calculate
>>> from fractions import Fraction
>>> a = Fraction(1,2)
>>> a
Fraction(1, 2)
>>> b = Fraction('1/3')
>>> b
Fraction(1, 3)
>>> a + b
Fraction(5, 6)
>>> a - b
Fraction(1, 6)