Skip to content
Theory · Operators · lesson 1 of 9

Arithmetic and division

What you'll learn

What you'll learn:

  • find the quotient and the remainder with // and %, and remember that / always gives a float;
  • explain why 0.1 + 0.2 does not equal 0.3, and compare fractional numbers with math.isclose;
  • join conditions with and, or and not, and know what they return;
  • count the conditions that hold by adding True and False as numbers.
An adult shares 17 apples between five children, three each, and two apples tagged 17 % 5 stay in the adult's hand

Picture an adult with 17 apples and five children. The adult hands out apples one at a time around the circle while there are enough for everyone. Each child gets three apples, and two stay in the adult's hand.

Such a share-out has two answers: how many each child got and how many are left. On a board, the adult writes 17 // 5, which is three apples each. The two spare apples get a tag reading 17 % 5.

Addition, subtraction and multiplication in Python work as they do on a calculator. Division is different: it has three operators, and each answers its own question.

Python
apples = 17
kids = 5
print(apples + kids, apples - kids, apples * kids)  # 22 12 85
print(apples / kids)  # 3.4
print(apples // kids)  # 3
print(apples % kids)  # 2

Let's go through the program line by line:

  1. The first two lines put the number of apples and the number of kids into variables.
  2. The third line prints 22 12 85: the sum, the difference and the product, as on a calculator.
  3. apples / kids prints 3.4: what each child gets if the apples are cut into slices.
  4. apples // kids prints 3: how many whole apples each child gets.
  5. apples % kids prints 2: how many apples stay with the adult.

Three kinds of division

The values an operator works on are called operands. In 17 // 5, the operands are 17 and 5. Let's take each of the three division operators in turn.

True division /

The / operator always gives a float, even when the operands divide evenly: 6 / 2 is 3.0, not 3. The result type does not depend on the numbers, so you pick / when you need a fraction.

Floor division //

Floor division // rounds the quotient down, toward minus infinity. For positive numbers, that is the same as dropping the fractional part. For negative ones it is not: -7 / 2 is -3.5, and -7 // 2 is -4, the smaller of the two neighboring integers.

Remainder %

The % operator gives the remainder of a division: the apples left in the hand. The remainder after dividing by 2 tells whether a number is even: 8 % 2 is 0, and 7 % 2 is 1.

Quotient and remainder together

// and % are two halves of one division. Python ties them so that a // b * b + a % b always equals a. For the apples, that is 3 * 5 + 2, which is 17 again: the apples handed out plus the remainder make as many as there were.

Here the analogy ends: there are no negative apples, but Python divides negative numbers too. The same rule means that the remainder takes the sign of the divisor, the right operand:

Python
print(6 / 2, 7 // 2, -7 // 2)  # 3.0 3 -4
print(-7 % 3, 7 % -3)  # 2 -2
print(253 % 10, 253 // 10)  # 3 25

Let's take each line:

  1. The first prints 3.0 3 -4: / gave a float, and // rounded -3.5 down to -4.
  2. In the second, -7 % 3 is 2. Check it: -7 // 3 is -3, and -3 * 3 + 2 is -7.
  3. In the same line, 7 % -3 is -2: the divisor is negative, so the remainder is negative too.
  4. The third takes a number apart into digits: n % 10 is the last digit, and n // 10 is the number without it.

Step through the program and watch q and r: at each check they add back up to n.

LiveQuotient, remainder and true divisionstep 1 of 13 · frames: 1

n is the number of apples: 17.

Watch how q and r give back n

Interviewers like to ask this: "What are -7 // 2 and -7 % 3?" The answer always follows from the rule a // b * b + a % b == a and rounding down.

Two more rules about division:

  • if either operand of // or % is a float, the result is a float too: 7.0 // 2 is 3.0;
  • a divisor equal to zero stops the program with a ZeroDivisionError.

Powers and order

The ** operator raises to a power: 2 ** 10 is 1024, and 2 ** 0.5 is the square root of two. Precedence decides which operator runs first. The levels go like this:

  1. **;
  2. *, /, // and %;
  3. + and -.
Python
print(2 + 3 * 4, (2 + 3) * 4)  # 14 20
print(2 ** 10, 2 ** 0.5)  # 1024 1.4142135623730951
print(-2 ** 2, (-2) ** 2)  # -4 4

Operators of the same level, except **, run from left to right: 20 // 3 * 2 is 6 * 2, that is, 12. ** runs from right to left: 2 ** 3 ** 2 is 2 ** 9, that is, 512. Parentheses change the order, as in math.

** runs even before the minus in front of a number, so -2 ** 2 is -(2 ** 2). To square a negative number, put it in parentheses.

Common mistake

The average of two numbers is written without parentheses:

Python
a = 4
b = 6
average = a + b / 2  # ✗
print(average)  # 7.0

Python reports no error: the division ran before the addition, which gave 4 + 3.0. Put the sum in parentheses: (a + b) / 2 gives 5.0.

Key points

  • / always gives a float, and // rounds the quotient down, toward minus infinity.
  • % gives the remainder, and a // b * b + a % b always equals a; the remainder takes the divisor's sign.
  • ** runs first, then *, /, // and %, and + and - come last.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way