The priority of Python Boolean operators

Time:2020-2-9

Two days ago, I experienced the challenge of answering questions on xue.cn. I made a mistake in one question, but I didn’t understand what was wrong. The title is as follows. What will be printed after the code runs?

if True or False and False:
    print('1')
else:
    print('2')

This topic examines two simple knowledge points, one is Boolean operation, the other is flow control statement. It turned out to be 1, and I chose 2.

Retell my solution. First, the Boolean part, I putTrue or False and FalseThe result ofFalse, the code is simplified as follows:

if False:
    print('1')
else:
    print('2')

I was confused for a short time. Later, I came back to my senses and organized a note “testing Python process control basic skills with Aunt Liu, 35”, which was used to consolidate the basic skills.

If branch control statement of process control will only execute the branch with true condition, so the result of the above code segment is 2. Then I found out that I was wrong in the Boolean part. Input and run directly on xue.cnTrue or False and False, the result isTrue

true

But the answer isFalse
How do I figure it out?

  • True or FalseBe equal toTrue
  • True and FalseBe equal toFalse

Boolean onlyTrueandFalseTwo, Boolean operators onlyAnd and, Or or, Non notThree. I thought I knew all about it. How could I still do something wrong? What’s wrong?

The simplest operation, self-test, I have mastered, no mistakes.
true1

It’s strange. What’s wrong? If you don’t want to understand, open the two chapters of “self study is a craft” entry “and” value and corresponding operation “, and read the Boolean operation operator part repeatedly, then you can find the key is:

The lowest priority is or, followed by and, and the highest priority is not

And the above mental calculation, I putandorIt is regarded as equal priority and calculated from left to right. ——Be careful: this understanding is wrong! These two have different priorities!

True or False and FalseThe calculation sequence of should be:

  • No brackets, nonot, firstand
  • False and FalseBe equal toFalse, now the statement isTrue or False
  • Finally, or,True or FalseBe equal toTrue

Find another topicTrue and False or not TrueSelf test:

  • No brackets, firstnotnot TrueBe equal toFalse, now the statement isTrue and False or False
  • Next calculateandTrue and FalseBe equal toFalse, now the statement isFalse or False
  • Final calculationorFalse or FalseThe result isFalse

Now it’s really strange how I took it for grantedandandorThink of it as equal priority! ——Fortunately, there is still a chance to find out the mistakes and omissions of your knowledge points by brushing the questions!

Recommended Today

Java Persistence with MyBatis 3 reading notes

Chapter 1 Introduction to mybatis The following contents are the important points that I think when I read this book. On the one hand, I’d like to make a note for myself and help other colleagues brief introduction mybatis: it is an open source framework that simplifies and implements the Java data persistence layer. It […]