Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKevin Ohlsson
4,559 PointsHow does 'if (number % 2 == 0)' work in this example?
def even_odd(number):
if (number % 2 == 0): # <------ how does this work, what happens here? Check if the remainder of number 2 becomes zero?
return True
else:
return False
What i find confusing is how the arguments are inputted, if i were to read this is would read: if number is modulo 2 is equal to zero. Is that correct 'reading' of what the code does?
2 Answers
Jennifer Nordell
Treehouse TeacherHi there! I'm just going to expand a little on what James has already said. I know the "modulo/modulus" operator can be confusing for some, so let's not use it when we're reading it out loud. You could read it this way: "If the number divided by 2 has a remainder of 0 then do this, otherwise do this". And let's take a look at some examples:
- 1 % 2 will result in a 1. One divided by two results in 0 with a remainder of 1.
- 2 % 2 will result in a 0. Two divided by two results in a 1 with a remainder of 0.
- 3 % 2 will result in a 1. Three divided by two results in a 1 with a remainder of 1
- 4 % 2 will result in a 0. Four divided by two results in a 2 with a remainder of 0.
- 5 % 2 will result on a 1. Five divided by two results in a 2 with a remainder of 1.
Seeing a pattern here? Whenever you see if( x % y == 0)
, you can read that also as "If x is evenly divisible (no remainder) by y". We say a number is evenly divisible by another when it can be divided by that number without a remainder. An odd number is any number that is not evenly divisible by 2. An even number is any number that is evenly divisible by 2.
Hope this helps!
james south
Front End Web Development Techdegree Graduate 33,271 Pointsmodulo, or mod (%) returns the remainder of division, so 8 % 5 is 3, 99 % 3 is 0, etc. if the result is 0 the numbers divide evenly, so if a number mod 2 is 0 the number is even.
Kevin Ohlsson
4,559 PointsThanks James, Jennifer's example helped me understand yours betters. It occurred to me now that i didn't really understand the relationship between True, False and mod. It also occurred to me that it's safe to assume that any kind of logic in a computer program must always be either true or false, right? Is there some kind of intermediate between True and False? Like, maybe?
I'd say that 'maybe' is comprised of the logic that 'leads up to' the final True or False. So that the 'final question' in a program is always starting with 'maybe' and the process of how information flows in the program makes the maybe True or False. If that makes any sense :)
Anyways, now the modulo concept makes sense!
Kevin Ohlsson
4,559 PointsKevin Ohlsson
4,559 PointsThank you so much Jennifer!! This explained my question perfectly!