Summary of common mathematical functions in Python's math module

Source: Internet
Author: User
Tags bitwise cos mathematical functions
This article mainly introduces the mathematical functions of the math module in Python, and also makes a list of operator priority, the need for friends can refer to the following





In mathematics, in addition to subtraction arithmetic-this is elementary math-there are many other operations such as exponentiation, Root, logarithmic, and so on, a module in Python is needed to achieve these operations: math



Module is a very important thing in Python and you can interpret it as a python extension tool. In other words, Python provides some of the things that are available by default, but these are not provided by default to meet the needs of programming practices, and some other tools have been specifically created. These tools are called "modules"



Any pythoner can write the modules and put them on the web for others to use.



After the installation of Python, there are some modules installed by default, this is called "Standard library", "Standard library" module does not need to install, you can directly use.



If no modules are included in the standard library, they need to be installed before they can be used. The installation method of the module, I particularly recommend the use of PIP to install. Just mention here, the back will be dedicated to tell, the impatient crossing can own Google.
Using the Math module
The math module is in the standard library, so you don't have to install it, you can use it directly. Use this method:





>>> Import Math


Using Import to reference the Math module, you can use the tool provided by this module. For example, to get PI:





>>> math.pi3.141592653589793


What can be done with this module? You can see it in the following ways:





>>> 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']


The Dir (module) is a very useful instruction that can be used to view the tools contained in any module. As you can see from the list above, in the math module, you can calculate the positive sin (a), cos (a), sqrt (a) ...



These are what we call functions, that is, the functions of various computations are provided in module math, such as the calculation of powers, which can be used with the POW function. But, how to use it?



Python is a very thoughtful girl, she has already provided a command, let us see how each function is used.





>>> Help (Math.pow)


Enter the instructions above in interactive mode, then enter to see 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).


This shows the use of the POW function in the Math module and the related instructions.



The first line means that this is the built-in function of the Math module. POW Help information (so-called built-in, called the built-in function, is that this function is Python by default)
The third line, which represents the function's arguments, is two, and is the function's calling method
Line four, which is the description of the function, returns the result of the x**y, and explains the meaning of x**y later.
Finally, press the Q key to return to Python interactive mode
An additional message from the above is that the POW function and the x**y are equivalent, both of which are computed for x's Y-times.





>>> 4**2
16
>>> math.pow(4,2)
16.0
>>> 4*2
8


In particular, 4**2 and 4*2 are very much different.



In a similar way, you can see how any one of the functions in the math module is used.



About the "function" of the problem, here do not do in-depth elaboration, the caretaker in accordance with their own in mathematics learned to understand. Later, there will be chapters devoted to the study of functions.
Here are some examples of functions commonly used in the math module, crossing can be compared with their 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% 3
2.0
>>> 5% 3
2


Several common functions
There are a few commonly used functions, listed, if not remember it does not matter, know that there is good, when using Google.



Seek absolute value


>>> abs(10)
10
>>> abs(-10)
10
>>> abs(-1.2)
1.2


Rounded


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

>>> # If you don't know the usage of this function, you can use the following methods to see the 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.



Operation Priority
Starting from elementary mathematics, we study the priority of operations, such as "first multiplication, then plus minus" in arithmetic, which indicates that multiplication and division have higher priority than addition and subtraction.



For the same level, it is calculated in order from left to right.



The order of precedence for the various operations in Python is listed in the table below. However, in general, do not need to remember, can be in accordance with the mathematics to understand, because mankind has already invented mathematics, in the computer to do the operation does not need to write a new set of norms, just to conform to the mathematics can.


Operator Describe
Lambda Lambda expression
Or Boolean "or"
and Boolean "and"
Not X Boolean "Non"
In,not in Member Test
Is,is not Identity testing
<,<=,>,>=,!=,== Comparison
| Bitwise OR
^ Bitwise XOR OR
& Bitwise-AND
<<,>> Shift
+,- Addition and subtraction
*,/,% Multiplication, division and redundancy
+x,-x PLUS sign
~x Rollover by bit
** Index
X.attribute Property Reference
X[index] Subscript
X[index:index] Addressing segment
F (Arguments ...) Function call
(Experession,...) Binding or tuple display
[Expression,...] List display
{Key:datum,...} Dictionary display
' Expression,... ' String conversions



The table above lists the operators used in Python, listed in order from low to high. Although there are many still do not know what is going on, but first listed, and so on later use, can also come back to view.



Finally, we want to mention the lore in the operation: parentheses. As long as there are parentheses, the parentheses are calculated first. This is a consensus in mathematics, and there is no need to explain.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.