python Boolean operation does not return a Boolean value

Introduction

Can you answer the output of the following code?

question_1.py


print('penguin' and 'PENGUIN')

image.png

If the answer is not `` `PENGUIN```, you may want to read this article.

Evaluation of objects in Python

First, let's briefly review Python objects. In Python all objects can be evaluated as True or False. In particular, the objects that evaluate to False are narrowed down to the following (cited from references).

The above four are often used, so it is convenient to remember them. Therefore, in Python, it is possible to directly assign an object to a conditional expression and evaluate it as follows.

eval_obj.py


penguin_list = ['Emperor', 'Humboldt', 'Adelie']
if (penguin_list):
    print("Penguins festival")
else:
    print("Penguins are extinct")
#Execution result:Penguins festival

or Or is called OR, and it is common to return true if x or y is true and false otherwise when `` `x or y```. As we saw earlier, in Python objects are evaluated as True or False. Therefore, the following evaluation is possible.

or_peugin.py


print('penguin' or None)

Well, normally the above would return True, but Python doesn't. The result is as follows.

Output result


penguin

In fact, in Python x or y returns x if the evaluation of x is True, otherwise y. In short, __ Thinks like a normal OR operation and returns the object when the value is fixed. __ Here are some samples.

or_sample.py


print('Emperor' or 'Humboldt')
#Output result: Emperor
print('Humboldt' or 'Emperor')
#Output result: Humboldt
print('penguin' or 0)
#Output result: penguin
print(False or 'penguin')
#Output result: penguin
print(0 or False)
#Output result: False
print(False or 0)
#Output result: 0

and And is called AND, and it is common to return true if both x and y are true when `` `x and y```, and false otherwise. However, in Python, the return value is not a bool value. In Python, x and y returns x if x evaluates to False, y otherwise. In short, __ Thinks like a normal AND operation and returns the object when the value is fixed. __ Here are some samples.

and_sample.py


print('Emperor' and 'Humboldt')
#Output result: Humboldt
print('Humboldt' and 'Emperor')
#Output result: Emperor
print('penguin' and 0)
#Output result: 0
print(False and 'penguin')
#Output result: False
print(0 and False)
#Output result: 0
print(False and 0)
#Output result: False

Finally

Due to the recent machine learning boom and python's simple grammar, I think that there are many people who are dealing with python somehow (including myself). However, it is interesting to study properly, and somehow it may cause unexpected bugs and unexpected behavior. Until yesterday, the author was also "a logical operation is decided to return a bool value (nose hoji)", so it may be that he created a ridiculous creature in the near future and released it to the world. As mentioned in the references, [Introduction to Python Practice] published this year (https://www.amazon.co.jp/Python%E5%AE%9F%E8%B7%B5%E5%85%A5 % E9% 96% 80-% E8% A8% 80% E8% AA% 9E% E3% 81% AE% E5% 8A% 9B% E3% 82% 92% E5% BC% 95% E3% 81% 8D% E5% 87% BA% E3% 81% 97% E3% 80% 81% E9% 96% 8B% E7% 99% BA% E5% 8A% B9% E7% 8E% 87% E3% 82% 92% E9% AB% 98% E3% 82% 81% E3% 82% 8B-WEB-PRESS-plus% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA / dp / 429711111X / ref = sr_1_1? dchild = 1 & hvadid = 386600561258 & hvdev = c & jp-ad-ap = 0 & keywords = python% E5% AE% 9F% E8% B7% B5% E5% 85% A5% E9% 96% 80 & qid = 1594634033 & sr = 8-1 & tag = yahhyd-22) is a great book for learning Python. Let's take this opportunity to try "Python somehow"!

References

-[Introduction to Python Practice](https://www.amazon.co.jp/Python%E5%AE%9F%E8%B7%B5%E5%85%A5%E9%96%80-%E8%A8% 80% E8% AA% 9E% E3% 81% AE% E5% 8A% 9B% E3% 82% 92% E5% BC% 95% E3% 81% 8D% E5% 87% BA% E3% 81% 97% E3% 80% 81% E9% 96% 8B% E7% 99% BA% E5% 8A% B9% E7% 8E% 87% E3% 82% 92% E9% AB% 98% E3% 82% 81% E3% 82% 8B-WEB-PRESS-plus% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA / dp / 429711111X / ref = sr_1_1? dchild = 1 & hvadid = 386600561258 & hvdev = c & jp- ad-ap = 0 & keywords = python% E5% AE% 9F% E8% B7% B5% E5% 85% A5% E9% 96% 80 & qid = 1594634033 & sr = 8-1 & tag = yahhyd-22)

Recommended Posts

python Boolean operation does not return a Boolean value
[Python] Boolean operator (or / and) does not return Boolean value
Python Boolean operation return value is not always bool type
Python list is not a list
Python version does not switch
[Python] return A [or / and] B
When incrementing the value of a key that does not exist
A special Python codec that seems to know but does not know
[VScode] autopep8 format does not work [Python]
Virtualenv does not work on Python3.5 (Windows)
[Python] [LINE Bot] Create a parrot return LINE Bot
Python> Python does not include the last offset
EOFError: EOF when reading a line does not have a standard input value set
Opencv4.1 + Windows10 + Python 3.7.5 cv2.VideoCapture does not display correctly
Python Note: When assigning a value to a string
Why does Python have to write a colon?
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] Expression (1,2) does not make tuples with parentheses
Python early return
[python] vector operation
Python OS operation
[Python] Matrix operation
[Python for Hikari-] Chapter 06-04 Functions (arguments and return value 3)
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Key input that does not wait for key input in Python
Why not create a stylish table easily with Python?
python> check NoneType or not> if a == None:> if a is None:
C | Python> Negative value modulo (%) operation> C: Returns minus | Python: Returns plus
[Python for Hikari-] Chapter 06-03 Functions (arguments and return value 2)
What does the last () in a function mean in Python?
When creating a pipenv environment, I got addicted to "Value Error: Not a valid python path"
A story that pyenv is stuck because the python execution command PATH does not pass