1. An overview of data types
2. Integer type
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.
3. Floating point type
As shown in the figure above, passing in a string of '118' can also convert a string to the corresponding floating point number.
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.
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:
In addition to the decimal module, we can also use another module fractions to import the Fraction class in the fractions module:
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.