Chapter 5 · pp. 61–72
Conditionals and Recursion
The main topic of this chapter is the if statement, which lets a program run different code depending on its current state. Before that, the chapter introduces two arithmetic operators, floor division and modulus. They are very useful for building the number-based tests that if statements rely on. This section covers those two operators.
Why ordinary division is not always enough
Suppose a movie runs 105 minutes and you want to know its length in hours. Dividing with / gives a floating-point number:
>>> minutes = 105
>>> minutes / 60
1.75
In Python 3, / always gives a float, even when the numbers divide evenly. People rarely say "1.75 hours" for a movie, though. They say "1 hour 45 minutes." That needs two separate whole-number answers: how many full hours, and how many minutes are left over. Floor division and modulus give exactly those two answers. (p. 61)
Floor division with //
The floor division operator // divides two numbers and then rounds the result down to a whole number. It gives the number of times the second number fits completely into the first.
>>> hours = minutes // 60
>>> hours
1
Since 105 divided by 60 is 1.75, rounding down gives 1 full hour. (p. 61)
More examples:
7 // 2 is 3, because 3.5 rounds down to 3.
59 // 60 is 0, because 59 minutes is not yet a full hour.
120 // 60 is 2, because it divides evenly.
"Rounds down" means toward the smaller number, not just dropping the decimal part. For positive numbers these are the same thing. For negatives they differ: -7 // 2 is -4, since −3.5 rounds down to −4.
The modulus operator %
The modulus operator % divides two numbers and returns the remainder. The remainder is what is left after you take out as many whole groups as will fit. (p. 61)
There are two ways to find the leftover minutes in our movie example.
- Subtract the full hours from the total:
minutes - hours * 60 gives 45.
- Use the operator directly:
minutes % 60 also gives 45.
The second way is shorter and does not need the hours variable. (p. 61)
The two operators are linked by this relationship:
a=b×(a//b)+(a%b)
Check it with a=105 and b=60: 60×1+45=105. So the movie is 1 hour and 45 minutes long.
Here is a second example. A video lasts 200 seconds.
200 // 60 is 3, so there are 3 full minutes.
200 % 60 is 20, so 20 seconds are left over.
- The video is 3 minutes 20 seconds long.
Useful tricks with the modulus operator
The modulus operator is more useful than it first looks. Here are two common uses.
Testing divisibility. If x % y equals zero, then x is divisible by y, because nothing is left over. For example, 12 % 4 is 0, so 12 is divisible by 4. But 13 % 4 is 1, so 13 is not. A special case is checking for even numbers: n % 2 == 0 is true exactly when n is even. (p. 61)
Extracting the right-most digits. In base 10, x % 10 gives the last digit of x, and x % 100 gives the last two digits. For example, with x = 4729:
4729 % 10 is 9
4729 % 100 is 29
This works because our number system is built on powers of ten. Dividing by 10 or 100 leaves the last one or two digits as the remainder. (p. 62)
The same idea works for anything that repeats in a cycle. If it is 10 o'clock and you want the hour 5 hours later on a 12-hour clock, (10 + 5) % 12 gives 3.
A note about Python 2
Division behaves differently in Python 2. There, / does floor division if both numbers are integers. It does floating-point division if at least one of them is a float. So in Python 2, 105 / 60 gives 1, not 1.75. In Python 3, / always gives a float, and // is the operator for floor division. Keep this in mind when you read older code. (p. 62)
Common mistakes
- Mixing up
/ and //. Use / when you want a decimal answer and // when you want the whole number of times something fits.
- Reading
% as "percent." In Python it means "remainder after dividing."
- Dividing by zero. Both
x // 0 and x % 0 cause a ZeroDivisionError.
Check yourself
- What are the values of
47 // 10 and 47 % 10? What does each one tell you?
- A program stores
minutes = 135. Write the two expressions that give the hours and the leftover minutes. What are their values?
- How can you use
% to test whether a number n is divisible by 5?
- What does
8352 % 100 return, and why does this work?
- In Python 3, what does
105 / 60 give compared with 105 // 60? How would Python 2 treat 105 / 60?