FAQ: Learn Python: Syntax - Modulo

This community-built FAQ covers the “Modulo” exercise from the lesson “Learn Python: Syntax”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Modulo

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

28 posts were split to a new topic: How is modulo used and how does it determine which team?

12 posts were split to a new topic: Food for thought question?

2 posts were merged into an existing topic: Food for thought question?

I’ve rewritten the exercise directions to be more clear:

1: You’re trying to divide a group of people who are standing in a line into four teams. Each person is assigned a number based on their position in the line. In addition, each of the four teams is assigned a unique number that serves as the unique identifier for that team. To determine which team number a person number is assigned to, one must compute person number modulo 4. You are assigned number 27. Calculate your team number (27 modulo 4), and store in my_team.

2: Print my_team to display the team number that you are assigned to.

3: Calculate and print the team number of the person on your left (person number 26.) Then calculate and print the team number of the person on your right (person number 28.)

15 Likes

Why does 4 % 27 return 4?

Twenty-seven into four is zero, remainder 4.

In long division, the next division would be 40 / 27.

If we play with the numbers that come up in long division (which is easiest done on paper with a pencil, btw) we inevitably stumble upon a common sequence in the decimal portion of the quotient. A repeating sequence.

>>> 4/27
0.14814814814814814
>>>

Any way we start computing the quotient, we will end up with 148 somehow repeated, ad infinitum. All rational numbers share this tendency.


>>> 40/27
1.4814814814814814
>>> 

The remainder is 13.

>>> 13/27
0.48148148148148145
>>> 

Ignore the 5. It’s proven that this sequence never ends and always repeats. The 5 is due to rounding.

Oh, but we know 13 was not divisible because it was the remainder. We forget to multiply it by 10.

>>> 130/27
4.814814814814815
>>> 

The remainder is 22.

>>> 220 / 27
8.148148148148149
>>> 

Again, ignore the 9 as an artifact of rounding.

Because we know this a repeating and infinite sequence, there really isn’t much point to going any further with the division. But it’s been fun, hasn’t it?


Conversely…

Anytime we see a repeating sequence we may conclude that this is a rational number, also proved in maths. We’ll leave the reader to contribute that portion of this discussion.

2 Likes

I misread this question and nearly melted my brain in the process.

From the lesson it says “You’re trying to divide a group into four teams. All of you count off, and you get number 27.”

I misread this as, you totaled 27 as the one who counts, not you were assigned the number 27.

Without knowing which number I was, I couldn’t understand how the remainder of 3 was “the team I’m on” until someone clarified my interpretation of the question. It makes total sense now.

I suggest re-writing the question as follows:

“You’re trying to divide a group into four teams. All of you count off, and you are number 27”. Note, I changed “get” to “are”.

7 Likes

I still dont get it.
If I am number 27, we don’t now how many people are there in total.
If I get number 27 as a result of the count, I know at least that but nothing more.

The only thing “ 27 modulo 4 ” tells me, is how many people remain after creating 4 equal size teams. Not how many people are in those teams and certainly not in which team I would be.

Why would the amount of teams and/or amount of persons in total determine in which specific team I would be?
I would guess it is determined by whether I count myself first or last or where in the group I am standing when counting off. But lacking that information, I see no mathematical way to determine it.

4 Likes

Hello @bytecookie and welcome to the Codecademy Forums!

We do not need to know the total number of people here. The only information we need is that everyone needs to be divided into 4 teams and that you are number 27.

Team 1: 1, 5, 9, 13, 17, 21, 25
Team 2: 2, 6, 10, 14, 18, 22, 26
Team 3: 3, 7, 11, 15, 19, 23, 27
Team 4: 4, 8, 12, 16, 20, 24, …

No matter the total number of people being divided, number 27 will always be in team 3.

27 % 4 => 3

We can replace 27 with any number we want to determine what team the person with that number would be in.

6 Likes

Hi. I recently did this exercise and actually figured it out right away (I didn’t however understand why). However, I did read the other comments mentioned here. I appreciated the one method mention where the commenter said something like, “it’s like a coach putting people on a team, team 1, team 2, team 3, team 4. The first student in line goes to team 1, the next student goes to team 2, the next student goes to team 3”, like a rotation; It starts back over. I tried this myself, and compared it to the results I initially got before even reviewing these comments. Here is what I did:

my_team = 27 % 4
print (my_team) 3

print (my_team - 1) 2
print (my_team + 1) 4
print (my_team - 2) 1

Above, I simply subtracted (not knowing it would work lol) from the total of my team to get to the 25, which would divide evenly by four giving an even four teams. I also subtracted to get the results for number 26 and 28. These numbers perfectly match to the previous illustration, where each student would be on either team 1, 2, 3 or 4. Number 25 ends up on team 1, number 26 ends up on team 2, and number 28 ends up on team 4. I found this out just by subtracting or adding to the total to get that team member #.

6 Likes

can anyone tell me or clear the last question in this exercise (Food for thought: what number team are the two people next to you (26 and 28) on? What are the numbers for all 4 teams? (Optional Challenge Question))

i didnt understand the answer of this
kindly help

1 Like

thanks a lot :handshake: :smiley:

I don’t get it can someone help please

I REQUIRE ASSISTANCE

please…

What is your question? Are you having a hard time with the modulo operator (%)? How are you with long division?

Thank you for responding mtf i ended up solving it after a while and i have completed it though not completley understanding it as i’m only 13 years of age, but of course my account is monitored by my parents, or that would be an immediate violation of codecademy rules

13 is the minimum age, so you’re okay. Just be wise on the forums and do not divulge any personal information (in DM, either). Protect your privacy.

I don’t know how arithmetic is taught in school theses days. Is there any paper and pencil work? Can you work this out on paper?

  _____
4 ) 27

That is the longhand division technique for, 27 ÷ 4. It results in a quotient on the right (or above) and a Remainder on the bottom. The remainder is the modulo.

27 / 4  =>  6 plus the remainder in decimal form (0.75)

27 % 4  =>  3

         6  (whole number quotient)
      -----
    4 ) 27
       -24  (6 X 4 subtracted from the Dividend)
      -----
         3  (Remainder)

Notice that 4 X 0.75 is equal to 3?


Let’s try a tougher one…

5040 ÷ 35

   -------
35 ) 5040  |  100
    -3500
    ------
     1540  |   40
    -1400
    ------
      140  |    4
     -140
    ------   ------
        0  |  144

Since there is no remainder we know that 5040 % 35 is equal to 0 and the quotient is an integer, not a float (decimal number).

I understand the division and now i know that the remainder is modulo but i don’t understand how they’re wanting me to use it in the question, the instructions are confusing and Though i completed this i want to understand it, can you please help.

Three things to understand about modulo:

  1. it is always less than the dividend
  2. it is never less than zero
  3. it is cyclic, meaning the sequence of modulo outcomes repeats.

It is the third point that this exercise is leveraging to divide up a number of players into four groups.

Players number 3, 7, 11, 15, 19, 23 and 27 will all be on the same team, number 3 since all of those numbers have 3 remainder when divided by 4.

3 Likes