Operators and Expressions · Lesson 7 of 39
Arithmetic Operators and Precedence
Python is a superb calculator. Beyond the usual plus and minus, it has operators for whole-number division, remainders and powers – the tools behind everything from splitting a bill to checking whether a year is a leap year.
In this lesson
- The seven arithmetic operators
//and%– and why they are so useful- Operator precedence (which part is calculated first)
- The
mathmodule for square roots, rounding up and more
The arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Addition | 7 + 2 | 9 |
- | Subtraction | 7 - 2 | 5 |
* | Multiplication | 7 * 2 | 14 |
/ | Division (always float) | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Modulo (remainder) | 7 % 2 | 1 |
** | Power | 7 ** 2 | 49 |
a, b = 17, 5
print(a + b, a - b, a * b)
print(a / b)
print(a // b, a % b)
print(a ** 2)
22 12 85 3.4 3 2 289
Floor division and modulo, side by side
Think of sharing 17 sweets between 5 children. Each child gets 17 // 5 = 3, and 17 % 5 = 2 sweets are left over.
17 sweets, 5 children
- 17 // 5 = 3how many each child gets
- 17 % 5 = 2how many are left over
- 5 × 3 + 2 = 17the two always fit back together
Modulo turns up everywhere:
number = 42
print("even" if number % 2 == 0 else "odd") # even/odd check
seconds = 3725
print(seconds // 3600, "h", seconds % 3600 // 60, "min", seconds % 60, "s")
print(1234 % 10) # last digit
print(1234 // 10) # drop the last digit
print(divmod(17, 5)) # both at once
even 1 h 2 min 5 s 4 123 (3, 2)
Common mistake: // with negative numbers
// rounds down (towards minus infinity), not towards zero. So -7 // 2 is -4, not -3, and -7 % 2 is 1. Java and C give different answers here – a favourite interview question.
print(-7 // 2, -7 % 2)
print(int(-7 / 2)) # truncates towards zero instead
-4 1 -3
Mixing ints and floats
If either side is a float, the result is a float. / always gives a float:
print(3 + 2.0)
print(10 / 5)
print(10 // 4.0)
5.0 2.0 2.0
Operator precedence
When an expression has several operators, Python follows a fixed order – the same idea as BODMAS from school maths:
Highest priority first
- ( )brackets
- **power (evaluated right to left)
- +x -xunary plus and minus
- * / // %left to right
- + -left to right
print(2 + 3 * 4) # * before +
print((2 + 3) * 4) # brackets first
print(2 ** 3 ** 2) # right to left: 2 ** 9
print(-3 ** 2) # ** binds tighter than the minus
print(100 / 10 * 2) # left to right
14 20 512 -9 20.0
Tip
When in doubt, add brackets. (-3) ** 2 is 9 and nobody reading your code has to remember the precedence table.
The math module
For anything beyond basic arithmetic, import the standard math module:
import math
print(math.sqrt(144))
print(math.ceil(4.1), math.floor(4.9)) # round up / down
print(math.pi)
print(math.factorial(5))
print(math.gcd(24, 36))
print(abs(-12), round(2.675, 2), pow(2, 10))
12.0 5 4 3.141592653589793 120 12 12 2.67 1024
round(2.675, 2) gives 2.67, not 2.68, because 2.675 is stored as 2.67499999… in binary. Python also uses "banker's rounding" for exact halves: round(2.5) is 2 and round(3.5) is 4.
Real example: EMI calculator
A loan EMI uses powers and division. The formula is EMI = P × r × (1 + r)ⁿ ÷ ((1 + r)ⁿ − 1), where r is the monthly interest rate:
principal = 500_000 # loan amount
annual_rate = 9 # percent
months = 60
r = annual_rate / 12 / 100
emi = principal * r * (1 + r) ** months / ((1 + r) ** months - 1)
print(f"Monthly EMI: ₹{emi:,.2f}")
print(f"Total interest: ₹{emi * months - principal:,.2f}")
Monthly EMI: ₹10,379.18 Total interest: ₹122,750.66
Practice
Try it yourself
Print the sum of the digits of 4,729 using only
//and%.Show solution
n = 4729 total = n % 10 + n // 10 % 10 + n // 100 % 10 + n // 1000 print(total)22
A bus holds 42 people. How many buses are needed for 250 students? (Hint: round up.)
Show solution
import math print(math.ceil(250 / 42)) print(-(-250 // 42)) # same trick without math6 6
Quick check
What is 9 // 2 + 9 % 2?
5 (4 + 1).
What is 2 ** 3 ** 2?
512 – powers are evaluated right to left, so it is 2 ** 9.
What does -7 // 2 give in Python?
-4, because floor division rounds towards minus infinity.
Summary
/always returns a float;//floors;%gives the remainder;**is power.%is the go-to tool for even/odd checks, digits and time conversions.- Precedence: brackets →
**→ unary minus →* / // %→+ -. - The
mathmodule addssqrt,ceil,floor,pi,gcdand more.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.