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

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

考虑以下片段:

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

本质上,我想在a不为负的时候做点什么。如果不是这个,我想在 a 不是 0 时做一些事情,我会简单地写:

if a != 0 :

本着同样的精神,我试过了:

if a !< 0 :

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

您可以使用 a >= 0.

而不是 a == 0 or a > 0

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

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

我不熟悉任何有此运算符的语言。根本不需要。

至于你的片段:

if a == 0 or a > 0

if a >= 0

一模一样

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

if a >= 0:
    print(a)

嗯python!>不会work.But

if not a > 70:

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

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

这出奇地有效

您可以使用 > 或 < 表达式查看“非”运算符。

尽管以下对我有用(not 运算符不起作用,使用波浪号 ~,不起作用):

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

结果:

True

我终于找到了这个问题的答案:

如果不是 (a > b)

而且它工作正常!花了这么长时间才到达那里,我一定很愚蠢!

戴夫。