FAQ: Learn Python: Syntax - Modulo


#1

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!


#3

Hello to all, I didnt really get how this modulo calculation goes. Could someone shed some light on it for me please?

How will the remainder of a division tell me which team I am on if it doesnt even mention how many people in total are being divided by the 4 groups?


#4

Recall how we learned long division in around grade 4.

   ______
15 ) 230

Which we would read, fifteen into two-hundred-thirty. The first step would be to divide 15 into 23.

      1
   ------
15 ) 230
   - 15
   ------
      8

We see it goes one time, so we multiply 1 times 15, and subtract that from 23. This leaves 8. We next bring down the zero in 230, and divide again.

      15
   ------
15 ) 230
   - 15
   ------
      80
    - 75
    -----
       5

In the language of the day we would have read this as, “fifteen goes into two-hundred-thirty, fifteen times with five remainder.”

The remainder is the modulo of a division operation. We often see % described as the remainder operator.

    230 % 15  =>  5

#5

I get what Modulo is and how it is calculated, but I don’t get why I would be on team number 3 if I was number 27. If I was number 27 and each team had 4, then I should be on team number 7 shouldn’t I? The instructions seem to imply that my_team is equal to my team number. Instructions below:

You’re trying to divide a group into four teams. All of you count off, and you get number 27.

1. Find out your team by computing 27 modulo 4. Save the value to my_team .

2. Print out my_team . What number team are you on?

3. 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?


#6

Yeah, it’s not teams of 4, it’s 4 teams, but you still need to know the total number to tell you anything. This is a poorly written exercise. I understand how modulo and division works. Just not why the question is phrased the way it is, as it’s impossible to answer. I’ve submitted a bug report.


#7

Its not just the exercise that is poorly written. The whole lesson is.

“If the number is divisible, then the result of the modulo operator will be 0.”
What does that even mean? Divisible by what? 5 /2 in python will still give you 2.5 so 5 is still divisible by 2 it wont just be an int.

“The modulo operator is useful in programming when we want to perform an action every nth-time the code is run.”
How? won’t be that treated via a loop and an if statement?


#8

Thank you guys, someone just explained me it last night and now I get the logics behind it. So I will leave in here an easy way of understanding the logics behind it as well as where I was mixing things up.

Basically I was thinking “How can I know which team will I be if I dont know how many people we are in total?”. The division I was doing was, if for example we have 100 people, we divide them 100 by 4 and the first 25 people will go into group 1, the next 25 (from 26 until 50) into group 2, the next 25 (from 26 til 75) into group 3 and the remaining people into group 4.

The explanation I was given is that this calcultion should be based on logics, and always the most effective way of doing things is also the most simple of it. The way I was doing it needed an extra answer (total number of people) but the optimal way of doing it would be for example when in physical education class the teacher divides the students into teams. So let’s say 10 kids divided into 3 teams. The teacher wouldnt divide 10 by 3, many times he wouldn’t even need to count the total number of kids. He would simply say: “You go into team number one, now you, the next in line, go into team number 2, next one into team number 3, now you again into team number 1, next into team number 2, next one into team number 3, now you again into team number 1, next one into team number 2 and next one into team number 3… wait, that’s 9 kids but there is one child remaining, so you go into team number 1”.

So again, the most effective way of doing something is usually the simple way, and by having to ask extra questions to be able to solve the problem you are already adding a degree of complication to it.


#9

These were both written a bit confusing, but they make sense if you understand the math/syntax.

On your first example, when it says divisible, it means only divisible into integers/whole numbers. 5 % 2 = 2 + 2, modulo/remainder of 1. So even though technically you can do 5/2 and receive a float of 2.5, if you wanted to use the remainder for something, you could perform 5 % 2 to figure that out.

On your second example, it’s saying that if you wanted to run your program for example, 100 times, but you wanted it to take action every 5 times, you could check to see if your number is an even multiple of 5. Like so:

for x in 100:
#what to do every time.
if x % 5 == 0:
#what to do every 5th time

So since 20 % 5 == 0, when you get to the 20th iteration, it would run a command. Same at 25, 30, etc.


#10

Thanks, this makes total sense now! Every 4th kid is on team 4, every 3rd kid is on team 3, etc.


#11

‘divide a group into four teams’ is not equal ‘4 people in each team’. This question would made sense if it changed to ‘4 people in each group’. If so, you were in team 7: 27//4=6;27%4=3.

It DOES needs total people for the original question as you mention.


#12

You’re trying to divide a group into four teams. All of you count off, and you get number 27.

Find out your team by computing 27 modulo 4. Save the value to my_team .

Each group of four kids, taken in order of count off, will each be on a different team.

Not quite. Starting anywhere in the sequence, every fourth kid will be on the same team. There is no ‘every 3rd’ or every 2nd. The whole thing revolves around division by 4. The remainder determines which team a player is on.

MOD    TEAM
 1       A
 2       B
 3       C
 0       D

What are all the MOD 1?

1 5 9 13 17 21 25 29

What are all the MOD 2?

2 6 10 14 18 22 26 30

What are all the MOD 3?

3 7 11 15 19 23 27 31

What are all the MOD 0?

4 8 12 16 20 24 28 32

#13

exactly - if one were to say every 3rd kid will be in team 3, that would suggest that kid-6 would be in team 3 (3;6;9;12;15;…), however this is not true as we are working with 4 groups/teams (4;8;12;16;20;24;28;…), therefore the correct statement would be, ‘every 3rd kid within each grouping of 4 will be in team 3’ i.e. kid 7 is in the second grouping of 4 and is representing the third ‘quartile’/group, and is therefore in group 3.


#14

4 posts were split to a new topic: Im feeling disrrespected here


#15

Is there a way for Python to easily calculate the team assignments?


#16

No. Python is like any computer… dumb. We tell the computer what to do via the Python languge.


#17

Well, that’s what I mean. Codecademy asks this as “food for thought”. Just wondered what the program would look like to calculate this.


#18

Can we do it with Python? Probably yes. Does Python do this? Probably, no. Python is a tool not a solution.


#24

I’m not really sure what the last part:

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?

is asking in terms of “What are the numbers for all 4 teams?”. Is the desired values 6, 12, 18, 24? And I’m to write in 27%6? 27%12? But they’re the same, can I just get some clarification?

I wrote the following and I was able to hit next, but I don’t see how my answer was sufficient.

my_team=27%4
print(my_team)
person1=26%4
person2=28%4
teams=4%27
print(person1, person2, teams)


#26

“What are the numbers for all 4 teams?”
What exactly is this asking? And given the explanation how do we code it?
Thanks.


#27

What are all the MOD 1?

1 5 9 13 17 21 25 29

What are all the MOD 2?

2 6 10 14 18 22 26 30

What are all the MOD 3?

3 7 11 15 19 23 27 31

What are all the MOD 0?

4 8 12 16 20 24 28 32