A float is a number with a decimal point. Python uses the float data type to store decimal numbers. Floats are commonly used to represent prices, heights, temperatures, and measurements.
Store floats in variables:
Check the Data Type
To verify that a value is a float, use the built-in type() function. If it outputs <class 'float'>, the value is a float.
Using Floats in Calculations
You can perform mathematical operations with floats. For example, you can add, subtract, multiply, and divide them.
You can also use variables in mathematical operations:
Scientific Notation
Python supports scientific notation for writing very large or very small decimal numbers.
1.5e6means1.5 × 10⁶.
7.2e-3means7.2 × 10⁻³.
Scientific notation makes long numbers easier to write and read.
Floats vs Integers
Integers and floats are different data types.
Although 25 and 25.0 represent the same numeric value, Python stores them as different data types.
Floating-Point Precision
When you add two float numbers in Python, the output can be different from normal math.
For example, 0.1 + 0.2 should be 0.3. However, Python prints 0.30000000000000004.
Try:
This happens because computers store numbers using binary (0s and 1s), not the decimal system. Because of this, some decimal numbers, such as 0.1 and 0.2, cannot be stored exactly as they are.
Note: This is not a bug in Python. This is standard behavior in almost all programming languages.