Detailed description of and and or actual usage in Python

Source: Internet
Author: User
special properties of and and or

In Python, and and OR, the Boolean logic calculus is executed, but they do not return a Boolean value; instead, they return one of the values that they actually compare. Let's look at the example below.

>>> ' A ' and ' B ' B ' >>> ' and        ' B ' '         >>> ' a ' and ' B ' and ' C '

The value of a left-to-right calculus expression in a Boolean context when using and. 0, ', [], (), {}, None are false in a Boolean context, and everything else is true. Fortunately, almost everything. By default, the class instance in the Boolean context is true, but you can define a specific method in the class to make the calculation of the class instance false. You will learn about classes and these special methods in the 5th chapter. If all values in the Boolean context are true, then and returns the last value. In this example, the value of the and calculation ' a ' is true, and then the ' B ' calculation value is true, eventually returning ' B '.
If a value in the Boolean context is false, and returns the first false value. In this example, "is the first false value."
All values are true, so and returns the last truth, ' C '.

>>> ' A ' or ' B '         a ' >>> ' or ' B ' B '          >>> ' or [] or {}     {}>>> def sidefx ():. ..     Print "in Sidefx ()"     ... Return 1>>> ' a ' or Sidefx ()    ' a '

When using or, the left-to-right calculus is evaluated in a Boolean context, just like and. If a value is true, or returns the value immediately. In this case, ' a ' is the first truth value.
The value of the or calculation ' is false, and then the value of ' B ' is true, so it returns ' B '.
If all values are false, or returns the last False value. The value of the or calculation "is false, then the value of the calculation [] is false, the value of the calculation {} is False, and finally the {} is returned.
Note that or in a Boolean context, the expression calculus continues until the first true value is found, and then the remaining comparison values are ignored. This feature is important if some values have side effects. Here, the function Sidefx is never called, because the value of the or calculation ' a ' is true, so it immediately returns ' a '.

If you are a C-language hacker, you must be familiar with bool? A:B expression, if bool is true, the expression calculus value is a, otherwise B. You can do the same thing based on how and and or in Python.

Cases. Introduction to And-or Skills

>>> a = "First" >>> B = "Second" >>> 1 and A or B ' first ' >>> 0 and A or B ' second '

Does this syntax look similar to bool in C? An A:B expression. The entire expression is left-to-right, so the calculation of the and expression is performed first. The 1 and ' first ' calculation is ' first ', then ' first ' or ' second ' is ' first ' calculation value.
0 and ' first ' calculation value is False, then 0 or ' second ' calculus value is ' second '.

However, since this Python expression simply carries out Boolean logic, it is not a specific composition of the language, which is the and-or technique and the bool in C language? A:B syntax is a very important difference. If A is false, the expression will not work as you would expect. (Can you tell if I've been tossed about by this problem?) More than once? )

Example. And-or Skill Invalid Occasion

>>> a = "" >>>b = "Second" >>> 1 and A or B        ' second '

Since a is an empty string, the hollow string in the Boolean context of Python is considered false, and the calculation value of 1 and ' ' is ', the last ' or ' second ' calculation value is ' second '. Oh! This value is not what you want.

And-or techniques, bool and A or B expressions, when a value in a Boolean context is false, does not resemble a C-language expression bool? A:B work like that.

The real trick behind the and-or technique is to make sure that the value of a is never false. The most common way is to make a [a], b become [b], and then use the first element of the return value list, which should be one of a or B.

Example: safe use of and-or tips

>>> a = "" >>> B = "Second" >>> (1 and [a] or [b]) [0] "

Because [a] is a non-empty list, it is never false. Even if a is 0 or ' or other false values, the list [a] is also true because it has an element.

So far, the trick may seem to outweigh the value of the problem. After all, the same thing can be done with an if statement, so why go through these troubles? Well, in many cases you have to choose between two constant values, because you know the value of a is always true, so you can use this simpler syntax without worrying about it. For the use of more complex forms of security, there are still good reasons for doing so. For example, in some cases of the Python language, an If statement is not allowed, such as in a lambda function.

Note : This is because the statement is interpreted from left to right. We can understand that.
and statements:
0and* no need to reconsider * is 0 or 1, the result is 0
1and* need to consider whether * is 0 or one to determine the result.

1or* does not need to consider the following *, the result is 1
0or* need to consider the following * to determine the outcome

"Recommended"

1. Python and, or, and and-or syntax summary

2. Parsing and and or usage in Python

3. Sharing the example of and/or in Python tutorial

4. Summarize Python's logical operators and

5. Python: Logical judgment and Operator instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.