Python and, or, and and-or syntax summaries _python

Source: Internet
Author: User
Tags in python

I. and:

In Python, and and or perform Boolean logic calculations, as you would expect, but they do not return Boolean values; instead, they return one of the values that they actually compare.

Copy Code code as follows:

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

The value of an expression from left to right in a Boolean context, and returns the last value if all the values in the Boolean context are true.

If a value in the Boolean context is false, and the first false value is returned

Two, or:

Copy Code code as follows:

>>> ' A ' or ' B ' ' a ' >>> ' or ' B ' ' B ' >>> ' or [] or {}
{} >>> 0 or ' a ' or ' C ' ' a '
[Code]
When using or, the value is evaluated from left to right in the Boolean context, like and. If a value is true, or immediately returns the value

If all of the values are false, or the last false value is returned

Note that the or in a Boolean context will always perform an expression calculus until the first true value is found, and then the remaining comparison values are ignored

Third, And-or:

And-or combines the two preceding syntaxes, reasoning.

[Code]
>>> a= ' i ' >>> b= ' second ' >>> 1 and A or B ' I ' >>> (1 and a) or B ' a ' >& Gt;> 0 and A or B ' second ' >>> (0 and a) or B ' second ' >>>


This syntax looks similar to bool in C language. An A:B expression. The entire expression is performed from left to right, so the calculation of the and expression is performed first. The 1 and ' a ' is the value of ' a ', and then ' second ' is evaluated as ' a '.

The 0 and ' a ' calculation value is False, then the 0 or ' second ' calculus value is ' second '.

And-or is mainly used to imitate the three-mesh operator bool?a:b, that is, when the expression bool is true, take a or B.

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

Iv. Safe use of and-or

Copy Code code as follows:

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

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

A responsible programmer should encapsulate the and-or technique into a function:

Copy Code code as follows:

def choose (bool,a,b): Return (bool and [a] or [b]) [0] Print choose (1, ', ' second ') # "

Related Article

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.