Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

integer - Python numbers in Boolean contexts

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

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...