Q1What does this code print?
print(type(10 / 2))
Numeric Types and Floating Point
Get comfortable with Python's int and float types and the operators that trip people up, all by running examples in your browser.
Python has two numeric types
Python has two main numeric types for working with numbers: int for whole numbers and float (floating-point) for decimals.
Both are numbers, so you can do all the basic arithmetic — addition, subtraction, multiplication, division, and modulo — with either one.
But their syntax and precision differ, so you pick the right one for each situation.
Write it without . and you get an int.
Write it with . and you get a float.
Either way, type() tells you which one you have.
Even if the value looks like a whole number, adding .0 like 3.0 makes it a float. On the flip side, just writing 175 makes it an int.
Negative numbers work the same way — -5 is an int, -5.5 is a float.
# int (whole number)
age = 25
print(type(age)) # <class 'int'>
# float (floating-point)
weight = 175.5
print(type(weight)) # <class 'float'>
# Same "3", different types depending on how you write it
print(type(3)) # <class 'int'>
print(type(3.0)) # <class 'float'>
# Negatives work the same way
print(type(-10)) # <class 'int'>
print(type(-10.5)) # <class 'float'>
Quick tour of the operators
There are 7 operators you use for numeric math. The four basic ones + - * / work the same as in other languages, but // (integer division), % (modulo), and ** (exponentiation) come up a lot in Python, so it's worth locking them in.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division (always float) | 10 / 3 | 3.3333... |
| // | Integer division (truncates decimals) | 10 // 3 | 3 |
| % | Modulo (remainder of division) | 10 % 3 | 1 |
| ** | Exponentiation | 2 ** 10 | 1024 |
/ returns a float even between ints
10 / 2 is 5.0 (float), not 5.
Python's / is designed to always return a float, so the result is a float even when both operands are ints.
If you want an integer result, use // instead.
# Basic arithmetic
print(10 + 3) # 13
print(10 - 3) # 7
print(10 * 3) # 30
# Division always returns a float
print(10 / 3) # 3.3333333333333335
print(10 / 2) # 5.0 (still a float even when it divides evenly)
# Integer division (truncates) and modulo
print(10 // 3) # 3
print(10 % 3) # 1
# Exponentiation: a ** b means "a to the power of b"
print(2 ** 10) # 1024
print(3 ** 2) # 9
print(2 ** 0.5) # 1.4142135623730951 (use it as a square root)
Compound assignment operators (+= / -= and friends)
Patterns like "x = x + 1" — where you do something to a variable and write the result back to itself — come up all the time.
You can shorten that to "x += 1", and these shortcut forms are called compound assignment operators.
Each of `+ - / // % * has a compound form.
The short form is a one-line way to say "do something to myself and write it back".
Internally it behaves exactly like the expanded form on the right.
As an example, here's what each operator does when x = 10.
score = 10
score += 5 # 10 + 5 -> 15
print(score) # 15
score *= 2 # 15 * 2 -> 30
print(score) # 30
score -= 7 # 30 - 7 -> 23
print(score) # 23
# Works the same way with float
weight = 60.0
weight += 1.5
print(weight) # 61.5
Mixing int and float in operations
When you mix int and float, the result is always a float. That's because Python automatically promotes to whichever type has more precision.
As mentioned above, / (division) returns a float even between ints, while // (integer division) returns an int when both operands are ints.
int + int = int.
But / always returns a float.
If either side is float, the result is float.
# int with int stays int
print(10 + 3) # 13 -> int
print(type(10 + 3)) # <class 'int'>
# If either side is float, the result is a float
print(10 + 3.0) # 13.0
print(type(10 + 3.0)) # <class 'float'>
# / returns a float even between ints
print(10 / 2) # 5.0
print(type(10 / 2)) # <class 'float'>
# // stays int when both are ints
print(10 // 3) # 3
print(type(10 // 3)) # <class 'int'>
# // becomes float if either side is float
print(10 // 3.0) # 3.0
print(type(10 // 3.0)) # <class 'float'>
float has limited precision
Computers store floats as a binary approximation. That's why even simple-looking calculations can produce a tiny amount of error.
The example below is a classic "float gotcha" — every beginner runs into it at some point.
Use Decimal when you need exact math
For situations where even a tiny error is unacceptable — like money calculations — you can do exact decimal math with the standard library's Decimal type. We won't cover it in this article, but just remember: "float is an approximation" and "there's a different tool for exact math".
Switching types with int() and float()
To convert between numeric types, use the built-in functions int() and float().
One thing to watch out for: int() truncates the decimal part — it doesn't round. So passing 3.9 to int() gives you 3, not 4. For negative values, it rounds toward zero, so -3.9 becomes -3, not -4.
# float -> int is "truncate" (NOT rounding!)
print(int(3.9)) # 3
print(int(3.1)) # 3
print(int(-3.9)) # -3 (rounds toward zero)
# int -> float just adds .0
print(float(5)) # 5.0
print(float(-2)) # -2.0
# Using conversion through a variable
a = 3.9
a = int(a)
print(a, type(a)) # 3 <class 'int'>
b = 5
b = float(b)
print(b, type(b)) # 5.0 <class 'float'>
In this article, you learned:
- The difference between int and float
- Exponentiation with "**" and compound assignment (+= and friends)
- The return-type difference between "/" (division) and "//" (integer division)
- Float precision issues
- Type conversion with int() / float()
Knowledge Check
Answer each question one by one.
Q2What does this code print?
x = 10
x **= 3
print(x)`
Q3What does this code print?print(int(-3.9))