Skip to content
Advertisement

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

Suppose I have this code:

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

That is: I want to do something when a is not negative.

I know that I can write if a != 0: to check whether a is not equal to 0.

So, I tried using if a !< 0:, following similar logic. However, this is apparently not supported:

>>> if a !< 0:
  File "<stdin>", line 1
    if a !< 0:
         ^
SyntaxError: invalid syntax

Why is this syntax invalid? What can I use instead to simplify the conditional?

Advertisement

Answer

Instead of a == 0 or a > 0, simply use a >= 0.

See https://docs.python.org/library/stdtypes.html#comparisons for a complete list of available comparison operators.

Advertisement