Skip to main content

Python Float Data Type

1. Why Do We Need Float Data Types?

Up to this point, we were dealing with numbers like:

123
456
1000

These are whole numbers, meaning they do not contain any fractional or decimal part. Such values are perfectly handled using the integer (int) data type.

However, in real-world applications, numbers are not always so simple. Consider the following scenarios:

The price of diesel: ₹78.43 Currency conversion: ₹70.12 per dollar Scientific measurements: 9.81 (acceleration due to gravity)

These values clearly include a decimal point, and therefore cannot be represented using integers. This is where the float data type comes into the picture.

2. What is a Float in Python?

A floating point number (or simply float) is any number that contains a decimal point.

f = 123.456
print(type(f)) # <class 'float'>

This confirms that Python automatically identifies numbers with decimal points as float type values.

3. Creating Float Values

Creating a float in Python is straightforward. Any number written with a decimal point is treated as a float.

a = 1.23
b = 0.0
c = 100.5

Even if the decimal part is zero, Python still considers it a float:

x = 10.0
print(type(x)) # float

4. Important Rule: Float Values Are Always Decimal

This is one of the most critical concepts to understand. In integers, we saw that Python allows multiple representations:

a = 123 # Decimal
b = 0b1111 # Binary
c = 0o123 # Octal
d = 0x1A # Hexadecimal

All of the above are valid integer representations. However, this flexibility does NOT exist for float values.

Invalid Float Representations

f = 0b1.1011 # ❌ Invalid
f = 0o123.456 # ❌ Invalid
f = 0x123.456 # ❌ Invalid

These will result in syntax errors, because:

  • Binary, octal, and hexadecimal formats are only applicable to integers
  • Float values must always be written in decimal form only

This is a very important distinction that developers often overlook in the beginning.

5. Scientific Notation (Exponential Form)

Apart from the standard decimal representation, Python provides another powerful way to represent floating point numbers—scientific notation, also known as exponential form.

f = 1.2e3
print(f) # 1200.0
  • 1.2e3 means: 1.2 × 10³ = 1.2 × 1000 = 1200.0
  • You can also use uppercase E: f = 1.2E3

6. Why Scientific Notation is Useful

Scientific notation becomes extremely useful when dealing with very large or very small numbers, because it allows you to represent them in a compact and readable format.

Instead of writing 1200000000000000.0, you can write 1.2e15.

7. Internal Representation Insight

Although Python makes working with floats simple, internally it uses a system similar to the IEEE 754 floating-point representation, which stores numbers in binary format. This sometimes leads to minor precision issues.

# Example
print(0.1 + 0.2) # 0.30000000000000004

This happens due to how floating-point numbers are stored internally, and it is a normal behavior in most programming languages.