Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of python Logic Operation and strange return value (not,and,or) problem

Shulou Source: shulou.com Published: 2022-06-01 06:13:55 09月17日 Update

Editor to share with you the python logic operation and strange return value (not,and,or) problem example analysis, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

First, the priority for and, or, and not is not > and > or.

The same priority is calculated from left to right.

Let's start with non-operations. Python's non-operations are nothing special compared to these languages. Not has only two return values, True and False. In Python, objects with false truth values, including False,None, the number 0, an empty string, and an empty container type. Any object other than that is true.

Then there is the and operation. The rules for the and (and) operation of Python are

If the expression on the left is true, the value of the expression on the right is returned

Otherwise, return the value of the expression on the left

Finally, the rules for Python OR (or) operations are

If the expression on the left is true, the value of the expression on the left is returned

Otherwise, return the value of the expression on the right

Feel it in code:

So now let's add priority-related hybrid operations:

# first of all, not has the highest priority, then not 8 returns False

# (False or 3 and 4 or 2 and 0 or 9 and 7)

# the next priority is and

# (False or 4 or 0 or)

# finally or returns a true value of 4

The above is all the contents of the article "example Analysis of python Logic Operations and Strange return value (not,and,or) problems". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Operations expressions priorities articles examples logic problems analysis next content right objects truth rules highest but not much two code only Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft vpn Shulou Tech Info Huawei