The author recently learned two interesting quirks about Python boolean values:
Equality Comparison with Integers:
True
and False
are equal to the integer values 1
and 0
respectively when using the ==
operator.is
operator, True
is not the same object as 1
, and False
is not the same as 0
.==
) and object identity (is
).Adding True Values:
True
is equal to 1
, the author discovered that you can add True
values together like integers.True
values in a list, as demonstrated by the example:ls = [True, True, False, True, False, False, True, False]
print(sum(ls)) # Output: 4
The author finds these behaviors both strange and entertaining, and suggests readers check out their Substack post for more cool Python boolean-related insights.
翻譯成其他語言
從原文內容
levelup.gitconnected.com
從以下內容提煉的關鍵洞見
by Liu Zuo Lin 於 levelup.gitconnected.com 07-21-2024
https://levelup.gitconnected.com/i-recently-learn-2-strange-things-about-python-boolean-values-8e44f1e5735e深入探究