Python's logical Operation __python

Source: Internet
Author: User

Python's logical operations (And,or,not) are not quite the same as those in C + +, Java, and other languages. The logical operations of these languages return values that are of bool value, while Python returns different.

In the first place, the operation of Python does not have a special location compared to these languages. Not only two return values, True and false. In Python, a true value is a false object, including False,none, number 0, an empty string, and an empty container type. Any other object is true.

Next is the operation, Python's and the rule of operation is if the left expression is true, return the value of the right expression otherwise, return the value of the left expression

It seems strange at first, what a logical operation is so complicated. There is certainly a purpose in doing so. The benefit of this rule is found when we need not the bool of an expression but the value of the expression, but only if we need to determine the next action based on the bool value of the expression.

For example, you need to count the weight of a peach weight of more than 500 grams, written in C, may be written like this

for (i = 0; i < n; ++i) {if (Weights[i] >) sumofweight + = Weights[i];

With Python, you can write this.

For x in weights:sumofweights = x > x

is obviously a lot simpler.

Finally, or operation, the rule of Python or operation is that if the expression on the left is true, it returns the value of the expression on the left, otherwise, returns the value of the expression on the right.

This rule and or operation of the expatiating, is to simplify the code, increase efficiency.

Some students may find the rules of and or or are more complex and not easy to remember. In fact, this is not a memory, because, whether it is an and or, the value of the result is the value of the expression that ultimately determines the truth of the entire expression .

For and operations

A and B if a is true, continuing to compute the B,B will determine the ultimate value of the entire expression, so the result of B is that if a is false and no B is needed, the value of the whole expression is false, so the result is a

For or operations

A or B if a is true, you do not need to compute B to know the true value of the entire expression, so the result is the values of a if a is false, continuing to compute B,B will determine the final value of the entire expression, so the result is the value of B

Finally, say an and or expression, and the usual and or or expressions are the following expressions

Condition and A or b

This expression and the only three-mesh operator in C

Condition? A:b

are similar, but slightly different. The semantics of the three-mesh expression in C are as follows

If Condition:a else:b

But Python and or or are different, because Python's logical expression of the rules of operation, it must be guaranteed that the true value of a, and C is the same as the three-mesh operator, and if a is false, regardless of the true value of condition, always choose b rather than a.

It can be said that Python's logical operation of the programmer to bring a great programming shortcut, but if you do not understand the reason for such a design, it can not freely use its convenience, and even caused errors without knowing.


Welcome reprint, Reprint please indicate author and source evercoding.net, do not use for commercial use

This article address: Http://blog.EverCoding.net/2012/06/20/logic-operation-in-python

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.