OperatorsBeginner7 min13 / 63

Arithmetic Operators

Learn how Python does math — from basic addition to the surprisingly handy modulo operator — and understand the difference between regular and floor division.

Computers are incredible calculators. Before we teach Python to make decisions or repeat tasks, we need to teach it to do math. Python supports all the arithmetic you learned in school — plus a few operators you may not have seen before that turn out to be incredibly useful.

In this lesson we will walk through every arithmetic operator, run real examples, and pick up a few insights that will save you from common beginner stumbles.

See it in action

Visual walkthrough1 / 5
1

Python Does Math Too

Python has seven arithmetic operators — the four you know from school, plus three powerful extras. Master these and you can make Python calculate almost anything.

Python arithmetic works directly in the REPL — try typing `2 + 2` and pressing Enter.

#The Basics: + - * /

The four everyday operators work exactly as you would expect. You can use them with whole numbers (int) or decimal numbers (float).

The four classic operators. Notice that / always gives a decimal result.
print(10 + 3)   # addition
print(10 - 3)   # subtraction
print(10 * 3)   # multiplication
print(10 / 3)   # division
Note

Division always returns a float

Even 10 / 2 returns 5.0, not 5. The / operator always produces a float in Python 3. This is intentional — Python wants to be honest that division can produce a fraction.

#Floor Division: //

Sometimes you only care about the whole number part of a division result — no decimals, no rounding, just chop everything after the decimal point. That is what // (floor division) does.

Think of it like this: if you have 10 cookies and 3 friends, how many whole cookies does each person get? The answer is 3 — you ignore the leftover.

// discards everything after the decimal point.
print(10 / 3)    # regular division  -> float
print(10 // 3)   # floor division    -> int (whole part only)
Common mistake

Floor division rounds DOWN, not toward zero

With negative numbers, // floors toward negative infinity, not toward zero.

-7 // 2 gives -4, not -3.

This surprises many beginners. For positive numbers it behaves exactly like truncating, but keep this in mind when working with negatives.

Floor division always goes toward the lower integer.
print(-7 // 2)   # floors toward negative infinity
print(7 // 2)    # floors toward zero (same as truncation here)

#Modulo: %

The % operator gives you the remainder after division. Going back to the cookie example: 10 cookies shared among 3 friends — each gets 3, and there is 1 left over. That leftover is what % returns.

% returns the remainder, not the quotient.
print(10 % 3)   # 10 divided by 3 is 3 remainder 1
print(15 % 5)   # 15 divided by 5 is 3 remainder 0
print(7 % 2)    # 7 divided by 2 is 3 remainder 1
Think of it like

Modulo is like a clock

Imagine a 12-hour clock. After 12, the hours wrap back to 1. Modulo works the same way — it wraps numbers around. 14 % 12 gives 2, just like 14:00 is 2 o'clock on a 12-hour clock.

This wrapping property makes % useful for things like cycling through a list or alternating between two states.

One of the most common uses of % is checking whether a number is even or odd. An even number has no remainder when divided by 2; an odd number has a remainder of 1.

% 2 is the classic even/odd test.
number = 42
print(number % 2)   # 0 means even

number = 7
print(number % 2)   # 1 means odd

#Exponentiation: **

The ** operator raises a number to a power. 2 ** 8 means "2 to the power of 8", which is 2 multiplied by itself 8 times.

** works with both integer and fractional powers.
print(2 ** 8)    # 2 to the power of 8
print(3 ** 3)    # 3 cubed
print(9 ** 0.5)  # square root of 9 (power of 0.5)
Tip

Square root trick

You do not need a special function for square roots. Because the square root of a number is the same as raising it to the power of 0.5, you can write x ** 0.5 and Python handles it perfectly.

#Mixing int and float

What happens when you mix a whole number and a decimal in the same expression? Python promotes the result to float to avoid losing any precision. Think of it as Python being careful — it would rather give you too much information than too little.

Any float in the expression makes the result a float.
print(type(3 + 2))      # int + int  -> int
print(type(3 + 2.0))    # int + float -> float
print(type(10 // 2))    # floor div  -> int
print(type(10 // 2.0))  # floor div with float -> float

#Order of Operations

Python follows the same order of operations you learned in math class — often remembered as PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction). When in doubt, use parentheses to make your intention clear.

Parentheses always win. Use them freely to make code readable.
print(2 + 3 * 4)      # multiplication first -> 14
print((2 + 3) * 4)    # parentheses first   -> 20
print(2 ** 3 ** 2)    # ** is right-to-left: 2 ** (3**2) = 2**9 = 512
Watch out

** is right-associative

2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512, NOT (2 ** 3) ** 2 = 64. Exponentiation chains from right to left. When chaining **, always use parentheses to be explicit.

Quick check

What does `17 % 5` evaluate to?

Key takeaways

  • `/` always returns a `float`; use `//` when you only want the whole-number part of a division.
  • The `%` (modulo) operator returns the remainder — great for even/odd checks and wrapping values.
  • `**` raises a number to a power; `x ** 0.5` is a handy way to compute a square root.
  • Mixing an `int` and a `float` in any expression promotes the result to `float`.
  • Parentheses override all other precedence rules — use them freely to make your math intentions clear.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
print(10 / 2)
print(10 // 3)
print(10 % 3)
Fix the bug#2

This code should print whether 42 is even, but it has a bug. What is wrong?

fix-bug
number = 42
print(number / 2)
Fill in the blank#3

Complete the code so it prints the square root of 25 (which is 5.0).

result = 25  0.5
print(result)
Reorder the lines#4

Put these lines in the right order so the code converts 90 seconds into minutes and leftover seconds, then prints both.

1
leftover = total % 60
2
total = 90
3
minutes = total // 60
4
print(minutes, "minute(s)", leftover, "second(s)")
Your turn
Practice exercise

Write a small Python program that takes the number seconds = 3661 and prints: 1. How many complete hours are in that many seconds. 2. How many leftover minutes after removing the hours. 3. How many leftover seconds after removing the hours and minutes.

Expected output: `` 1 hour(s) 1 minute(s) 1 second(s) ``

Hint: there are 3600 seconds in an hour and 60 seconds in a minute.

Try it live — edit the code and hit Run to execute real Python:

solution.py · editable