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 Python Basics (2015) Logic in Python You Can't Handle the Truth!

Dillon Shaw
Dillon Shaw
6,400 Points

Isn't True 0 and False 1?

It's been a while since I've worked with any programming language but I know that C/C++ programs, don't they return 0 on success and 1 on failure? Is Python switching this around for some reason or is success and failure for a program different from "truthiness"?

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

If I remember correctly (and it's been ages) so don't hold me to this, but in old school ANSI C you could return a 0 to indicate a normal exit from main. That was true even when main had a return type of void. But when I had it at university we were always told not to return 0 or 1 and instead return EXIT_SUCCESS which upon a failed exit of the function would result in an error code that you could then look up later.

But generally speaking, nowadays, 0 is considered "falsey" and any non-zero number is considered "truthy". Someone correct me if I'm wrong here! Thanks :thumbsup:

edited for additional note

This is not exclusive to Python. This also includes JavaScript, Java, and PHP (and probably others).

Eduardo Calvachi
Eduardo Calvachi
6,049 Points

Dillon Shaw This boils down to what computers actually understand, after a prog language has been compiled or interpreted all the way down it, eventually gets to binary instructions in bytes(groups of bits). A bit can have either a 0 or 1 value. 0 represents off and 1 represents on. Since True and False are boolean operators with only 2 possible values, it makes sense to match them to the value that is actually stored in the computer memory.

Dillon Shaw
Dillon Shaw
6,400 Points

Oh okay and perhaps this is also related to what the video mentioned about None also being an option for boolean. (EXIT_SUCCESS no error)