简体   繁体   English

python 中的“大于”或“等于”与“等于”或“大于”

[英]“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:而以下会引发 SyntaxError:

foo = 1

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

why does it make a difference in what order you use the comparison operators?为什么使用比较运算符的顺序会有所不同?

>= 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.据我所知,使用比较运算符的最古老的编程语言是 FORTRAN 和 COBOL,它们都遵循>= / <=约定。

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. Python 的原则是“应该有一个,最好只有一个,明显的做事方式”,但在我所知道的所有其他语言中也是如此。 Notably, => has an entirely different meaning in some other programming languages, most notably Javascript, where it denotes a lambda expression.值得注意的是, =>在其他一些编程语言中具有完全不同的含义,最值得注意的是 Javascript,它表示 lambda 表达式。

The simple answer: It is just how the syntax works.简单的答案:这就是语法的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM