Skip to content
CCC Python Course

Integer arithmetic and operators

Module
M1.2
Lesson
1 of 1
Reading time
5 min

In this lesson

  • Compute with //, %, ** and /, using precedence and parentheses to control the order Python evaluates a longer expression.
  • Predict what // and % give with negative numbers, and recognize the ZeroDivisionError that dividing by zero raises.
  • Shorten repeated updates to a variable with augmented assignment.
  • Use abs, min, max, divmod and pow instead of writing that arithmetic by hand.

The module on variables covered +, - and *. Contest problems also need a way to split a total into groups and find what is left over, or to raise a number to a power. Sometimes you need an answer with a decimal part too. Python gives you an operatorA symbol such as + or // that performs a computation on one or more values.In the glossary for each of these, and a fixed order for which one runs first in a longer line.

Operators and precedence

This lesson uses seven arithmetic operators: +, -, *, /, //, % and **. The rest of this lesson covers what each one computes. First, a line with more than one operator needs an order to run them in, called precedenceThe fixed order in which Python runs the operators in a line that has more than one. Parentheses change that order.In the glossary.

** runs first, even before a leading minus sign, so -2 ** 2 is -4, not 4. Next come *, /, // and %, run left to right. Last come + and -, also left to right. Parentheses override this order: whatever sits inside them runs first. When you are not sure what a line computes, add parentheses. They cost nothing and remove the guesswork.

examples/operator_precedence.py
print(2 + 3 * 4)print((2 + 3) * 4)print(-2 ** 2)print(20 - 5 - 3)

Output

14
20
-4
12
The order Python runs operators in, in a longer line

2 + 3 * 4 runs the multiplication first and gives 14. Parentheses around 2 + 3 change that order and give 20. -2 ** 2 computes the power before the minus sign, giving -4. 20 - 5 - 3 runs left to right, giving 12, the same as (20 - 5) - 3.

Floor division and modulo

The // operator is floor divisionDivision that rounds the exact answer down to a whole number, written //.In the glossary. It divides two numbers, then rounds the exact answer down to the whole number at or below it. 17 // 5 is 3, because the exact answer 3.4 rounds down to 3.

