Python logical operator __python

Source: Internet
Author: User

When I first looked at Python, I thought the logic operator was different from other languages, so I recorded it.
There are three logical operations of Python: and, or, not. to correspond with, or, or not respectively.

Strictly speaking, the operand of a logical operator should be a Boolean expression. But Python has a more flexible approach to this.
Even if operands are numbers, the interpreter treats them as "expressions".
A Boolean value of 1,0 for a number other than 0 is a Boolean value of 0.
In Python, the empty string is false and the Non-empty string is true. The Non-zero number is true.

For and operator A and B:
As long as the expression on the left is true, the value returned by the entire expression is the value of the right expression, otherwise, the value of the left expression is returned
For or operator A or B:
As long as the expression on the left is true, the value returned by the entire expression is the value of the left expression, otherwise, the value of the right expression is returned
For not operator not a:
Returns False if A is True. If A is False, it returns TRUE.
Example:
Suppose the variable A is ten, and B is 20.
(A and B) return 20.
(A or B) returns 10.
Not (A and B) returns False

 #coding: utf-8 test1 = test2 = 0 test3 = ' ' test4 = ' i ' Print test1 and Test3 #resu lt = ' Print test3 and test1 #result = ' Print test1 and test4 #result = ' The ' a ' print test4 and test1 #result = the print test1 or test2 #result = test1 or test3 #result = print Test3 or test4 #result = "" "Print test2 or test4 #result =" The "the" "a" test1 or test4 #result = print test4 or test1 #result = "Fir St "Print test2 or test3 #result = ' Print test3 or test2 #result = 0 

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.