gpt4 book ai didi

python - and 运算符如何在 python 中工作?

转载 作者:行者123 更新时间:2023-11-28 19:54:38 26 4
gpt4 key购买 nike

你能解释一下在 python 中是如何工作的吗?我知道什么时候

x  y  and
0 0 0 (returns x)
0 1 0 (x)
1 0 0 (y)
1 1 1 (y)

在解释器中

>> lis = [1,2,3,4]
>> 1 and 5 in lis

输出为假

但是,

>>> 6 and 1 in lis

输出为真

它是如何工作的?

在我的程序中只有当两个值都在列表中时才必须输入 if 条件的情况下该怎么办?

最佳答案

尽管有很多相反的论点,

6 and 1 in lis

表示

6 and (1 in lis)

意味着:

(6 and 1) in lis

page Maroun Maroun 在他的评论中链接到表示 and 的优先级低于 in

你可以这样测试:

0 and 1 in [0]

如果这意味着 (0 和 1)在 [0] 中,那么它将评估为 true,因为 0[0] 中.

如果它表示 0 and (1 in [0]) 那么它将评估为 0,因为 0 为假。

它的计算结果为 0

关于python - and 运算符如何在 python 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35288845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com