Why does python 0 or 1 return 1? Principle of or operation

Why does the python expression of 0 or 1 return 1?

First of all, you should first understand the usage of or. In the comparison operation of logical or, such as m or n, Python will first perform the bool operation bool(m) on m. If True is returned, the return value of m or n is m, if False is returned, the value of n is returned. Then it is clear that the return value of 0 or 1 returns 1.

In order to verify the operation method and principle of or, we will verify it through an example below, such as why the return value of [] or [] is the second empty list [] instead of the first empty list [].

or return value example code

>>> a = []
>>> b = []
>>> id(a)
2586266068736
>>> id(b)
2586266433216
>>> a or b
[]
>>> c = a or b
>>> id(c)
2586266433216

Original text:Why does python 0 or 1 return 1? Principle of or operation

Disclaimer: Content is for reference only.

Guess you like

Origin blog.csdn.net/weixin_47378963/article/details/134821684