Skip to content
FollowtheDamnCode

Arithmetic Operators

+, -, *, /, //, %, and **.

4 minute read

Big idea

Python has seven arithmetic operators. Four you know from school; three — //, %, and ** — do the jobs that come up constantly in programs.

The operators

OperatorMeaningExampleResult
+add7 + 29
-subtract7 - 25
*multiply7 * 214
/divide7 / 23.5
//floor divide (whole part)7 // 23
%remainder (modulo)7 % 21
**power7 ** 249
Try them all — change a and b
a = 17
b = 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

Run and edit it in the app.

// and % are a team

17 // 5 is how many whole 5s fit in 17 (3). 17 % 5 is what's left over (2). Together they split a number into "full groups" and "the rest":

Seconds into minutes and seconds
total = 200
minutes = total // 60
seconds = total % 60
print(minutes, "min", seconds, "sec")
output
3 min 20 sec

Run and edit it in the app.

% has a famous second use: checking divisibility. n % 2 is 0 exactly when n is even.

Predict: remainders
print(10 % 2)
print(11 % 2)
print(15 % 4)
print(4 % 15)
What does it print? Show the answer
0
1
3
4

Run and edit it in the app.

4 % 15 is 4: fifteen doesn't fit into 4 at all, so all 4 is left over.

Watch it

Powers

** raises to a power. 2 ** 10 is 1024. A fractional power gives roots: 9 ** 0.5 is 3.0, the square root.

Fix the bug: the area of a square with side 6 is 36
side = 6
area = side ^ 2
print(area)
output
36

Run and edit it in the app.

In Python ^ is not power — it's a bitwise operator you won't need for a long time. Use **.

What is 23 // 4 and 23 % 4?

Think of it like

Sharing 17 sweets among 5 friends. 17 // 5 is how many each friend gets (3). 17 % 5 is how many are left in the bag (2).

Key terms

  • Operator — A symbol that does something to values, like +.
  • Floor division (//) — Division that keeps only the whole part.
  • Modulo (%) — The remainder after division.

Check yourself

What is 17 % 5?

Which operator raises to a power?

How can you tell if n is even?