Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In Python 3.x, all non-zero numbers are mapped to "True" in Boolean contexts; however, in an equality statement, 1 behaves different from all the rest (see below). What's up with that? Why is 1 special among non-zero numbers?

>>> bool(1)
True
>>> bool(2)
True
>>> 1 and True
True
>>> 2 and True
True
>>> 1 == True
True
>>> 2 == True
False

Note that this is not explained by the difference between '==' and 'is':

>>> one = 1
>>> one is True
False
>>> two = 2
>>> two is True
False
question from:https://stackoverflow.com/questions/65888057/python-numbers-in-boolean-contexts

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
900 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...