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 trial

Python

How does the function return a true/false value?

Got the correct code for the challenge but I don't understand how the return line in the function evaluates to a boolean value. Why doesn't it evaluate to an actual integer rather than a boolean value? Which lesson in the course went over this concept?

import random
start = 5
def even_odd(num):        
    return not num % 2

while start:
    num = random.randint(1, 99)
    if even_odd(num):
        print("{} is even.".format(num))
    else:
        print("{} is odd.".format(num))
    start -= 1

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Good question!

What I don't get is how num % 2 leads to True/False.

Specifically, num % 2 doesn’t directly lead to a True or False. Since it is modulo 2, it will always return either a 0 or a 1. It is the not preceding the expression that interprets the 0 or 1 in a truthy fashion (or context). Where, not 0 Becomes True and not 1 becomes False.

Post back if you need more help. Good luck!!!

Gotcha!

I also just realized I was confused with what the % operator does - forgot it tells what the remainder is and not just division that I thought it did.

Thank for the clarification!

Hi Chailette,

Python has the concept of 'truthy' and 'falsey'. For every built-in data type some values behave as though they are True (thus 'truthy') and others behave like they are False (falsey).

it's discussed in this video and you should also check out this helpful Stack Overflow question (which includes a list of which values are truthy and falsey for each data type).

Simply put, you can substitute a truthy value when you are checking for a boolean True and get the same result. For example:

if 5:
    print("5 is truthy!")

The only practical distinction between a truthy value and a real True is that they are not strictly equal:

if 5 == True:
    print("it's True!")
else:
   print("it's not True!")

Will print "it's not True!".

Hope that makes sense

Cheers

Alex

Thanks for responding, Alex.

I actually already understood the idea of truthy or falsey values. What I don't get is how num % 2 leads to True/False. Yes, I get that any number other than 0 would be truthy, but is there any way for the % operator to be used and just result in an integer without it being interpreted as True/False? Is it because of the 'not' that leads to a boolean value?