Python gives you two everyday number types you’ll use constantly: integers (int) and floating‑point numbers (float). At first they look simple—whole numbers vs. decimal numbers—but the real learning happens when you start mixing them, converting between them, and dealing with floating‑point precision.
In this tutorial, you’ll learn:
int and float together in expressionsint(8.9) becomes 8 (and why that can surprise you)round() correctly0.19999999999999996int and float in Python?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)
Code language: CSS (css)
You should see something like:
<class 'int'>
<class 'float'>
Code language: HTML, XML (xml)
[Checking types of 20 and 5.0 in Python]
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.
Look at this division:
20 / 4
Even though both numbers are integers and the result is a whole number, Python returns:
5.0
Code language: CSS (css)
Python 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
Code language: JSON / JSON with Comments (json)
The output will be:
5
6
Notice how 20 // 3 doesn’t round—it drops the decimal part.
[/ vs // outputs]
If you combine an int and a float, the result becomes a float:
10 + 2.5
10 * 2.5
2 ** 8
2.0 ** 8
Code language: CSS (css)
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.
int and float int and float are classes (and you use them like constructors)
When you write:
int(256.0)
Code language: CSS (css)
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]
int() surprise: it does NOT roundA 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)
Code language: CSS (css)
This returns:
5
You can also pass a second argument to round to a certain number of decimal places:
round(4.666666, 2)
round(4.666666, 1)
Code language: CSS (css)
This returns:
4.67
4.7
Code language: CSS (css)
This is especially useful for displaying results cleanly.
[Using round() with and without decimal places]
Try this:
1.2 - 1.0
Code language: CSS (css)
You 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.
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 / 3
Mathematically, 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.
When you’re doing everyday calculations, floats are usually fine. But you should be careful in situations where exact decimal precision is important.
If you’re showing results to users (like prices or measurements), rounding is often enough:
value = 1.2 - 1.0
round(value, 2)
Instead of:
(1.2 - 1.0) == 0.2
Use 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.
decimal.DecimalFor 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')
Code language: JavaScript (javascript)
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]
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)
Code language: PHP (php)
[Exercise results in Jupyter Notebook]
Now that you know how int and float interact—and why floats can behave unexpectedly—you’re ready to explore decimal‑safe approaches for cases like money and fixed precision.
In the next tutorial, you’ll learn better strategies for working with decimals when exactness matters.