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深掘り質問