Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Number Game App Even or Odd

Kevin Ohlsson
Kevin Ohlsson
4,547 Points

How 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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? :smiley: 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! :sparkles:

Kevin Ohlsson
Kevin Ohlsson
4,547 Points

Thank you so much Jennifer!! This explained my question perfectly!

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,258 Points

modulo, 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
Kevin Ohlsson
4,547 Points

Thanks 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!