This article mainly introduces the common mathematical functions in the math module of Python, and also analyzes the priority# of the operators. ##Made a list, friends in need can refer to it
In mathematics, in addition to the four operations of addition, subtraction, multiplication and division - this is primary school mathematics - there are other more operations, such as multiplication Square, square root, logarithm operations, etc. To implement these operations, you need to use a module in Python: Math module (Any Pythoner can write modules and put these modules online for others to use. After installing Python, some modules are installed by default. This is called the "standard library". The modules in the "standard library" do not need to be installed and can be used directly. If the module is not included in the standard library, it needs to be installed before it can be used. As for the installation method of the module, I especially recommend using pip to install it. I’m just mentioning it here, it will be discussed specifically later, impatient readers can google it themselves.
Use the math module
The math module is in the standard library, so you don’t need to install it and you can use it directly. The usage method is:
>>> import math
>>> math.pi 3.141592653589793
>>> dir(math) ['doc', 'name', 'package', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', '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', 'sqrt', 'tan', 'tanh', 'trunc']
>>> help(math.pow)
mode, then press Enter and see the following message:
Help on built-in function pow in module math: pow(...) pow(x, y) Return x**y (x to the power of y).
The third line represents the parameters of this
function. There are two. It is also the calling method of the function. The fourth line is a description of the function. It returns the result of x**y and will be explained later. The meaning of x**y. Finally, press the q key to return to the Python interactive modeAn additional information seen from the above is that the pow function and x**y are equivalent, and both calculate the yth power of x.
>>> 4**2 16 >>> math.pow(4,2) 16.0 >>> 4*2 8
Please note that there is a big difference between 4**2 and 4*2.
The following are several commonly used examples of functions in the math module. Readers can compare them with their own debugging.
>>> math.sqrt(9) 3.0 >>> math.floor(3.14) 3.0 >>> math.floor(3.92) 3.0 >>> math.fabs(-2) # 等价于 abs(-2) 2.0 >>> abs(-2) 2 >>> math.fmod(5,3) # 等价于 5%3 2.0 >>> 5%3 2
Several common functions
There are several commonly used functions. Let’s list them. It doesn’t matter if you can’t remember them. Just know that they are there. Just google them when you use them.
Find the absolute value
>>> abs(10) 10 >>> abs(-10) 10 >>> abs(-1.2) 1.2
>>> round(1.234) 1.0 >>> round(1.234,2) 1.23 >>> # 如果不清楚这个函数的用法,可以使用下面方法看帮助信息 >>> help(round)
Help on built-in function round in module builtin: round(...) round(number[, ndigits]) -> floating point number Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number. Precision may be negative.
Operation Priority
Since primary school mathematics, we have studied the issue of priority of operations. For example, among the four arithmetic operations, "multiplication and division first, then addition and subtraction" mean that multiplication and division have higher priority than addition and subtraction.
For the same level, calculation is performed in the order of "left to right".
table
lists the priority order of various operations in Python. However, in general, there is no need to memorize it, and it can be understood according to mathematics. Since humans have invented mathematics, the operations performed in computers do not need to rewrite a new set of specifications. They only need to comply with mathematics. Just hit it.lambda | Lambda Expression |
or | Boolean "or" |
and | Boolean "AND" |
not x | Boolean "NOT" |
in, not in | Member Test |
is, is not | Identity Test |
<, <=, >, >=, !=, == | Compare |
Bitwise OR | |
Bitwise XOR | |
Bitwise AND | |
Shift | |
Addition and subtraction | |
Multiplication, division and remainder | |
Positive and negative signs | |
Bitwise flip | |
Index | |
Attribute reference | |
subscript | |
Addressing segment | |
Function call | |
Binding or tuple display | |
List display | |
Dictionary display | |
String | Conversion |
Finally, what I want to mention is the ultimate killer in operations: parentheses. As long as there are parentheses, the contents inside the parentheses are calculated first. This is a consensus in mathematics and requires no explanation.
The above is the detailed content of Summary of commonly used mathematical functions in Python's math module. For more information, please follow other related articles on the PHP Chinese website!