Article Directory
1. Numeric data type
The Python language can be easily used to deal with numerical calculation problems. In the numerical calculation process, the two common data types are integer type (int) and floating point type (float).
The integer type (int) is a data type that represents integers. Unlike other computer languages that have precision limitations, the number of digits in an integer in Python can be of any length (limited only by computer memory). Integer objects are immutable objects.
The floating-point type (float) is a data type that represents real numbers. Corresponding to double precision (double) and single precision (float) in other computer languages, the precision of the floating-point type in Python is system-dependent.
2. Python built-in numerical operations
The built-in numeric operators and functions in the Python language support common mathematical operations on numbers.
Common built-in numeric operators:
operator | describe |
---|---|
+ | addition |
- | subtraction |
* | multiplication |
/ | real number division |
// | Integer division, rounded down |
% | take the remainder |
** | power |
It should be noted here that
in the multiplication operation, * cannot be omitted, and the expression should be different from the way of writing in mathematics. For example, the statement: m = 4ab must be written as: m = 4 * a * b.
The basic usage is as follows:
Compound assignment operators
All binary operators (+, -, , /, //, %, **) can be combined with assignment operators to form compound assignment operators (+=, -=, =, /=, //=, %=, **=), there must be no spaces between compound assignment operators. If a and b are operands, then a += b is equivalent to a = a + b; a *= b is equivalent to a = a * b.
a=1.5
b=2
a+=b #等价于a=a+b
Common built-in numeric functions
function | describe |
---|---|
abs(x) | Find the absolute value of x |
divmod(x,y) | 求x//y,x%y |
pow(x,y[,z]) | z can be omitted, if there is no, then calculate x**y, if there is, then calculate (x ** y)%z |
round(x[,n]) | Round x, if there is no n, take an integer, if there is n, keep n decimal places |
max(x1,x2,x3,…,xn) | Get the maximum value of x1, x2, x3, ..., xn |
min(x1,x2,x3,…,xn) | Get the minimum value among x1,x2,x3,...,xn |
Note here:
in pow(x,y[,z]), if z exists, then x and y must be integers, otherwise an exception will be thrown.
The basic usage is as follows:
Three, math library
Math, the standard function library for Python mathematical calculations, provides 4 mathematical constants and 44 functions. The math library only supports integer and floating-point operations, and the use of third-party libraries requires import.
Take the constant e in the math library as an example. There are two import methods:
Common constants in the math library:
constant | describe |
---|---|
pi | PI |
e | Natural logarithm |
inf | positive infinity |
in | non-floating point flag |
Common functions in the math library:
function | describe |
---|---|
fabs(x) | Find the absolute value of x |
fmod(x,y) | find x%y |
gcd(x,y) | Find the greatest common divisor of x and y, where x and y are integers |
trunc(x) | find the integer part of x |
modf(x) | Find the fractional and integer parts of x |
ceil(x) | Round up to find the smallest integer not less than x |
floor(x) | Round down to find the largest integer not greater than x |
factorial(x) | Find the factorial of x, where x is an integer |
pow(x,y) | Find x to the power of y |
exp(x) | Find e to the power of x |
sqrt(x) | find the square root of x |
log(x[,n]) | Find the logarithm of x, if there is no n, find lnx |
log2(x) | Find the 2-log value of x |
log10(x) | Find the 10-log value of x |
sin(x) | Find the value of the sine function of x |
cos(x) | Find the cosine function value of x |
tan(x) | Find the value of the tangent function of x |
asin(x) | Find the arcsine function value of x |
acos(x) | Find the arccosine function value of x |
time(x) | Find the arctangent function value of x |