Python Basic Tutorial Digital Processing (math) module detailed

Source: Internet
Author: User
1.math Introduction

The code is as follows:


>>> Import Math
>>>dir (Math) #这句可查看所有函数名列表
>>>help (Math) #查看具体定义及函数0原型

2. Common functions

The code is as follows:


Ceil (x) Fetch top
Floor (x) Bottom
Fabs (x) Take absolute value
Factorial (x) factorial
Hypot (x, y) sqrt (x*x+y*y)
The Y-square of the POW (x, y) x
sqrt (x) Open Square
Log (x)
LOG10 (x)
Trunc (x) truncation of rounding parts
isNaN (x) determines whether Nan (not a number)
Degree (x) radian Turn angle
radians (x) angle to radians

In addition, the module defines two constants:

The code is as follows:


E = 2.718281828459045
PI = 3.141592653589793

Random

1. Introduction

The random is used to generate randomly generated numbers, and we can use it to randomly generate a number or select a string

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)

Used to generate a random floating-point number within a specified range, A/b is the upper and lower bounds

As long as the a!=b, will generate a floating point between the two, if a=b, the resulting floating point is a

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)
Used to generate integers in a specified range, a is the lower bound, and B is the upper bound, resulting in a random integer a<=n<=b;

If a=b, then n=a; if a>b, error

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):
......
Valueerror:empty range for Randrange () (20,11,-9)

Random.randrange ([start], stop, [, step])
Gets a random number in the collection that increments by the specified cardinality from the specified range, and the base default value is 1

The code is as follows:


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

Random.choice (Sequence)
Gets a random element from the sequence, the parameter sequence represents an ordered type, not a specific type, refers to List,tuple, a string, etc.

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 (a)
>>> A
[4, 5, 2, 1, 3]
>>> Random.shuffle (a)
>>> A
[3, 2, 5, 1, 4]

Random.sample (sequence, k)
A random fetch of k elements from a specified sequence is returned as a fragment, and 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]
>>> A
[1, 2, 3, 4, 5]

Decimal

1. Introduction

Default, floating point math lacks precision

The decimal module provides a decimal data type for floating-point calculation. This type of float is implemented in comparison to the built-in binary floating-point numbers to help

Financial applications and other occasions where precise decimal expressions are required,
Control accuracy,
To control rounding to suit legal or regulatory requirements,
Make sure that decimal digits are accurate, or where the user wants to calculate the results in accordance with the hand calculation.
Decimal reproduces the manual mathematical operations, which ensures that the data accuracy of the binary floating point numbers cannot be accurately maintained. High precision enables Decimal to perform modulo operations and equivalence tests that cannot be performed 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 #设置全局精度
>>> decimal (' 0.1 ')/decimal (' 0.3 ')
Decimal (' 0.3333 ')

Fractions
Fraction type

Structure

The code is as follows:


>>> from fractions import fraction
>>> fraction ( -10) #分子分母
Fraction (-8, 5)
>>> fraction (123) #分子
Fraction (123, 1)

>>> fraction (' 3/7 ') #字符串分数
Fraction (3, 7)

>>> fraction ('-.125 ') #字符串浮点数
Fraction (-1, 8)

>>> Fraction (2.25) #浮点数
Fraction (9, 4)

>>> from decimal Import decimal
>>> Fraction (Decimal (' 1.1 ')) #Decimal
Fraction (11, 10)

Calculation

The code is as follows:


>>> from fractions import fraction
>>> A = fraction
>>> A
Fraction (1, 2)
>>> B = fraction (' 1/3 ')
>>> 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.