Detailed description of the math module in the basic python tutorial

Source: Internet
Author: User
This article mainly introduces pythonr's knowledge about the digital processing module (math). For more information, see section 1. math.

The Code is as follows:


>>> Import math
>>> Dir (math) # You can view the list of all function names.
>>> Help (math) # view the definition and function 0 prototype

2. Common functions

The Code is as follows:


Ceil (x) Top
Floor (x) Base fetch
Fabs (x) returns the absolute value.
Factorial (x) factorial
Hypot (x, y) sqrt (x * x + y * y)
Power y of pow (x, y) x
Sqrt (x) Square
Log (x)
Log10 (x)
Trunc (x) truncation integer
Isnan (x) determines whether NaN (not a number)
Degree (x) radians Rotation Angle
Radians (x) angle to radian

In addition, this module defines two constants:

The Code is as follows:


E = 2.718281828459045
Pi = 1, 3.141592653589793

Random

1. Introduction

Random is used to generate random numbers. We can use it to randomly generate numbers or select strings.

The Code is as follows:


Import random

2. Common functions

Random. random ()
Used to generate a random floating point number: range [0.0, 1.0)

The Code is as follows:


>>> Import random
>>> Random. random ()
0.999410896951364
Random. uniform (a, B)

Generates a random floating point number in the specified range. a and B are upper and lower limits.

As long as! = B, a floating point number between the two is generated. If a = B, the generated floating point number is

The Code is as follows:


>>> Random. uniform (10, 20)
13.224754825064881
>>> Random. uniform (20, 10)
14.104410713376437
>>> Random. uniform (10, 10)
10.0

Random. randint (a, B)
Generates an integer in the specified range. a is the lower limit, B is the upper limit, and a <= n <= B;

If a = B, n = a. If a> B, an error is returned.

The Code is as follows:


>>> Random. uniform (10, 10)
10.0
>>> Random. randint (10, 20)
15
>>> Random. randint (10, 10)
10
>>> Random. randint (20, 10)
Traceback (most recent call last ):
......
ValueError: empty range for randrange () (20, 11,-9)

Random. randrange ([start], stop, [, step])
Obtains a random number from a set that increments by the specified base number within the specified range. The default value of the base number is 1.

The Code is as follows:


>>> Random. randrange (10,100, 5)
95
>>> Random. randrange (10,100, 5)
45

Random. choice (sequence)
Obtain a random element from the sequence. The sequence parameter indicates an ordered type. It is not a specific type. It generally refers to list, tuple, string, and so on.

The Code is as follows:


>>> Random. choice ([1, 2, 3, 4])
1
>>> Random. choice ([1, 2, 3, 4])
3
>>> Random. choice ('hello ')
'E'

Random. shuffle (x [, random])
Used to disrupt elements in a list

The Code is as follows:


>>> A = [1, 2, 3, 4, 5]
>>> Random. shuffle ()
>>>
[4, 5, 2, 1, 3]
>>> Random. shuffle ()
>>>
[3, 2, 5, 1, 4]

Random. sample (sequence, k)
Returns k elements from a specified sequence as a segment. The sample function does not modify the original sequence.

The Code is as follows:


>>> A = [1, 2, 3, 4, 5]
>>> Random. sample (a, 3)
[1, 4, 5]
>>> Random. sample (a, 3)
[1, 2, 5]
>>>
[1, 2, 3, 4, 5]

Decimal

1. Introduction

By default, floating point mathematics lacks accuracy.

The decimal module provides a Decimal data type for floating point calculation. Compared with the built-in binary floating point number, float is helpful.

Financial applications and other scenarios that require precise decimal expression,
Control precision,
Control rounding to adapt to legal or regulatory requirements,
Make sure that the decimal digit is accurate or that the calculation result is consistent with the manual calculation result.
Decimal reproduce the manual mathematical operation, which ensures that the binary floating point cannot accurately preserve the data accuracy. High Precision enables Decimal to execute modulo operations and equivalent tests that are not supported by binary floating point numbers.

2. Use

The Code is as follows:


>>> From decimal import Decimal
>>> Decimal ('0. 1')/Decimal ('0. 3 ')
Decimal ('0. 3333333333333333333333333333 ')

>>> From decimal import getcontext
>>> Getcontext (). prec = 4 # set global precision
>>> Decimal ('0. 1')/Decimal ('0. 3 ')
Decimal ('0. 3333 ')

Fractions
Score type

Structure

The Code is as follows:


>>> From fractions import Fraction
>>> Fraction (16,-10) # denominator
Fraction (-8, 5)
>>> Fraction (123) # molecule
Fraction (123, 1)

>>> Fraction ('123') # string score
Fraction (3, 7)

>>> Fraction ('-. 100') # string floating point number
Fraction (-1, 8)

>>> Fraction (2.25) # Floating Point Number
Fraction (9, 4)

>>> From decimal import Decimal
>>> Fraction (Decimal ('1. 1') # Decimal
Fraction (11, 10)

Computing

The Code is as follows:


>>> From fractions import Fraction
>>> A = Fraction (1, 2)
>>>
Fraction (1, 2)
>>> B = Fraction ('20140901 ')
>>> B
Fraction (1, 3)
>>> A + B
Fraction (5, 6)
>>> A-B
Fraction (1, 6)

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.