In Python, numeric data is primarily represented by two built‑in types: integers (int) for whole numbers and floating‑point numbers (float) for values that include fractional components. Although the distinction appears simple, professional Python development requires a precise understanding of how these types interact, how to perform safe conversions (casting), and how to manage floating‑point precision issues that can affect correctness and presentation.
In this tutorial, you’ll learn:
- What happens when you use
intandfloattogether in expressions - How type conversion (also called casting) works in Python
- Why
int(8.9)becomes8(and why that can surprise you) - How to use
round()correctly - Why floats sometimes produce “weird” results like
0.19999999999999996
Int & Float
An int is a whole number with no decimal point, like 20, -7, or 256.
A float is a number with a decimal point (or scientific notation), like 5.0, 3.14, or 1e-3.
In Python, these are not just “formats”—they are built‑in classes (types). That matters because conversion uses these class names.
Try this in Python:
type(20)
type(5.0)
Output : You should see something like
<class 'int'>
<class 'float'>
Code language: HTML, XML (xml)
[Checking types of 20 and 5.0 in Python]
What happens when you mix int and float?
When you perform math, Python tries to avoid losing information. The general rule is:
If an expression contains a float, Python usually returns a float.
Division always returns a float
Look at this division:
20 / 4Even though both numbers are integers and the result is a whole number, Python returns:
5.0Python does this because division can easily produce non‑whole results (like 20 / 3), and returning a float consistently avoids surprises.
If you specifically want integer division, use //:
20 // 4
20 // 3
The output will be:
5
6
Notice how 20 // 3 doesn’t round—it drops the decimal part.
[/ vs // outputs]
Adding, multiplying, exponentiation with mixed types
If you combine an int and a float, the result becomes a float:
10 + 2.5
10 * 2.5
2 ** 8
2.0 ** 8
The presence of 2.0 in the last example makes the result a float.
This behavior is called type promotion: Python promotes the “smaller/safer” type (int) into the “wider” type (float) to preserve information.
Converting between int and float
int and float are classes
When you write:
int(256.0)
You are calling the int class to create an integer value from another value.
Similarly:
float(256)
creates a float from an integer.
Programmers often call this conversion process casting.
[ Casting float to int and int to float]
The big int() surprise: it does NOT round
A common mistake is assuming int() rounds to the nearest integer. It does not.
Instead, int() truncates—it removes everything after the decimal point.
int(8.9)
int(8.99999)
Code language: CSS (css)
Both produce:
8
That number might be extremely close to 9, but it’s still 8 after truncation.
This is important when a value is close to the next integer:
int(4.999999)
Code language: CSS (css)
This becomes 4, not 5.
So whenever you need “nearest integer” behavior, don’t use int() directly.
round()
Python provides round() when you actually want rounding behavior:
round(4.999999)
This returns:
5
Rounding to a fixed number of decimal places
You can also pass a second argument to round to a certain number of decimal places:
round(4.666666, 2)
round(4.666666, 1)This returns:
4.67
4.7Code language: CSS (css)
This is especially useful for displaying results cleanly.
[Using round() with and without decimal places]
The classic float pitfall: “Why is this not exactly 0.2?”
Try this:
1.2 - 1.0You might expect 0.2, but Python can display something like:
0.19999999999999996
Code language: CSS (css)
This is not a Python bug. It’s a property of how floats work in almost all programming languages.
Why it happens
Floats are stored in memory using binary (1s and 0s). Many decimal values (like 0.1 or 1.2) cannot be represented perfectly in binary with finite space.
So floats are approximations. That approximation is usually extremely close, but sometimes it shows up in calculations.
You may notice something similar when printing repeating decimals:
14 / 3Mathematically, 14/3 is 4.666666... repeating forever. A float can only store a limited approximation, so you’ll see a value that stops after some digits.
How to handle float precision safely
When you’re doing everyday calculations, floats are usually fine. But you should be careful in situations where exact decimal precision is important.
Use rounding when presenting results
If you’re showing results to users (like prices or measurements), rounding is often enough:
value = 1.2 - 1.0
round(value, 2)Avoid direct equality checks with floats
Instead of:
(1.2 - 1.0) == 0.2Use tolerance‑based comparison:
import math
math.isclose(1.2 - 1.0, 0.2)Code language: CSS (css)
This checks whether two float values are close enough within a reasonable tolerance.
Money and fixed decimals: prefer decimal.Decimal
For financial calculations, you usually want decimal math where the number of decimal places is known (like two decimals for currency).
Python provides the decimal module for this purpose. You don’t need to master it right now, but it’s good to know it exists:
from decimal import Decimal
Decimal('1.20') - Decimal('1.00')This gives an exact decimal result.
In an upcoming tutorial, you can go deeper into decimals and money‑safe calculations.
[ Float subtraction vs Decimal subtraction]
Practice exercises
Try these in your Python shell or Jupyter notebook and observe the results. Don’t just run them—predict first, then confirm.
# 1) Predict the output type
print(type(10 + 2.5))
print(type(10 * 2))
print(type(10 / 2))
# 2) Casting surprises
print(int(9.99999))
print(round(9.99999))
# 3) Float precision
print(0.3 - 0.1)
print(round(0.3 - 0.1, 2))
# 4) Integer division
print(7 // 2)
print(7 / 2)
