Is there an operation for not less than or not greater than in python?

Consider a following snippet:

a = 0 if a == 0 or a > 0: print(a) 

Essentially, I want to do something when a is not negative. If instead of this, I had wanted to do something when a is not 0, I would have simply written:

if a != 0 : 

In the same spirit, I tried :

if a !< 0 : 

assuming the consistency of the Python where user starts guessing the correct implementations once he/she gets used to the language. I was surprised to see that this particular operation does not exist in Python! My question is that why such simple thing has not been implemented in Python and is there another way in which it has been implemented. Any feedback is highly appreciated. Thank you

7

7 Answers

Instead of a == 0 or a > 0 you could just use a >= 0.

0

Well python !> doesn't work.But

if not a > 70: print(' The number is Not bigger than 70') else: print(' The number is DEFINITELY bigger than 70') 

this surprisingly works

I was surprised to see that this particular operation does not exist in Python!

I'm not familiar with any language that does have this operator. It is simply not needed.

As for your snippets:

if a == 0 or a > 0

It is exactly the same as if a >= 0

1

You can use the equal or greater than operator:

if a >= 0: print(a) 

Following worked for me though (not operator does not work, using tilde ~, doesn't work):

((-1 > 0) or (0 > 0)) == False 

Result:

True 

I finally worked out the answer to this one:

if not (a > b)

And it works correctly! I must be dumb to have taken so long to get there!

Dave.

You could look into the "not" operator with a > or < expression.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like