pythonboolean-logic

"Greater than" or "equal" vs "equal" or "greater than" in python


why is it that we use "greater than" or "equal", rather than "equal" or "greater than"?

foo = 1

if foo >= 1:
  print("Greater than 1")
>>> Greater than 1

while the following would raise a SyntaxError:

foo = 1

if a => 1:
  print("Greater than 1")

why does it make a difference in what order you use the comparison operators?


Solution

  • >= is one operator, not two. Same with <=. As for why the order is the way it is in modern programming languages, the answer is just 'convention'.

    The decision to make it >=/<= rather than =>/=< is by convention, and is common among nearly all existing programming languages that use comparison operators at all. The oldest programming languages that used comparison operators, to my knowledge, are FORTRAN and COBOL, both of which follow the >=/<= convention.

    I dunno if there was more design rationale behind it at the beginning, besides that in mathematics we say "greater than or equal to", rather than "equal to or greater than", and thus >= more accurately reflects that.

    As for why => and =< are not valid, it's mostly to avoid redundancy and/or confusion. Python has a principle that "there should be one, and preferably only one, obvious way to do things", but this is also the case in every other language I know of. Notably, => has an entirely different meaning in some other programming languages, most notably Javascript, where it denotes a lambda expression.