Math module and how to use it

Source: Internet
Author: User
Tags cos sin

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:

?
1 >>> importmath

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

?
12 >>> math.pi3.141592653589793

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

?
12 >>> 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.

?
1 >>> help(math.pow)

Enter the instructions above in interactive mode, then enter to see the following information:

?
1234567 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.

?
123456 >>> 4**216>>> math.pow(4,2)16.0>>> 4*28

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.

?
1234567891011121314 >>> 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%32.0>>> 5%32

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

?
123456 >>> abs(10)10>>> abs(-10)10>>> abs(-1.2)1.2

Rounded

?
1234567 >>> round(1.234)1.0>>> round(1.234,2)1.23>>> # 如果不清楚这个函数的用法,可以使用下面方法看帮助信息>>> help(round)
?
1234567 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.

Math module and how to use it

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.