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 If This Then That

if not in python

Hello,

In the program below, I dont get this; if not age > 25000:

age = 20000
if not age > 25000: 
       print('whipper snapper')

Also, is this considered short circuit?

thanks

Is this the code you are referencing?

if not age > 25000:
    age = 20000
    if not age > 25000:
        print('whipper snapper')

If this is the entire code, it won't run. It's checking age against 25000 before age is defined, which will give you an error. Even if age is defined prior, the code won't give you errors. If age is defined and less than 25000 it will print "whipper snapper" and set age to 20000.

# Age defined above.
if age <= 25000:
    print('whipper snapper')
    age = 20000

This code will do the exact same thing.

fixed code formatting

orange sky,

Check this thread for how to post code: https://teamtreehouse.com/forum/posting-code-to-the-forum

jacinator,

I think you misinterpreted the code because of the lack of formatting. I fixed the formatting if you want to try answering again.

Thanks Jason Anello, that is more than probable.

3 Answers

orange sky, the line of code that you are asking about is not the simplest format.

It could also be written if age <= 25000:. That would be simpler, but I don't know the context that it is rising out of.

The line checks if age is not greater than 25000, or age is less than or equal to 25000.

Adam Fournace
PLUS
Adam Fournace
Courses Plus Student 4,115 Points

Not equal is !=

Example:

if  j != k:
  print("not equal") 

I agree with Adam Fournace in that it's easier to understand

age = 20000
if not age > 25000: 
       print('whipper snapper')

by thinking of "if not age > 25000" as "if age IS NOT > 25000" (or like the != comparison operator in javascript).

Also, to answer your other question, I don't believe that's technically an example of a short-circuit evaluation, because there's no other conditions included in the if statement. The interpreter is checking for one condition...age > 25000 or not. If you had another conditional statement joined to it with "or" or "and", or if the code block used one of the built-in functions any() or all()...THEN I think you'd see an example of short-ciruiting.