[Python] Boolean operator (or / and) does not return Boolean value

There are operators ʻor, ʻand, and not in Python, but these are called Boolean operators.

Truth value in Python

Python can represent any object as a truth value (true / false value), and the following built-in functions are judged to be false.

--Constants defined as false: None and False --Zero in numeric type: 0, 0.0, 0j, Decimal (0), Fraction (0, 1) --Empty sequence or collection:'', (), [], {}, set (), range (0)

Also, if the object's class defines a __bool__ () method and it returns a False, or if it defines a__len__ ()method and returns 0, it will be judged false. ..

The built-in function bool () is used for judgment.

>>> bool(None)
False
>>> bool(False)
False
>>> bool(0)
False
>>> bool("")
False
>>> bool([])
False
#Everything that is not false is true
>>> bool("0")
True
>>> bool([False])
True

How to use or

ʻUsed when you want to determine whether either A or B is true`. Of course, it can be used outside of control syntax such as if statements.

>>> x = True
>>> y = False
>>> x or y
True
>>> bool(x or y)
True

By the way, ʻor and ʻand described later do not return a Boolean value, evaluate the value of the compared object from the left, and return it as it is when the return value is fixed.

In the above example (x or y)

--If x is true, y is not evaluated and x is returned (even if y is true, x is returned) --If not, return y

It works

>>> a = 0 #Define a value whose truth value is True
>>> b = 1 #Define a value whose truth value is False
>>> a or b #Since b is True, b is returned
1
>>> b or a #Even if it is reversed, b is True, so b is returned.
1

>>> b = "" #Reassign a value different from a whose truth value is False
a or b #b is returned because a is False
""

By the way, ʻor` behaves in the same way even if two or more are connected, and is evaluated from the left.

>>> a = 0
>>> b = 1
>>> c = 2
>>> a or b or c
1

It's easy to think of it as an operator that returns a truth value, but don't forget.

How to use and

ʻAnd can be used when you want to determine whether both ʻA and B are true.

As for ʻand`, the return value is the object to be evaluated, so a Boolean value is not returned.

>>> x = 0
>>> y = 1
>>> x and y
0

As with ʻor`, the object is returned when the result is confirmed.

--Return x if x is false --Otherwise it returns y

It works.

Note that we haven't strictly checked that everything we compare is true, as false objects will return the results as soon as they are found.

Two or more can be connected for ʻand`.

>>> a = 0
>>> b = 1
>>> c = 2
>>> a and b and c #Returns a because a is false
0
>>> d = 10
>>> d and b and c #When d is true, b is true and c is confirmed, the result is confirmed, so c is returned.
2

How to use not

not is an operator that represents negation, and takes only one value after it, such as not False, and negates that value.

Unlike the two operators described above, it returns a Boolean value.

>>> x = 0
>>> y = 1
>>> not x #x is a value that becomes False, but if not is added, it becomes True.
True
>>> not y #y is a value that is True, but if not is added, it becomes False.
False

not has a higher priority than ʻor and ʻand and is evaluated first.

>>> a = 0
>>> b = 1
>>> c = 2
>>> not a and b and c #False a is negated and becomes true, and since everything is true, the last evaluated object c is returned
c
>>> d = 10
>>> d and not b and c #Everything was true, but b is denied and false, so b is returned
False

Although it is basic, it is easy to forget about the return value, so I would like to use it consciously.

Recommended Posts

[Python] Boolean operator (or / and) does not return Boolean value
python Boolean operation does not return a Boolean value
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
Python bitwise operator and OR
Python or and and operator trap
[Python] return A [or / and] B
Python Boolean operation return value is not always bool type
[Python for Hikari-] Chapter 06-04 Functions (arguments and return value 3)
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
[Python for Hikari-] Chapter 06-03 Functions (arguments and return value 2)
Python version does not switch
Python, yield, return, and sometimes yield from
[Introduction to Udemy Python3 + Application] 49. Function citation and return value declaration
Python / Numpy> Link> Difference between numpy.random and random.random> thread-safe or not
[Python] Basic pattern and usage of if statement (comparison operator and Boolean operator)
Python does not output errors or output just because the indent is misaligned
Technical English> you use the boolean operators [and, or, and not] to ...> Boolean Operations — and, or, not
[Python] Use and and or when creating variables
[VScode] autopep8 format does not work [Python]
Connect a lot of Python or and and
Copy file and rewrite cell value @python
Virtualenv does not work on Python3.5 (Windows)
Python> Python does not include the last offset
Non-logical operator usage of or in python
Examples and solutions that the Python version specified in pyenv does not run
Opencv4.1 + Windows10 + Python 3.7.5 cv2.VideoCapture does not display correctly
File DL, byte value and delete in Python3
Jinja2 2.9.6 does not work on Lambda Python 3 series
python memo-"if not A and B" was "if (not A) and B"
[Python] Which should be used, return or return None
[Python] Expression (1,2) does not make tuples with parentheses
It would be wise to write like boolean and "A" or "B" [Python] [But]