Get the App
SLTechnology News&Howtos  ›  Development  › 

Priority of Python logical operations

Shulou Source: shulou.com Published: 2022-06-03 12:37:45 10月01日 Update

This article mainly explains "the priority of Python logic operation". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the priority of Python logic operation".

1. And 'and'

If the left side of the and is False, the left result (False) is returned directly; if the left side of the and is True, the returned result depends on the value on the right. You need true on both sides to return true;. The right side is false, so false is returned as a whole.

2. Or'or'

If the left side of the or is True, the left result (True) is returned directly; if the left side of the or is False, the returned result depends on the value on the right. As long as one side is true, true; will be returned. Although the right side is false, the overall true will be returned.

3. Not 'not'

Not is easy to understand, which is a reverse operation. You need true on both sides to return the current reverse of true;. Currently, false is returned for true, and true is currently returned for false.

For example: not True---- > False;not False--- > True

4. Examples:

# and or not

# priority, () > not > and > or

# print (2 > 1 and 1

< 4) # print(2 >

1 and 1

< 4 or 2 < 3 and 9 >

6 or 2

< 4 and 3 < 2) # T or T or F #T or F # print(3>

4 or 42) # T

# print (2 > 1 and 3

< 4 or 4 >

5 and 2

< 1) # T # print(1 >

2 and 3

< 4 or 4 >

5 and 2 > 1 or 9

< 8) # F # print(1 >

1 and 3

< 4 or 4 >

5 and 2 > 1 and 9 > 8 or 7

< 6) # F # print(not 2 >

1 and 3

< 4 or 4 >

5 and 2 > 1 and 9 > 8 or 7

< 6) # F #ps int ---->

Bool non-zero conversion to bool True 0 conversion to bool is False

# print (bool (2))

# print (bool (- 2))

# print (bool (0)

# # bool-- > int

# print (int (True)) # 1

# print (int (False)) # 0

"''x or y x True, then return xdistinct'"

# print (1 or 2) # 1

# print (3 or 2) # 3

# print (0 or 2) # 2

# print (0 or 100) # 100

# print (2 or 100 or 3 or 4) # 2

# print (0 or 4 and 3 or 2)

'x and y x True, then return yawness.'

# print (1 and 2)

# print (0 and 2)

Print (2 or 1

< 3) print(3 >

1 or 2 and 2)

5. Priority of logical operation

The priority of the operator is not > and > or. This priority knowledge point will be applied to the composite operation in the same way as addition, subtraction, multiplication and division. And has a high priority in or and and. If the priority of or is greater than and, the priority of not in false,not and and should be high; if the priority of and is higher than not, true should be returned.

Thank you for reading, the above is the content of "the priority of Python logic operation". After the study of this article, I believe you have a deeper understanding of the priority of Python logic operation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Priority operation logic result learning content depends on right right that is numerical value whole knowledge knowledge points addition subtraction multiplication and division side examples ideas situations articles. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Microsoft OPPO Reno Apple