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 put`True or False and False`

The result of`False`

, 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.cn`True or False and False`

, the result is`True`

。

But the answer is`False`

。

How do I figure it out?

`True or False`

Be equal to`True`

`True and False`

Be equal to`False`

Boolean only`True`

and`False`

Two, Boolean operators only`And and`

, `Or or`

, `Non not`

Three. 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.

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 put`and`

、 `or`

It 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 False`

The calculation sequence of should be:

- No brackets, no
`not`

, first`and`

`False and False`

Be equal to`False`

, now the statement is`True or False`

- Finally, or,
`True or False`

Be equal to`True`

Find another topic`True and False or not True`

Self test:

- No brackets, first
`not`

，`not True`

Be equal to`False`

, now the statement is`True and False or False`

- Next calculate
`and`

，`True and False`

Be equal to`False`

, now the statement is`False or False`

- Final calculation
`or`

，`False or False`

The result is`False`

Now it’s really strange how I took it for granted`and`

and`or`

Think 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!