Article From:https://www.cnblogs.com/chaoguo1234/p/9127658.html

Integer Integer

In Python2.X, there are two types of Integer, one is the common type of 32bit, the other is an unrestricted long type, which identifies the L or L behind a number to identify the long type, and when 32bit occurs, the overflow can not be expressed.It will automatically convert into long type.

In Python3.X, Integer has only one type, that is, long type. Therefore, the L or L logo can not be written later.

Floating-point number Float

In standard CPython, the number of floating-point is implemented in the double type, and what kind of results is the result when an integer is operated with a floating point number. The answer is the floating-point type. When doing arithmetic, Python converts operands upwards to more.The complex type and then the calculation. In Python, the type of floating-point number is more complex than the Integer type, and complex Complex is more complex than the floating point number (if Decimal and Fraction are taken into account, then Decimal and Fraction are compared.Integer is complex, and floating point numbers are more complex than Decimal and Fraction. Decimal and Fraction can not work together. Python will report errors.

Fixed-point number Decimal

1 The generation of fixed-point number

The fixed-point number is generated by the Decimal class:

```from decimal import Decimal
Decimal("0.1") + Decimal("0.10") + Decimal("0.2")
#The run result is Decimal ("0.40")```

In the above example, the Decimal operation automatically performs the precision conversion, and the accuracy of the operation result is the same as that of the arithmetic item with the greatest accuracy.

In Python2.7, Python3.X, you can also generate fixed-point numbers from floating point numbers.

```Decimal(0.1) + Decimal(0.1) + Decimal(0.1) - Decimal(0.3)
#Operation result: Decimal ('2.775557561565156540423631668E-17')```

Because of the problem of floating-point number representation, a large fixed point number may be generated at the end. In order to avoid this situation, the precision of the fixed point number can be specified manually.

2 Precision of fixed-point number

The default precision of the fixed-point number is 28 bits, which can be set manually, and once set, the accuracy of all subsequent fixed-point numbers created in the current thread is the same. The method of setting the precision is to use the decimal.getcontext method.

```#Before setting precision
decimal.Decimal(1) / decimal.Decimal(7)
####Running result
####Decimal('0.1428571428571428571428571429')

#After setting precision
decimal.getcontext().prec = 4
decimal.Decimal(1) / decimal.Decimal(7)
####Running result
####Decimal('0.1429')```

Rational number Fraction

1 The formation of rational numbers

The rational number is generated by the Fraction class:

```from fractions import Fraction
Fraction(1, 3) + Fraction(1, 3)
#Running results Fraction (2, 3)```

Rational numbers can also be generated from floating point strings.

```Fraction("1.333")
#Running results Fraction (1333, 1000)```

2 The number of floating-point numbers is converted to rational number

When a floating point number is converted to a rational number, there may be loss of accuracy. At this time, the maximum value of the molecule can be set, so as to produce the desired results.

```a = (4.0 / 3).as_integer_ratio()
a = Fraction(*a)
#The operation result is Fraction (22517998136852479, 13510798882111488).

a.limit_denominator(10)  #Set the maximum value of a molecule
#The running result is Fraction (4, 3).```

Boolean

PythonThe Boolean type bool in the Boolean contains only two values, True and False, and True and False are essentially 1 and 0, but they are also two instances of the bool type, and bool type is a subclass of the int type, so there will be the following phenomenon:

```isinstance(True, int)
#Run result True

True == 1
#Run result True

True is 1
#Run result False

True + 4
#Running result 5```