1. Numeric data types
There are several numeric types built-in and supported by Python.
2. Built in numeric types
Integers and floats are numeric types that are built into Python. Integers, int for short, are great for whole numbers and anytime you need to handle substantial values. Floats work for holding fractional amounts where an approximation is acceptable and scientific notation.
3. Decimals
Decimals are perfect for exacting precision and currency operations. To use Decimals, we need to import them from the decimal module as shown here. Notice a Decimal doesn't convert to scientific notation.
4. Printing floats
Working with floats can result in printing out scientific notation; however, f-strings can help us handle outputting those correctly.
For example, if there is a float like 0.00001 and you print it, you'll get back scientific notation of 1e-05. However, f-strings allow us to pass a format specifier to them to control the output by following the variable with a colon and a format specifier, which for floats is the letter f.
For example,
print(f"{0.00001:f}")
You will get the output of 0.000010.
5. Printing floats
However, if you try print(f"{0.0000001:f}"), you will get back
0.000000. It isn't what you probably expected, but it happens because the bare float format specifier stops at six decimal places. We can tell it what precision we want by adding a .X where X is the precision we want. For example print(f"{0.0000001:.7f}") will return 0.0000001
6. Python division types
Python supports two kinds of division, float division using a single backslash and floored division using a double backslash. For example, 4/2 will return 2.0; 4//2 will return 2 as it floors the result. This behavior is exemplified better with 7//3, which will also return 2.
7. Let's practice!
Let's go practice.