One, Python data types

1. An overview of data types

Insert picture description here

2. Integer type

Insert picture description here

  • Different representations of integers

Insert picture description here

  • Convert integers to strings of different bases

Insert picture description here

  • Creation of integers

Insert picture description here
From the above figure, we can see that passing the string '118' to the int function can also return the integer 118.

When passing two parameters, the first parameter must be a string, and the second parameter is used to specify the base.
Insert picture description here

3. Floating point type

  • Floating point creation

Insert picture description here
As shown in the figure above, passing in a string of '118' can also convert a string to the corresponding floating point number.

  • Represent floating-point numbers in scientific notation

Insert picture description here

  • The imprecision of floating-point number storage

As you all know, computers use binary for storage. When computers use binary to store floating-point numbers, they are inaccurate and there may be errors. Therefore, we need to be extra careful with floating-point operations.
Insert picture description here
We found that a very small number expressed in scientific notation was printed, not 0.
So how can we get accurate results? At this time, we must rely on some modules provided by Python, which in turn provide us with some useful classes, or methods, and functions.
First, we need to import the Decimal class in the decimal module:
Insert picture description here
In addition to the decimal module, we can also use another module fractions to import the Fraction class in the fractions module:
Insert picture description here
How to represent 1.1? We can use Fraction(11, 10 ) To represent;
how to represent 2.2, we can use Fraction(22, 10) to represent;
how to represent 2.2, we can use Fraction(33, 10) to represent.

In this way, we use the two modules of decimal or fractions to perform accurate calculations of floating-point numbers.

4. Boolean type

Insert picture description here

Guess you like

Origin blog.csdn.net/zhaopeng01zp/article/details/109322883