Common Uses of %
In the previous lesson, I introduced you to the various ways of getting mod out of Python. In this lesson, I’m going to show you common uses of the
% operator and why you would use mod in your code.
If the number mod 2 is zero, then it’s even, else it’s odd. One thing to notice here is that if you’re trying to do this kind of calculation, it’s best practice to use
== 0, and in the case of odd,
!= 0. Rather than, say, in the case of odd, using
The reason for this goes back to mod and negative numbers. If you compare
!= 0, you’ll always get the right answer. If you compare
== 1, depending on whether or not your numerator or divider is negative, you may get different values.
The number of columns that it prints it out in is based on a parameter passed into the function. The first step in
make_columns() is to find the longest item in the list that’s being passed in. Lines 4 and 5 find the longest item and then find the length of that longest item.
The f-string here is using a format configuration to pad the length of what is being printed. In line 7, I’m enumerating through the contents that are being passed in. For each item in that, I’m going to print that item and I’m going to pad it with hyphens to the width of the
width value—the width of the column that I calculated in line 5.
Line 9 is where the actual modulo math happens. Here, I’m doing a mod of the number of columns to be printed against the index number of the
enumerate() specifies the index based on
0 and I want to start at
1, I have to do
index + 1.
Notice the use of the brackets here. Remember the order of operations. If I didn’t have the brackets, I would be doing
1 % num_columns and then adding
index, rather than doing the mod of
index + 1.
index being divided is a multiple of the number of columns, i.e. the mod is
0, then it’s time to do carriage return and go to the next line. Finally, at the end of the function, after all of this, an extra
print() is done just in case the last row has fewer columns than the number of columns being passed in. Inside of the REPL, I can show you this in practice.
03:46 Printing the columns is an example of trying to cycle through some data. Without the mod operator, you have to do special cases to check whether or not you’re near the end of your list or need to wrap around. Because modular arithmetic wraps for you, it saves you some code of conditional checking. Here’s another example.
This function takes the value of
start, uses that as the position in the
faces string to start with, and then the value of
length and continues to print that string, cycling over and over again.
The key to doing this kind of cycle is determining the position based on the mod. The index of the first emoji to print is calculated inside of the
pos (position) variable. It’s based on
start, modded with the length of the
This guarantees—no matter what number is passed in—a valid happy face position will be found. Without the mod, you would have to do conditions on whether or not
start was less than or greater than the length of the
faces and do math to figure out how to adjust it.
pos as the starting position plus the current
count value of the loop. Once again, this is modded with
len(faces). That way, if
index goes past the end of
faces, it’ll loop back to the beginning.
06:16 Without modular arithmetic, this code probably would have had to have required two or three other conditional checks inside of it for boundaries and resets of variables. With the modular arithmetic, the code is nice and succinct.
The number of days is calculated using strict integer division of the 1,440 minutes in a day. The number of minutes that are not accounted for in that
days calculation is
extra_minutes, which is the
total_minutes % 1440.
Substitution ciphers map one letter of the alphabet to another. For example, in the message you’re trying to encode, anytime you see an
'A', you replace it with a
'C'. When you find a
'B', replace it with a
'E', et cetera.
Become a Member to join the conversation.