Python Conditionals


#1

https://www.codecademy.com/courses/learn-python/lessons/conditionals--control-flow/exercises/mix-n-match?action=lesson_resume

The exercise does not allow me to use “not False” as a boolean or part of one. Some problem with the syntax Is it because of the redundancy of using so many letter(of “not false”) instead of (“True”)?. If so then why allow “not True”? or not not not not True as I´ve seen in a previous python exercise?

Thanks!


#2

not False is a valid expression in python, if you had a syntax error it was somewhere else

syntax error means that your code simply cannot be read as a program, similar to if you hand in an english essay and it’s written in hieroglyphs


#3

well I typed in “True==not False” to get a true statement on the exercise I offered the link to. It told me there was an error on the line that was at. and I changed it to True==True and that was that… but I was left wondering where I might have gone wrong.


#5

True==not False on the other hand is invalid syntax, which I agree is strange at first, but has to do with that == is evaluated before not which makes True == not False evaluated in this order: (True == not) False which doesn’t make sense - syntax error

(I’ve never run into this myself, if that’s any indicaton of that you wouldn’t want to write something like this anyway)


#6

I had to think a while about why I had not run into this myself:


You shouldn’t be testing if two booleans are equal to each other - rather you should be combining results using and/or

So you should not do:

if is_green == True:
    ...

Instead, you should do:

if is_green:
    ...

or

if not is_green:
     ...

Because what have you really accomplished by comparing a bool to another bool? You’ll end up with another bool, which is where you started


#7

I understand that, yet True==True works perfectly fine, and so does False==False. Also True !=False works. Check for yourself.

Write:

bool_1= True!=False
if bool_1==True:
print "Valentine is a genius!"
else:
print “ionatan is right! You shouldn’t be comparing booleans!”

Also… not True==False works fine. I assume it´s an error from the site?


#8

No error anywhere. Just a piece of code that is particularly weird that doesn’t happen to be valid because of operator precedence

It has to be evaluated in some order, and given the order that is used, there is no way to evaluate True == not False

But you can use parentheses to change that order

And then there’s the question of whether it SHOULD be valid, to which I argue that it isn’t useful, so there’s no reason for it to be different


#9

No sense? How do you mean? All statements that you put in the place of True and/or false are going to be booleans in nature anyway… “If 8>7==True” is the same as True ==True because the information that is being taken away from 8>7 is the truth value… I think. I have started this programming adventure yesterday… once again. I’ll try not to quit…


#10

And as I said… “not True==False” works perfectly fine. So why doesn´t “True==not False”?


#11

Because True == not doesn’t make sense.

== happens before not

True == not False
(True == not) False


#12

It seems that you are right as I´ve typed “False==not True” and the same syntax error appeared. But it is counter-intuitive that the == sign doesn´t separate the two propositions.
Anyhow… You have explained it. Thanks!


#13

Yes. Counter-intuitive in a case that should be written some other way anyway, not because the language doesn’t allow it, but because it’s weird by itself, someone would actually cringe a bit when reading it.

…I can see how this looks weird from your perspective. But it isn’t deserving of being considered a wart, python doesn’t deserve that for this, it isn’t.

As humans we’re quick to assume that things which don’t match our way of thinking are wrong. Even when it doesn’t matter.


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.