The % operator is moduloThe remainder left over after floor division, written %.In the glossary: it gives what floor division leaves over. 17 % 5 is 2, the amount left after three full groups of five. Floor division and modulo always fit together: (17 // 5) * 5 + 17 % 5 equals 17 again.

examples/box_division.py
cupcakes = 17box_size = 5full_boxes = cupcakes // box_sizeleftover = cupcakes % box_sizeprint(full_boxes)print(leftover)

Output

3
2
Splitting seventeen cupcakes into full boxes of five

Seventeen cupcakes fill three full boxes of five, with two left over. 17 // 5 gives the 3 full boxes, and 17 % 5 gives the 2 left over.

Floor division still rounds down when the exact answer is negative. Down means down on the number line, away from zero. -7 // 2 is -4, because the exact answer -3.5 rounds down to -4, not up to -3.

examples/pair_rule.py
a = -7b = 2print(a // b)print(a % b)print((a // b) * b + a % b)

Output

-4
1
-7
Floor division and modulo still fit together with a negative number

-7 // 2 gives -4, and -7 % 2 gives 1. The pair still fits together: -4 * 2 + 1 equals -7. When the number after % is positive, the result is always from 0 up to one less than that number, whatever sign the first number has.

A board game shows the same idea. Picture a piece on space 1 of a 5-space board, numbered 0 to 4, moving back 3 spaces.

examples/board_wraparound.py
position = 1steps_back = 3spaces = 5print((position - steps_back) % spaces)

Output

3
Modulo keeps a backward move on the board

(1 - 3) % 5 gives 3: the piece leaves space 1, moves back past space 0, and wraps around to land on space 3.

A step-through makes the pairing between // and % easier to see. The program below turns a number of minutes into whole hours and the minutes left over, the same idea behind the box example, applied to time instead of cupcakes.

1total_minutes = 150
2hours = total_minutes // 60
3minutes = total_minutes % 60
4print(hours)
5print(minutes)
Output so far
(nothing printed yet)
Frames and objects, step 1 of 6FramesGlobal frame
Speed
Python starts at the top of the file. Line 1 runs first.
  • Just changed

Figure 1Tracking floor division and modulo as they split 150 minutes

Read the steps as text

The program divides 150 total minutes by 60 to get 2 whole hours, finds the remaining 30 minutes with the modulo operator, and prints 2 then 30.

  1. Python starts at the top of the file. Line 1 runs first.
  2. Line 1 runs: total_minutes is created with the value 150.
  3. Line 2 runs: hours is created with the value 2. 150 // 60 is 2: 60 fits into 150 twice, which uses 120 minutes.
  4. Line 3 runs: minutes is created with the value 30. 150 % 60 is 30: the minutes left over after those 120.
  5. Line 4 runs: it prints 2.
  6. Line 5 runs: it prints 30. The program has finished: no lines are left to run.

150 // 60 is 2, the whole hours, and 150 % 60 is 30, the minutes left over. A common mistake is writing total_minutes / 60 here, which answers a different question: how many hours as a fraction, not how many whole hours and how many minutes remain.

Division by zero

Dividing by 0 is not something Python can compute, whether you use /, // or %. Each one stops the program with a ZeroDivisionError.

examples/zero_division.py
boxes = 0print(17 // boxes)

Error: ZeroDivisionError

Traceback (most recent call last):
  File "zero_division.py", line 2, in <module>
    print(17 // boxes)
ZeroDivisionError: integer division or modulo by zero
Dividing by a variable that holds zero

Line 2 tries 17 // boxes while boxes is 0, so Python stops there instead of printing anything. The same error appears with / and % by zero. When you see ZeroDivisionError, check whether a value you are dividing by can be 0 before you divide.

True division and the .0 trap

The / operator is true divisionDivision written with / that always produces a float, even when the numbers divide evenly.In the glossary: it always gives a float, a number with a decimal part, even when the two numbers divide evenly.

examples/ticket_counters.py
tickets = 9counters = 2print(tickets / counters)print(tickets // counters)

Output

4.5
4
Nine tickets split across two counters

Nine tickets across two counters give 4.5 with /. Floor division gives 4, how many tickets each counter gets, with one ticket left over.

examples/exact_boxes.py
apples = 20baskets = 5print(apples / baskets)print(apples // baskets)

Output

4.0
4
True division gives a float even when the numbers divide evenly

Twenty apples across five baskets divide evenly, yet / still gives 4.0, not 4. The judge compares your output as text, and 4.0 does not match 4, even though the two numbers are mathematically equal. Reach for // whenever a problem expects a whole number.

Powers

The ** operator raises a number to a power. 2 ** 10 is 2 multiplied by itself ten times.

examples/powers_of_two.py
print(2 ** 10)print(2 ** 100)

Output

1024
1267650600228229401496703205376
A power that stays small, and one that grows far past what some other languages can hold

2 ** 10 gives 1024. 2 ** 100 gives a number with thirty-one digits, and Python prints every digit of it. The module on values, types and variables showed that Python integers can be as large as you need. There is no fixed limit the way some other languages have, so a big result never breaks your program.

Augmented assignment

Contest code often updates the same variable more than once, such as adding to a running total. Writing total = total + 5 works, but Python has a shorter form called augmented assignmentA shorthand such as += that updates a variable using its own current value in one step.In the glossary, which combines an operator with assignment in one step.

examples/score_growth.py
total = 0total += 3total += 10print(total)

Output

13
Building a total step by step with +=

total starts at 0, then total += 3 moves it to 3, and total += 10 moves it to 13. For numbers, total += 5 does the same thing as total = total + 5. Every arithmetic operator has an augmented form: +=, -=, *=, /=, //=, %= and **=, each updating a variable using its own current value. A common mistake is forgetting that the variable must already exist. count += 1 raises a NameError if count was never assigned first, for the same reason a plain reference to an unassigned name does.

Shortcuts: abs, min, max, divmod and pow

Python also gives you built-in functions that save you from writing common arithmetic by hand.

abs() returns a number with its sign removed, useful for a difference when you do not know which value is larger. Next, min() and max() each take two or more values and return the smallest or largest. After that, divmod() returns the floor division and the modulo of two numbers in one call, and pow() computes a power the same way ** does.

examples/useful_functions.py
first_stop = 4second_stop = 11print(abs(first_stop - second_stop))print(abs(second_stop - first_stop))
quiz_1 = 7quiz_2 = 10quiz_3 = 3print(min(quiz_1, quiz_2, quiz_3))print(max(quiz_1, quiz_2, quiz_3))
print(divmod(150, 60))print(pow(3, 4))

Output

7
7
3
10
(2, 30)
81
Built-in functions for common arithmetic

Two bus stops sit at positions 4 and 11. abs(4 - 11) and abs(11 - 4) both give 7, the distance between them, however you order the subtraction. Comparing three quiz scores, 7, 10 and 3, gives 3 as the minimum and 10 as the maximum. divmod(150, 60) packages the same hours-and-minutes split from the step-through above into a single call. Python prints the two results together inside parentheses. The module on tuples explains that packaging in full. pow(3, 4) gives 81, the same answer as 3 ** 4.

This lesson covered seven operators and the order they run in, floor division and modulo with negative numbers, augmented assignment, and five built-in functions that shortcut common arithmetic. Watch for / where a problem expects a whole number, and check whether a value can be 0 before you divide by it.

The problem below combines judges' scores into one final score.

Practice

Try this on the judge. The link opens the problem on WMOJ.

  1. 2026 J2
    Olympic Scores (opens on WMOJ in a new tab) WMOJ

    Turn five judges' scores and a difficulty factor into one final score.