Trouble with NOT


#1

Hi mate, I´ve camed to something I don´t really understa, could you or Tonya please help me? (5+3<=8) not (“Charlie” == “Delta”) How do I properly use not or just make this example right? (Conditionals & Control Flow

Mix ‘n’ Match) from 10/15 https://www.codecademy.com/courses/learn-python/lessons/conditionals--control-flow/exercises/mix-n-match?action=lesson_resume&link_content_target=interstitial_lesson

Ok I think it was a little bug or something like that, because now it is false, tough before it game me some error…


Doubt Python, why 15.0? (I´m a begginer, it´s not spam or anything, i just want to know)
#2

Let’s take a look at the problem…

expression not expression

is invalid code.

>>> a = 1
>>> b = 0
>>> a not b
SyntaxError: invalid syntax
>>> a and not b
True
>>> a or not b
1
>>> 

Whoa! What is going on here? We can get to that in a minute. First let’s examine the way that not is implemented.

It’s already been shown that expression not expression is invalid, but when we join the two expressions and make not part of the second expression it is valid and logical.

Above, a and b are expressions, as is not b. When we write,

a and not b

the computer casts everything to its truthy equivalent.

1  =>  True
0  =>  False

True and not False => True and True => True

Okay, that makes sense, right?

To understand not, think of it as casting what ever value it is paired up with to a truthy, or boolean.

not 1      => False

not ""     => True

not not 0  => False

not not 1  => True

As I’ve already suggested, study this topic until you get a handle on it. Will it wrack your brain? Most certainly. It does get easier, though, once some understanding creeps in. Ignore this subject at your own peril. Logical operators are at the heart of programming.


#3

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