Summary of commonly used mathematical functions in Python's math module

高洛峰
Release: 2017-03-13 15:37:03
Original
3177 people have browsed it

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 (

module) is a very important thing in Python. You can think of it as an extension tool for Python. In other words, Python provides some usable things by default, but these provided by default are far from meeting the needs of programming practice, so someone has specially made some other tools. These tools are called "modules"

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
Copy after login

Use import to reference the math module, and then you can use the tools provided by this module. For example, to get the pi:


>>> math.pi
3.141592653589793
Copy after login

What can this module do? It can be seen with 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']
Copy after login

dir(module) is a very useful command through which you can view the tools contained in any module. As can be seen from the above list, in the math module, you can calculate positive sin(a), cos(a), sqrt(a)...

We call these functions , that is, the module math provides various calculation functions, such as calculating exponentiation, you can use the pow function. But how to use it?

Python is a very thoughtful girl. She has already provided a command that allows us to see how to use each function.


>>> help(math.pow)
Copy after login

Enter the above command in interactive

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).
Copy after login

Here shows the usage and related instructions of the pow function in the math module.

The first line means that here is the help information of the built-in function pow of the math module (the so-called built-in is called a built-in function, which means that this function is included in Python by default)

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
Copy after login

Please note that there is a big difference between 4**2 and 4*2.

Using a similar method, you can view the usage of any function in the math module.

Regarding the issue of "function", I will not elaborate on it in depth here. I will just understand it based on what I have learned in mathematics. There will be a chapter dedicated to functions later.

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
Copy after login


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
Copy after login
Rounding

>>> round(1.234)
1.0
>>> round(1.234,2)
1.23

>>> # 如果不清楚这个函数的用法,可以使用下面方法看帮助信息
>>> help(round)
Copy after login

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.
Copy after login


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".

The following

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.

##|Bitwise OR^Bitwise XOR&Bitwise AND<<,>>Shift##+, -*,/,%+x,-x~x**x.attributex[index]x [index:index]f(arguments...)(experession,...)[expression,...]{key:datum,...}'expression,...'String
Operator
Description
lambda Lambda Expression
or Boolean "or"
and Boolean "AND"
not x Boolean "NOT"
in, not in Member Test
is, is not Identity Test
<, <=, >, >=, !=, == Compare
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
Conversion
#The above table lists all the operators used in Python, yes Listed in order from lowest to highest. Although there are many that I still don’t know how to use, I’ll list them first so that I can come back and check them out when I need them later.


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template