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) 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)