Python conditional question?

I am here, in this lesson, on this course page.

For REFERENCE, as per the course material:

statement_two = not (8 * 2) != 20 - 4

I was playing around with the example code to try to undertand the material better and am getting mixed results. Can anybody explain what’s going on here because what I am seeing is to making any sense to me!!!

This conditional doesn’t print anything. Why? (My thinking is it should print SOMETHING, because it can not be neither, it HAS to be one or the other. So then why is it not printing ANYTHING? )

if statement_two:
  print("true")
else:
  print("false")

While THIS if statement written DIRECTLY below the one above, DOES print, and prints false! Why? (My thinking is that both the first if statement above and the one below ask the same thing, if statement two is true then print something. If that is the case then why is only the second one printing something. I am asking if statement two is true, the second if statement is saying it is true (because it printed something) and is printing false as a string. The first one is saying it is not true by not printing anything - ie skipping it, so then in the first set of if statements the else should be printing…I don’t know what is going on here!! Somebody Please Help!!)

if statement_two:
  print("false")

Adding == True: to all of them like if statement_two == True: (True without quotes as a boolean) doesn’t change anything. It exhibits the exact same behavior as above.


Aren’t
if statement_two:
and
if statement_two == True:
Saying the same exact thing?


Also, aren’t
not 1 + 1 = 2
and
1 + 1 != 2
Saying the exact same thing??


Thank you to anyone who responds, in advance!!

Hi,
not works at the beginning of a statement and flips what comes after it. So, while;
not 1 + 1 == 2
and
1 + 1 != 2
evaluate to the same thing, they do so for slightly different reasons.
The first says;
It’s True, 1 + 1 is equal to 2, and now let’s flip it to be False.
The second says;
No, 1 + 1 is equal to 2, so that statement is False.

So, for statement_two. What you have is;
8 * 2 != 20 - 4 (or 16 != 16), which is False.
The not at the start then flips that result over to True.

I’m not sure why your statement didn’t print (possibly an indentation error ?, the code here seems fine), and you’re right about;
if statement_two == True
being practically the same as
if statement_two
as you’re always going to get the same answer.

Hope that helps (and hasn’t just confused you more…)

1 Like

Thank you! I thought I was going crazy, lol. Must be something with codecademy website.

Just wondering, is there a specific reason one might need to use one instead of the other when it comes to using the not vs != , can you think of a realistic example where it would be necessary to use one instead of the other?

Thanks so much for taking the time to clear all that up! I will have to read more about it…

Just to butt in…

They are very different operators. != is a negated equality comparison. It does not have any logic built into it. not has logic built in. We should be clear on the difference so these two operators are never confused.

1 Like

As @mtf says, != is basically used for comparing two items. So;
x != y
whereas not is usually used where you want to know if something is False. e.g.;
if not (light_is_on()):
might be checking that the light is off as opposed to on.

It’s one of those things that comes a little easier the more you use it (but still very easy to tie yourself in knots).
I try to keep things as straightforward as possible. Easier to read, as well as to check when it goes wrong.

have fun

2 Likes