• notice
  • Congratulations on the launch of the Sought Tech site

And and or in Python, with unexpected results

I. Introduction

A few days ago, a fan named [Chloe] asked a question about the basics of Python in the Python strongest king exchange group. About and and or, I will share it with you and learn it together.

image.png

Second, the solution process

Here [Jie] gave the answer. In fact, in Python, except for parentheses, and has a higher priority, then [Mr. Yu Liang] also gave the answer here. There are indeed no parentheses, and expressions operate from left to right, and The priority is higher than or, and the and is calculated first, and then the or.

image.png

First calculate the output corresponding to and is False, and then calculate with or together to get the output to be True.

Don't go, this topic [Mr. Yu Liang] still has expansion and new knowledge!

There is another interesting use of and and or:
print(1 and 'string')
# output: string
# Principle: The value of x and y can only be x or y. If x is true it is y, if x is false it is x
print(0 or 'string')
# output: string
# Principle: The value of x or y can only be x or y. If x is true, it is x, and if x is false, it is y.

image.png

3. Summary

Hello everyone, I'm Pippi. This article mainly shares the priority problem of and and or in the basics of Python, and gives specific analysis and code demonstrations for this problem, helping fans to solve the problem smoothly, and also doing some additional knowledge expansion and learning a lot of things .

image.png

Finally, thanks to the fans [Chloe] for asking questions, thanks to [Luna], [Jie], and [Mr. Yuliang] for the specific analysis and code demonstrations, thanks to [Ms. Yuliang] for sharing more Python knowledge, and thanks to [dcpeng] , [PI], [冫 Ma Yucheng] and others participated in the study and exchange.

Little friends, hurry up and practice it! If you encounter any problems during the learning process, please add me as a friend, and I will pull you into the Python learning exchange group to discuss and learn together.


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+