- Syntax
- Example 1: Positive Floating-Point Number
- Example 2: Negative Floating-Point Number
- Example 3: Zero as a Float
- Example 4: Arithmetic with Floats
- Example 5: Reading a Float from the User
- Example 6: Integer and Float Together
- Example 7: Floating-Point Precision
- Example 8: Comparing Floating-Point Numbers
- Example 9: Large Floating-Point Number
- Example 10: Float Without Leading Zero
- Common Mistakes
- Best Practices
- Key Points to Remember
A floating-point number, commonly called a float, is a number that contains a decimal point.
Python uses the built-in float data type to represent decimal values.
Floating-point numbers are commonly used for measurements, prices, percentages, scientific calculations, and any situation where fractional values are required.
Unlike integers, floating-point numbers have limited precision because they are stored using the IEEE 754 floating-point representation.
Syntax #
variable_name = floating_point_value
Components #
| Component | Description |
|---|---|
variable_name |
Name of the variable. |
= |
Assignment operator. |
floating_point_value |
A number containing a decimal point or written in scientific notation. |
Example 1: Positive Floating-Point Number #
main.py
price = 99.95
print(price)
print(type(price))
Output #
99.95
<class 'float'>
Explanation #
99.95contains a decimal point.- Python automatically stores it as a floating-point number.
- The
type()function confirms that the value belongs to thefloatdata type.
Example 2: Negative Floating-Point Number #
main.py
temperature = -18.5
print(temperature)
print(type(temperature))
Output #
-18.5
<class 'float'>
Explanation #
- Floating-point numbers can be positive or negative.
- The minus (
-) sign indicates a negative value. - The variable stores a value of type
float.
Example 3: Zero as a Float #
main.py
balance = 0.0
print(balance)
print(type(balance))
Output #
0.0
<class 'float'>
Explanation #
0.0represents zero as a floating-point number.- Although its value is zero, its data type is
float, notint.
Example 4: Arithmetic with Floats #
main.py
a = 5.5
b = 2.5
print(a + b)
print(a - b)
print(a * b)
print(a / b)
Output #
8.0
3.0
13.75
2.2
Explanation #
- Floating-point numbers support all arithmetic operations.
- The results are also floating-point numbers.
- Decimal values are preserved during calculations.
Example 5: Reading a Float from the User #
main.py
height = float(input("Enter your height: "))
print(height)
print(type(height))
Sample Input #
5.8
Output #
5.8
<class 'float'>
Explanation #
input()always returns a string.- The
float()function converts the string into a floating-point number. - The variable stores a value of type
float.
Example 6: Integer and Float Together #
main.py
marks = 90
bonus = 2.5
total = marks + bonus
print(total)
print(type(total))
Output #
92.5
<class 'float'>
Explanation #
- One operand is an integer.
- The other operand is a floating-point number.
- Python automatically converts the integer to a float before performing the addition.
- The result is a floating-point number.
Example 7: Floating-Point Precision #
main.py
print(0.1 + 0.2)
Output #
0.30000000000000004
Explanation #
- Many decimal numbers cannot be represented exactly in binary.
- Therefore, very small rounding errors may occur.
- This is a normal characteristic of floating-point arithmetic and not a Python error.
Example 8: Comparing Floating-Point Numbers #
main.py
a = 5.75
b = 3.25
print(a > b)
print(a == b)
Output #
True
False
Explanation #
- Floating-point numbers can be compared using comparison operators.
- Since
5.75is greater than3.25, the first expression returnsTrue. - The two values are different, so the equality comparison returns
False.
Example 9: Large Floating-Point Number #
main.py
distance = 9876543210.12345
print(distance)
print(type(distance))
Output #
9876543210.12345
<class 'float'>
Explanation #
- Floating-point numbers can represent very large values.
- However, unlike integers, they have limited precision.
- Extremely large or highly precise decimal values may lose some accuracy.
Example 10: Float Without Leading Zero #
main.py
value = .75
print(value)
print(type(value))
Output #
0.75
<class 'float'>
Explanation #
- Python allows decimal numbers to be written without a leading zero.
- Internally,
.75is treated as0.75. - For better readability, many programmers prefer writing
0.75.
Common Mistakes #
1. Forgetting to Convert User Input #
Incorrect
price = input("Enter price: ")
print(price + 5)
Output #
TypeError: can only concatenate str (not "int") to str
Reason
input() returns a string.
Correct
price = float(input("Enter price: "))
print(price + 5)
2. Expecting Exact Decimal Precision #
Incorrect
print(0.1 + 0.2 == 0.3)
Output #
False
Reason
Floating-point numbers may contain tiny rounding errors due to their binary representation.
3. Assuming 5 and 5.0 Have the Same Data Type #
Incorrect Assumption
print(type(5))
print(type(5.0))
Output #
<class 'int'>
<class 'float'>
Reason
5 is an integer, while 5.0 is a floating-point number.
4. Using Commas in Numeric Values #
Incorrect
price = 1,234.56
Reason
Python interprets this as a tuple, not as a floating-point number.
Correct
price = 1234.56
or
price = 1_234.56
Best Practices #
- Use
floatwhenever decimal values are required. - Convert user input using
float()before performing calculations. - Avoid comparing floating-point numbers directly when exact precision is required.
- Use a leading zero before the decimal point (for example,
0.75instead of.75) to improve readability. - Use numeric underscores to improve the readability of large floating-point values.
Key Points to Remember #
- Floating-point numbers are represented by the
floatdata type. - Floats store numbers with decimal points.
- Floats support all arithmetic operations.
- Python automatically converts integers to floats during mixed arithmetic operations.
- Floating-point values have limited precision.
- Small rounding errors are normal in floating-point arithmetic.
- Use
float()to convert strings into floating-point numbers. - Use
0.75instead of.75for better code readability.