Arithmetic and division
What you'll learn:
- find the quotient and the remainder with
//and%, and remember that/always gives afloat; - explain why
0.1 + 0.2does not equal0.3, and compare fractional numbers withmath.isclose; - join conditions with
and,orandnot, and know what they return; - count the conditions that hold by adding
TrueandFalseas numbers.

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.
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) # 2Let's go through the program line by line:
- The first two lines put the number of apples and the number of kids into variables.
- The third line prints
22 12 85: the sum, the difference and the product, as on a calculator. apples / kidsprints3.4: what each child gets if the apples are cut into slices.apples // kidsprints3: how many whole apples each child gets.apples % kidsprints2: 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:
print(6 / 2, 7 // 2, -7 // 2) # 3.0 3 -4
print(-7 % 3, 7 % -3) # 2 -2
print(253 % 10, 253 // 10) # 3 25Let's take each line:
- The first prints
3.0 3 -4:/gave afloat, and//rounded-3.5down to-4. - In the second,
-7 % 3is2. Check it:-7 // 3is-3, and-3 * 3 + 2is-7. - In the same line,
7 % -3is-2: the divisor is negative, so the remainder is negative too. - The third takes a number apart into digits:
n % 10is the last digit, andn // 10is the number without it.
Step through the program and watch q and r: at each check they add back up to n.
1n = 172d = 53q = n // d4r = n % d5x = n / d6print(q, r, x)7print(q * d + r == n)8n = -79d = 210q = n // d11r = n % d12print(q, r)13print(q * d + r == n)
n is the number of apples: 17.
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 afloat, the result is afloattoo:7.0 // 2is3.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:
**;*,/,//and%;+and-.
print(2 + 3 * 4, (2 + 3) * 4) # 14 20
print(2 ** 10, 2 ** 0.5) # 1024 1.4142135623730951
print(-2 ** 2, (-2) ** 2) # -4 4Operators 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:
a = 4
b = 6
average = a + b / 2 # ✗
print(average) # 7.0Python 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 afloat, and//rounds the quotient down, toward minus infinity.%gives the remainder, anda // b * b + a % balways equalsa; 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.