The Python Basics Tutorial Digital processing (math) module detailed _python

Source: Internet
Author: User
Tags shuffle


1.math Introduction

Copy Code code as follows:

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

2. Common functions

Copy Code code as follows:

Ceil (x) Take top
Floor (x) Bottom
Fabs (x) Take absolute value
Factorial (x) factorial
Hypot (x,y) sqrt (x*x+y*y)
POW (x,y) x's Y-second square
sqrt (x) Open Square
Log (x)
LOG10 (x)
Trunc (x) truncation of integral parts
isNaN (x) to determine whether Nan (not a number)
degree (x) radians Turn angle
radians (x) angle to radians

In addition, the module defines two constants:

Copy Code code as follows:

E = 2.718281828459045
PI = 3.141592653589793

Random

1. Introduction

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

Copy Code code as follows:

Import Random

2. Common functions

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

Copy Code code 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 to the upper and lower

As long as the a!=b, you will generate a floating-point number between the two, if a=b, then the generated floating-point number is a

Copy Code code 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 a specified range of integers, a is the lower bound, B is the upper bound, a random integer generated a<=n<=b;

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

Copy Code code 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])
Gets a random number from the specified range, in a collection that is incremented by the specified cardinality, with a cardinality default of 1

Copy Code code as follows:

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

Random.choice (Sequence)
Gets a random element from the sequence, and the parameter sequence represents an ordered type, not a particular type, generally referring to list,tuple, strings, etc.

Copy Code code 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

Copy Code code 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)
Randomly gets k elements from a specified sequence to return as a fragment, and the sample function does not modify the original sequence

Copy Code code 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

By default, floating-point math lacks precision

The decimal module provides a decimal data type for floating-point calculations. This type of float, compared to the built-in binary floating-point numbers, helps

Financial applications and other occasions where precise decimal expressions are required,
Control accuracy,
Control rounding to suit legal or prescribed requirements,
Make sure that decimal digits are accurate, or that the user expects the results to match the hand.
Decimal reproduces the manual mathematical operations, which ensures that binary floating-point numbers do not accurately retain the data precision. High precision enables Decimal to perform modulo operations and equivalent tests that cannot be performed by binary floating-point numbers.

2. Use

Copy Code code 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

Copy Code code 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 (' 1.1 ') #Decimal
Fraction (11, 10)

Calculation

Copy Code code as follows:

>>> from fractions import fraction
>>> a = fraction (1,2)
>>> A
Fraction (1, 2)
>>> B = fraction (' 1/3 ')
>>> b
Fraction (1, 3)
>>> A + b
Fraction (5, 6)
>>> A-b
Fraction (1, 6)

Related Article

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.