[英]Python - 'if not'
The following codes 以下代码
multiples = []
for i in range(1,1000):
if i % 3 == 0 or i % 5 == 0:
multiples.append(i)
addition = sum(multiples)
print addition
and 和
print(sum([i for i in range(1, 1000) if not (i%3 and i%5)]))
do the same thing. 做同样的事。
Now how does the if not
part compute in the second code? 现在,第二个代码中的
if not
部分如何计算?
What I'm saying is, in the first code the i % 3 == 0 or i % 5 == 0
had to be exclusively stated whereas the same thing is achieved on the second code without the == 0
. 我的意思是,在第一个代码中,必须专门说明
i % 3 == 0 or i % 5 == 0
,而在第二个代码中,无需== 0
即可实现相同的目的。
Using De Morgan's laws : 使用德摩根定律 :
i % 3 == 0 or i % 5 == 0
is the same as: 是相同的:
not (i % 3 != 0 and i % 5 != 0)
And in python, when converting a number to a boolean, any non-zero value becomes True
. 在python中,将数字转换为布尔值时,任何非零值都将变为
True
。
So instead of doing i % 3 != 0
in the if
, you can just use i % 3
, because if it's 0
it'll be False
and if it's non-zero it'll be True
. 因此,而不是做
i % 3 != 0
中if
,你可以使用i % 3
因为如果它的0
它会False
,如果它是非零这将是True
。
Here's python's truth table: https://docs.python.org/3.6/library/stdtypes.html#truth 这是python的真值表: https : //docs.python.org/3.6/library/stdtypes.html#truth
PS sum()
can take a generator as an argument, so you can actually just do: PS
sum()
可以将生成器作为参数,因此您实际上可以这样做:
sum(i for i in range(1, 1000) if not (i%3 and i%5))
and
and or
are boolean operators, not logical &
and |
and
和or
是布尔运算符,不是逻辑&
和|
. 。 So writing
所以写
a == 0 or b == 0
is the same as writing 和写作一样
not a or not b
So they do the same thing 所以他们做同样的事情
As a conclusion, the best way is to avoid negations in your conditions, don't create extra list comprehension but use generator comprehension instead, and I wouldn't mind testing against zero instead of using not
since they're integers after all. 作为结论,最好的办法是避免你的条件否定,不产生额外的列表理解,但用发电机的理解,而不是,我不会介意的测试对零,而不是使用
not
因为他们是整数毕竟。 I would do this: 我会这样做:
print(sum(i for i in range(1, 1000) if i%3==0 or i%5==0))
This is an effect of the way Python converts integers to booleans. 这是Python将整数转换为布尔值的方式的影响。 The result of
i % 3
is 0, 1, or 2, depending on the value of i
. i % 3
的结果取决于i
的值是0、1或2。 In Python 3.x (and by default also in Python 2.x, unless you've reassigned them) all non-zero integers evaluate to False
and zero evaluates to True
. 在Python 3.x中(并且默认情况下在Python 2.x中也是如此,除非您已对其进行了重新分配),所有非零整数的值均为
False
,零值的值为True
。
Thus if not (i % 3)
will evaluate (i % 3)
to an integer 0, 1, or 2, and then not
will convert these to False
, True
, True
. 因此
if not (i % 3)
将评价(i % 3)
为一个整数0,1或2,然后not
将这些转换为False
, True
, True
。
In Python 0
is usual interpreted as false. 在Python中,通常将
0
解释为false。 So in the first code block i % 3 == 0
is effectively equivalent to not i % 3
. 因此,在第一个代码块中,
i % 3 == 0
有效地等于not i % 3
。 Examples from command line: 命令行示例:
>>> 6 % 3 == 0
True
>>> not 6 % 3
True
>>> 7 % 3 == 0
False
>>> not 7 % 3
False
This shortcut of 0 === false
appears in many languages and can be a bit confusing if you're not used to it. 0 === false
这个快捷方式在许多语言中都会出现,如果您不习惯它,可能会感到困惑。
In Python every object has a boolean value. 在Python中,每个对象都有一个布尔值。
In the case of integers any of them is True
except 0
. 在整数的情况下,除
0
以外,它们中的任何一个都是True
。
So in your case when number i
can be divided by 3
the expression i % 3
will be 0
. 因此,当您将数字
i
可以除以3
,表达式i % 3
将为0
。 But we know that 0
is False
. 但是我们知道
0
是False
。 The not
operator will negate this expression so not False
will become True
. not
运算符将否定该表达式,因此not False
将变为True
。
The same holds for not i % 5
as well. 同样,
not i % 5
也适用。
Hope it helps. 希望能帮助到你。
Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.
可以测试任何对象的真值,以在if或while条件中使用,或用作以下布尔运算的操作数。 The following values are considered false:
以下值为“假”:
None
没有
False
假
zero of any numeric type, for example, 0, 0L, 0.0, 0j.
任何数字类型的零,例如0、0L,0.0、0j。
any empty sequence, for example, '', (), [].
任何空序列,例如'',(),[]。
any empty mapping, for example, {}.
任何空映射,例如{}。
instances of user-defined classes, if the class defines a nonzero () or len () method, when that method returns the integer zero or bool value False.
用户定义的类的实例,如果该类定义了一个非零 ()或len ()方法,则该方法返回整数零或布尔值False。 [1]
[1]
All other values are considered true — so objects of many types are always true.
所有其他值都被认为是真实的-因此许多类型的对象总是真实的。
See also: https://docs.python.org/2/library/stdtypes.html 另请参阅: https : //docs.python.org/2/library/stdtypes.html
see, i % 3 == 0 return true if the condition satisfy. 请参见,i%3 == 0如果条件满足,则返回true。 Now, in python suppose:
现在,在python中假设:
i = 6
6 % 3 = 0 and 0==0 is true
There fore the result will be true. 因此,结果将是正确的。 In the second case
在第二种情况下
6 % 3 = 0.
Now, in boolean 0 means false. 现在,布尔值0表示错误。 So by using not it means not false, ie, true.
因此,使用not表示不为假,即为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.