Numbers: int and float
Working with whole numbers and decimals. Every arithmetic operation Python can do — explained simply with real examples.
"Numbers are the language of logic. Master them in Python and every door in data, finance, science, and AI opens a little wider."
— ShurAITwo Types of Numbers in Python
Python has two main number types. Understanding the difference is the first step:
# int — whole numbers
age = 22
temperature = -5
population = 1400000000
# float — decimal numbers
height = 5.9
price = 299.99
pi = 3.14159
# Check the type
print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
If it has a dot — it is a float. If it does not — it is an int. Even 5.0 is a float, not an int, because of the dot.
Arithmetic Operators — Python's Calculator
Python can do all the maths you know, plus a couple of extras that are incredibly useful in programming:
| Symbol | Operation | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.333... (float) |
| // | Floor Division | 10 // 3 | 3 (drops decimal) |
| % | Modulo (remainder) | 10 % 3 | 1 (leftover) |
| ** | Exponent (power) | 10 ** 3 | 1000 (10³) |
a = 10
b = 3
print(a + b) # 13 — addition
print(a - b) # 7 — subtraction
print(a * b) # 30 — multiplication
print(a / b) # 3.3333333333333335 — always float
print(a // b) # 3 — floor division, drops decimal
print(a % b) # 1 — remainder after 10 ÷ 3
print(a ** b) # 1000 — 10 to the power of 3
Floor Division and Modulo — Simply Explained
These two confuse beginners the most. Here is the simplest way to understand them — think about sharing things equally:
Answer: 3 (the leftover 1 is ignored)
Answer: 1 (10 - 9 = 1 remaining)
Checking if a number is even or odd: number % 2 == 0 means even (no remainder). number % 2 == 1 means odd. You will use this constantly in real programs.
number = 7
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")
# Output: 7 is odd
Order of Operations — BODMAS
Python follows the same order of operations you learned in school — Brackets first, then powers, then multiply/divide, then add/subtract. Always use brackets when in doubt:
print(2 + 3 * 4) # 14 — multiply first, then add
print((2 + 3) * 4) # 20 — brackets first
print(2 ** 3 + 1) # 9 — power first (2³=8), then +1
print(10 / 2 + 3) # 8.0 — divide first, then add
Mixing int and float
When you do arithmetic with an int and a float together, Python always gives back a float. This is by design — Python wants to make sure no precision is lost:
print(5 + 2.0) # 7.0 — float wins
print(10 * 3.0) # 30.0 — float wins
print(7 / 2) # 3.5 — division ALWAYS gives float
print(7 // 2) # 3 — floor division gives int
Converting Between int and float
You can manually convert between the two types using int() and float():
# float → int (drops the decimal, does NOT round)
print(int(9.9)) # 9 — decimal part is cut off
print(int(3.1)) # 3 — NOT rounded to 3
# int → float
print(float(5)) # 5.0
print(float(100)) # 100.0
# string → int or float (useful when reading user input)
print(int("42")) # 42
print(float("3.14")) # 3.14
int(9.9) gives 9, not 10. It simply cuts off everything after the decimal point. To round properly, use Python's built-in round() function: round(9.9) gives 10.
Useful Number Functions
# round() — round to nearest whole number or decimal places
print(round(3.7)) # 4
print(round(3.14159, 2)) # 3.14 — keep 2 decimal places
# abs() — absolute value (removes negative sign)
print(abs(-50)) # 50
print(abs(50)) # 50
# max() and min() — largest and smallest of given values
print(max(10, 50, 30)) # 50
print(min(10, 50, 30)) # 10
# pow() — same as **
print(pow(2, 8)) # 256 — 2 to the power of 8
Real World Example — Bill Calculator
Here is a practical program that uses almost every number concept from this lesson:
# Restaurant bill calculator
food_price = 450.00 # float — price in rupees
drinks_price = 120.00
people = 4 # int — number of people
gst_percent = 18 # int — tax percentage
# Calculate total
subtotal = food_price + drinks_price
gst = (subtotal * gst_percent) / 100
total = subtotal + gst
per_person = round(total / people, 2)
print(f"Subtotal : ₹{subtotal}")
print(f"GST (18%) : ₹{gst}")
print(f"Total : ₹{total}")
print(f"Per person: ₹{per_person}")
Subtotal : ₹570.0
GST (18%) : ₹102.6
Total : ₹672.6
Per person: ₹168.15
"Every app that handles money, every game that tracks score, every science program that measures data — they all start here, with numbers."
— ShurAI🧠 Quiz — Question 1
What is the difference between 7 / 2 and 7 // 2 in Python?
🧠 Quiz — Question 2
What does 10 % 3 return?
🧠 Quiz — Question 3
What is the output of print(2 + 3 * 4)?
🧠 Quiz — Question 4
What does int(9.9) return in Python?
🧠 Quiz — Question 5
Which operator is used to raise a number to a power in Python? (e.g. 2 to the power of 5)