Can you answer the output of the following code?
question_1.py
print('penguin' and 'PENGUIN')
If the answer is not `` `PENGUIN```, you may want to read this article.
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
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"!
-[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