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

I am currently learning Python3, but I don't understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that it is supposed to return "True" or "False" and not one of the input numbers. Second, I don't see why the output (one of the input numbers) is so. Please help, thanks.

See Question&Answers more detail:os

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

1 Answer

The operator and returns the last element if no element is False (or an equivalent value, such as 0).

For example,

>>> 1 and 4
4 # Given that 4 is the last element
>>> False and 4
False # Given that there is a False element
>>> 1 and 2 and 3
3 # 3 is the last element and there are no False elements
>>> 0 and 4
False # Given that 0 is interpreted as a False element

The operator or returns the first element that is not False. If there are no such value, it returns False.

For example,

>>> 1 or 2
1 # Given that 1 is the first element that is not False
>>> 0 or 2
2 # Given that 2 is the first element not False/0
>>> 0 or False or None or 10
10 # 0 and None are also treated as False
>>> 0 or False
False # When all elements are False or equivalent

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