LearnFrenzy
Sign in Take a free test

Operators and Expressions · Lesson 7 of 39

Arithmetic Operators and Precedence

3 min read Updated Nov 7, 2026 Saurabh Samir

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 math module for square roots, rounding up and more

The arithmetic operators

OperatorMeaningExampleResult
+Addition7 + 29
-Subtraction7 - 25
*Multiplication7 * 214
/Division (always float)7 / 23.5
//Floor division7 // 23
%Modulo (remainder)7 % 21
**Power7 ** 249
a, b = 17, 5
print(a + b, a - b, a * b)
print(a / b)
print(a // b, a % b)
print(a ** 2)

Output

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

  1. 17 // 5 = 3how many each child gets
  2. 17 % 5 = 2how many are left over
  3. 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

Output

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

Output

-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)

Output

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

  1. ( )brackets
  2. **power (evaluated right to left)
  3. +x -xunary plus and minus
  4. * / // %left to right
  5. + -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

Output

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))

Output

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}")

Output

Monthly EMI: ₹10,379.18
Total interest: ₹122,750.66

Practice

Try it yourself

  1. 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)

    Output

    22
  2. 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 math

    Output

    6
    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 math module adds sqrt, ceil, floor, pi, gcd and more.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Popular