在python中是否有不小于或不大于的操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37862030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:01:14  来源:igfitidea点击:

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

pythoncomparison-operators

提问by Peaceful

Consider a following snippet:

考虑以下片段:

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

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

本质上,我想做一些事情a不是消极的。如果不是这样,我想要在ais not时做某事0,我会简单地写:

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

假设 Python 的一致性,用户一旦习惯了该语言就开始猜测正确的实现。我很惊讶地看到 Python 中不存在这个特定的操作!我的问题是,为什么这么简单的事情没有在 Python 中实现,是否还有另一种实现方式。任何反馈都受到高度赞赏。谢谢

回答by Michael Zhang

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

而不是a == 0 or a > 0你可以使用a >= 0.

https://docs.python.org/library/stdtypes.html#comparisons

https://docs.python.org/library/stdtypes.html#comparisons

回答by DeepSpace

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

我很惊讶地看到 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

if a == 0 or a > 0

It is exactly the same as if a >= 0

它与完全相同 if a >= 0

回答by Zentryn

You can use the equal or greater than operator:

您可以使用等于或大于运算符:

if a >= 0:
    print(a)

回答by user9501848

Well python !>doesn't work.But

好吧,python!>不起作用。但是

if not a > 70:

    print(' The number is Not bigger than 70')

else:
    print(' The number is DEFINITELY bigger than 70')

this surprisingly works

这令人惊讶地有效