This article mainly introduces the sorting of common mathematical functions in the math module of Python, and lists the operation priorities of operators, for more information, see the next article. This article mainly describes the common mathematical functions in the math module of Python, and lists the operation priorities of operators. For more information, see
In mathematics, apart from the addition, subtraction, multiplication, and division of the four arithmetic operations-this is a primary school mathematics-there are more arithmetic operations, such as multiplication, square, logarithm, and so on, A Python module is required: Math
Module is a very important thing in Python. you can understand it as a Python extension tool. In other words, Python provides some available items by default, but these provided by default are far from meeting the needs of programming practices, so someone specially made some other tools. These tools are called "modules"
Any Pythoner can write modules and put these modules online for use by others.
After Python is installed, some modules are installed by default. this is called the "Standard Library". the modules in the "standard library" can be directly used without installation.
If the module is not included in the standard library, it must be installed before use. I recommend pip to install the module. Here, I will only mention it later, and we will talk about it in a specific way. users who are in urgent need can google themselves.
Use math module
The math module is in the standard library, so it can be used directly without installation. The usage is as follows:
>>> import math
Use import to reference the math module. you can use the tool provided by this module below. For example, to obtain the circumference rate:
>>> math.pi3.141592653589793
What can this module do? You can see in the following method:
>>> 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']
Dir (module) is a very useful command that allows you to view the tools contained in any module. From the list above, we can see that in the math module, we can calculate the positive sin (a), cos (a), sqrt ()......
These are called functions, that is, various computing functions are provided in the math module. for example, pow functions can be used to calculate the multiplication party. But how to use it?
Python is a very thoughtful girl. she has provided a command for us to view how to use each function.
>>> help(math.pow)
In interactive mode, enter the preceding command and press enter to view the following information:
Help on built-in function pow in module math: pow(...) pow(x, y) Return x**y (x to the power of y).
The following describes how to use the pow function in the math module.
The first line indicates the pow help information of the built-in function of the math module (the so-called built-in is called a built-in function, which is the default function of Python)
The third line indicates the parameter of the function. There are two parameters, which are also the method of calling the function.
The fourth line is a description of the function, returns the result of x ** y, and explains the meaning of x ** y later.
Finally, press the q key to return to the Python interaction mode.
An additional information is shown above, that is, the pow function is equivalent to x ** y, and both calculate the power of y of x.
>>> 4**216>>> math.pow(4,2)16.0>>> 4*28
Note that 4 ** 2 and 4*2 are very different.
In a similar way, you can view the usage of any function in the math module.
I will not elaborate on the question of "functions" here, so let's take care of it and understand it as you have learned in mathematics. The following sections will be devoted to functions.
Below are some examples of functions in the commonly used math module. you can compare them with your own debugging.
>>> Math. sqrt (9) 3.0 >>> math. floor (3.14) 3.0 >>> math. floor (3.92) 3.0 >>> math. fabs (-2) # equivalent to abs (-2) 2.0> abs (-2) 2> math. fmod (5, 3) # equivalent to 5% 32.0> 5% 32
Several common functions
There are several commonly used functions. it doesn't matter if you can't remember them. if you know this, you can use google.
Absolute value
>>> abs(10)10>>> abs(-10)10>>> abs(-1.2)1.2
Rounding
>>> Round (1.234) 1.0 >>> round (1.234, 2) 1.23 >>># if you do not know how to use this function, you can use the following method to view help Information> 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.
Computing priority
From the beginning of elementary school mathematics, we will study the priority of calculation. for example, in the four arithmetic operations, "multiplication, division, and addition and subtraction" indicates that the priority of multiplication and division is higher than that of addition and subtraction.
For the same level, it is calculated in the order of "from left to right.
The following table lists the priorities of various operations in Python. However, in general, there is no need to remember, and we can fully understand it in mathematics. since humans have invented mathematics, there is no need to write a new set of standards for computing in computers, you only need to conform to the math.
Operator |
Description |
Lambda |
Lambda expressions |
Or |
Boolean "or" |
And |
Boolean "and" |
Not x |
Boolean "non" |
In, not in |
Member testing |
Is, is not |
Identity test |
<, <=,>, >= ,! =, = |
Comparison |
| |
By bit or |
^ |
Bitwise OR |
& |
Bitwise AND |
<,> |
Shift |
+ ,- |
Addition and subtraction |
*,/, % |
Multiplication, division, and remainder |
+ X,-x |
Plus or minus sign |
~ X |
Flip by bit |
** |
Index |
X. attribute |
Attribute reference |
X [index] |
Subscript |
X [index: index] |
Addressing segment |
F (arguments ...) |
Function call |
(Experession ,...) |
Bind or display tuples |
[Expression,...] |
List Display |
{Key: datum ,...} |
Dictionary display |
'Expression ,...' |
String conversion |
The above table lists all operators used in Python, which are listed in ascending order. Although many do not know what is going on, but first list it and use it later, you can also come back to view it.
Finally, we should mention the brackets in the operation. As long as there are brackets, calculate the brackets first. This is a consensus in mathematics and does not need to be explained.
The above is a summary of common mathematical functions in the math module of Python. For more information, see other related articles in the first PHP community!