Trouble with "not" operator


"not" operator

File "python", line 1
bool_one = 5 + 3 == 11 not 3 * 4/2 > 8
SyntaxError: invalid syntax

I'm really struggling to understand the "not" operator. I can figure what is true and what is false, but I'm stuck here. What application is this used for? If I can visualize it I can put it into action. Any help is appreciated!

bool_one = 5 + 3 == 11 not 3 * 4/2 > 8

bool_two = 3**4 < 4**3 not 6**4 != 2**4 

bool_three = 10 % 3 <= 10 % 2 not 9**4 > 5 * 4

bool_four = 3**2 + 4**2 not 6**8 < 5 - 4

bool_five = 3**2 < 5 + 4 not 5 - 6 > 72


not only negates and as such cannot have two operands.

>>> a = 1
>>> b = 0
>>> a not b
SyntaxError: invalid syntax

Once we express not as a boolean toggle of the expression on its right, b, then it's only too obvious why the above syntax is not valid.

c = not b
print a c

See where the operator drops out?

A boolean toggle? What?

Regardless what form an expression takes, its not value will always be a boolean. We essentially convert any expression to boolean just by not'ing it.

not 1     # False
not 0     # True
not "a"   # False
not ""    # True

I don't know whether to go any further with this, or if you are catching on and coming up with your own ideas. Think I'll leave it here for now, unless there are more questions.


That's given me more than enough to go on. I want to challenge myself to take it from here. I appreciate your contributions not only to this post, but on other ones as well.


I don't know if this qualifies as parodoxical, but when you have sufficiently completed this exercise, take a look at this study example (not a solution for anything).

>>> 1 and not 0
>>> not 1 and 0
>>> not 0 and 1
>>> 0 and not 1

Construct a similar example using or and see whether or not it parallels this.

>>> 1 or not 0
>>> not 1 or 0
>>> not 0 or 1
>>> 0 or not 1

We're still missing the 0 : 0 and 1 : 1, which should be interesting to work up.

Couldn't wait...

>>> 1 or not 1
>>> not 1 or 1
>>> not 0 or 0
>>> 0 or not 0
>>> 1 and not 1
>>> not 1 and 1
>>> not 0 and 0
>>> 0 and not 0


Extra study

To make it even more interesting,

f(x) or not g(x)

not f(x) or g(x)

and so on... How might we get this logic to play into our selection of functions in a dynamic state?

>>> def f(x):
	return x

>>> def g(x):
	return x

>>> a = 1
>>> b = 0
>>> f(a) or not g(b)    |
1                       |____ short-circuited
>>> f(a) or not g(a)    |     g(x) is ignored
1                       |
>>> f(b) or not g(b)
>>> f(b) or not g(a)


I find myself going down rabbit hole of Boolean logic now.

Here's where I'm at:

5 > 2, 5 != 2, 5 == 5, and so on. I guess I'm not quite where I need to be in terms of expanding that into the more complicated aspects.

Now I'm starting to understand more and more that it's not about what language you target, but understanding the logic of programming. I don't know when and if there's an "aha" moment, but I'm trying and I enjoy the work.


And so we have a broad and deep ocean of available programming languages, each tailored to their own unique application paradigms. Some languages work better for some things than others, but then those others work better for something else. It's a never ending shift of paradigms.

I am no expert at any language but just love playing with what you just discovered. Hope you'll keep that thought with you for a long time to come. It will erase all bias, eventually, and keep you from becoming a pragmaticist like me.


I am assuming that is not actual code, but pseudo-code, only to express your idea.

5 > 2    # True   5 is greater than 2

5 != 2   # True   5 does not equal 2

5 == 5   # True   5 and 5 are identical

Anytime you see an = sign, it signals something definitive in the scope. Without it, we signal inequality, in direction or value.

1 < 2
1 != 2

These are just outright facts. But in our program we will want it make decisions based upon dynamic data...

n > 2

n != 2

n == 5

Each carry their own particular meaning. Something to think upon.


That's the extent of my understanding at this point. How that ties into the operators is what's escaping me right now, I guess.


5 > 2 and 2 > 5 # false

5 > 2 and 2 < 5 # true


5 > 2 or 2 > 5 # true

5 < 2 or 2 > 5 # false

These make sense to me. Both operands must be true for a true statement with and, one must return true with or, but not is where I've hit a wall. What you illustrated I couldn't wrap my head around so I feel guilty for even having you put the time in.


False for the sake of getting in the habit.


This is what gets my wheels turning.


Don't. It's my time, and this is what I choose to do with it. Focus on what you are learning and let me care about my time.

Let's find that wall and see if you mightn't scale it with confidence.

5 > 2 or not 2 > 5

What do we expect?

Look at the not first, to see if you can evaluate it immediately, or need to wait for some preliminary result. In this case we can evaluate not immediately, so we do that.

not 2 > 5  =>  not False  =>  True

So now we have,

5 > 2 or True

Actually, this sucks as an example because 5 > 2 short-circuits and we never see the other operand. It is ignored because of the True return from the first operand. But we can dream up other examples. Can you come up with a couple of your own?


12 / 4 < 5 - 3 or not 7 * 3 == 11 + 10

Every exercise I've done has shown some form of extended math. Hopefully if anything I'm strengthening my mental math muscles.


Math only gets strengthened if that is what you pursue, not as a result of learning programming. They are not interdependent disciplines. We build our strength in the areas we choose.

Do you like cosmology? Then learn the math, and use programming to help you do the repetitive or on demand things that relate to cosmology.

Do you like physics? Or chemistry? or biology? or heaven forbid, math? You can tailor your programming to that chosen discipline. That's the beauty of it, and also why there are so many languages, as previously coined.


I like math, and I've always been comfortable with it. I think I had this fear that I would need to know advanced calculus to learn programming, albeit a silly fear. That's the first I've heard of the two not having to be intertwined, and that's refreshing.


I LOVE IT. Even with my rudimentary knowledge I can still understand the esoteric nature of it. For the same reason I love physics. My inkling of knowledge is enough for me to appreciate it. That's my joy in life. Not having to be an expert. Just doing the best I can to understand.


Took a day off to come back with a fresh outlook. Nailed it. Thanks again!


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