小编sud*_*nce的帖子

==和比较的逻辑是什么?

所以我在寻找问题并遇到了这个最近的问题

答案很简单,但我注意到的一件事是它实际上确实返回了SPAM精确匹配

所以这段代码

text = 'buy now'

print(text == 'buy now' in text)  # True
Run Code Online (Sandbox Code Playgroud)

返回True,我不明白为什么

我试图通过将括号放在不同的地方来找出答案

text = 'buy now'

print(text == ('buy now' in text))  # False
Run Code Online (Sandbox Code Playgroud)

返回False

text = 'buy now'

print((text == 'buy now') in text) # TypeError
Run Code Online (Sandbox Code Playgroud)

提高TypeError: 'in <string>' requires string as left operand, not bool

我的问题是这里发生了什么以及为什么会这样?

聚苯乙烯

我在 Ubuntu 20.04 上运行 Python 3.8.10

python logic boolean python-3.x

1
推荐指数
1
解决办法
36
查看次数

标签 统计

boolean ×1

logic ×1

python ×1

python-3.x ×1