Table of Contents
Integer and Floating-Point Numbers in Python
In Python, numbers mainly come in two basic types:
int— whole numbers (integers)float— decimal numbers (floating-point numbers)
You’ll use these constantly for calculations, counting, and measurements.
Integer numbers: `int`
An int represents whole numbers, both positive and negative, including zero.
Examples of int values:
05-31002025
Python automatically treats these as int when you write them in your code:
a = 10
b = -4
c = 0
print(a, type(a))
print(b, type(b))
print(c, type(c))Integers:
- Have no decimal point.
- Can be very large (Python automatically handles big integers; you don’t need to worry about size for normal use).
Floating-point numbers: `float`
A float is a number with a decimal point, or a number written in scientific notation.
Examples of float values:
3.140.0-2.51.05e3(which means $5 \times 10^3 = 5000.0$)2.5e-3(which means $2.5 \times 10^{-3} = 0.0025$)
Python treats any number with a decimal point as a float:
x = 3.14
y = -0.5
z = 2.0
print(x, type(x))
print(y, type(y))
print(z, type(z))Scientific notation examples:
a = 5e3 # 5000.0
b = 2.5e-3 # 0.0025
print(a, type(a))
print(b, type(b))
Note: Even if a float ends with .0 (like 2.0), it is still a float, not an int.
How Python chooses `int` vs `float`
Python decides the type from how you write the number:
- No decimal point →
int - Has a decimal point →
float - Scientific notation with
eorE→float
a = 5 # int
b = 5.0 # float
c = 5. # float
d = 0.5 # float
e = 5e0 # float
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))Mixing `int` and `float` in calculations
When you combine int and float in an arithmetic operation, Python usually converts the result to float to avoid losing decimal information.
a = 5 # int
b = 2.0 # float
print(a + b) # 7.0
print(a - b) # 3.0
print(a * b) # 10.0
Even if both numbers look “whole” but one is a float, the result will be float.
Division and number types
Python’s division operator / always returns a float, even if the numbers divide evenly.
print(4 / 2) # 2.0 (float, not 2)
print(9 / 3) # 3.0
print(5 / 2) # 2.5
If you want integer (whole number) division that throws away the decimal part, you can use // (floor division). This keeps the type rules in mind, but the detailed explanation of operators belongs in the arithmetic operations chapter. Here’s just a quick demonstration of how it affects types:
print(5 // 2) # 2 (int)
print(5.0 // 2) # 2.0 (float, but no decimal fraction shown)
Notice how using a float on either side of // makes the result a float.
Converting between `int` and `float`
Sometimes you need to switch types. Python provides built-in functions for this:
From `int` to `float`
Use float():
a = 5 # int
b = float(a) # convert to float
print(a, type(a))
print(b, type(b))
This will turn 5 into 5.0.
From `float` to `int`
Use int():
x = 3.9
y = int(x)
print(x, type(x))
print(y, type(y))
int() throws away the decimal part; it does not round.
So:
int(3.9)becomes3int(3.1)becomes3int(-2.7)becomes-2(towards zero)
If you need rounding (e.g. to the nearest whole number), that will be covered with other built-in functions later; for now, remember that int() simply cuts off the decimal part.
Typical uses of `int` vs `float`
Common uses:
- Use
intfor: - Counting things (people, items, attempts)
- Indexes and positions in lists
- Whole-number calculations (like pages, days, rounds)
- Use
floatfor: - Measurements (height, weight, temperature)
- Money in simple examples (though real financial apps need special handling)
- Percentages, averages, and anything that naturally needs decimals
Example:
age = 25 # int
number_of_items = 3 # int
temperature = 21.5 # float
height_m = 1.68 # float
print(age, type(age))
print(temperature, type(temperature))Pitfalls with floating-point numbers
Floats are stored in a way that can’t exactly represent all decimal numbers. This sometimes leads to surprising results.
For example:
print(0.1 + 0.2)
You might expect 0.3, but Python could show something like:
0.30000000000000004This isn’t a bug in Python; it’s how floating-point numbers work in almost all programming languages.
For beginners, the main takeaway:
- Small “weird” extra decimals with
floatare normal. - Avoid directly comparing floats for exact equality in serious programs (this idea will matter more later).
Checking the type of a number
To confirm whether a value is int or float, you can use type():
a = 10
b = 10.0
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
You may also see isinstance() used to check types:
print(isinstance(a, int)) # True
print(isinstance(a, float)) # False
print(isinstance(b, float)) # TrueThis is useful when you want to know what kind of number you’re working with inside a program.
Simple practice ideas
Here are a few small things you can try on your own:
- Create two
intvariables, add them, and check the type of the result. - Create one
intand onefloat, multiply them, and check the type of the result. - Convert an
inttofloatand back tointusingfloat()andint(). - Experiment with
0.1 + 0.2,0.1 + 0.1 + 0.1, and see the results.