Python built-in type (2) -- Boolean operation, python Boolean

Source: Internet
Author: User

Python built-in type (2) -- Boolean operation, python Boolean

 

In python, the bool operators are sorted by priority.or,and,not, Whereor,andShort-circuit Operator

notTest the true value of the expression before obtaining the inverse value.

notThe operator has only one expression,notTest the true value of the expression and then obtain the reverse value. The returned result is notTrueYesFalse

>>> expression1 = ''>>> expression2 = '1'>>> not expression1True>>> not expression2False
or, andThe result returned by the operator is the result of one of the expressions that meet the logical conditions in the expressions on both sides of the operator.

In other languages, for example, C # And bool operations, the result must be bool values. But in python, this is not the case. It returns the value of one of the expressions that meet the bool operation conditions.

  • x or y:

IfxIsTrue, The result isxIfxIsFalse, The result isy.

>>> expression1 = '1'>>> expression2 = '2'>>> expression1 or expression2'1'>>> expression2 or expression1'2'
  • x and y:

IfxIsFalse, The result isxIfxIsTrue, The result isy.

>>> expression1 = ''>>> expression2 = {}>>> expression1 and expression2''>>> expression2 and expression1{}
or, andShort-circuit Operator

Short-circuit operators mean that the expressions left and right of the operators are evaluated only when a value is required. For examplex or y, Python evaluate from left to right, first expressionxTrue Value test, if the expressionxIs the true value, accordingorOperator features, regardlessyWhat is the bool result of an expression? The result of an operator is an expression.x, So the expressionyDoes not evaluate. This behavior is calledShort Circuit.

# The function checks whether a number is an even number def is_even (num): print ('input num is: ', num) return num % 2 = 0 # is_even (1) is short-circuited, not executed >>> is_even (2) or is_even (1) input num is: 2 True >>> is_even (1) or is_even (2) input num is: 1 input num is: 2 True
or, andYou can use multiple operators in combination. When using this operator, perform a short circuit from left to right and finally enter the result.

Expressionx or y and z, Will firstx or yEvaluate the value, and then sum the resultzWhile still following the short circuit principle.

# Is_even (2) and is_even (4) are all short-circuited >>> is_even (1) and is_even (2) and is_even (4) this num is: 1 False # is_even (1) is False, is_even (3) is short-circuited # is_even (1) and is_even (3) is False, is_even (5) required value # is_even (1) and is_even (3) or is_even (5) is False, is_even (7) is short-circuited >>> is_even (1) and is_even (3) or is_even (5) and is_even (7) this num is: 1 this num is: 5 False
notOperator Priority Ratio or, andWhen used together not, And then calculate or, andValue
>>> is_even(1) or is_even(3)this num is : 1this num is : 3False>>> not is_even(1) or is_even(3)this num is : 1True>>> is_even(1) or not is_even(3)this num is : 1this num is : 3True>>>
notOperator Priority Ratio ==, !=Low, not a == bWill be interpreted not (a == b), a == not bA syntax error is prompted.
>>> not 1 == 1False>>> 1 == not 1SyntaxError: invalid syntax>>> not 1 != 1True>>> 1 != not 1SyntaxError: invalid syntax

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.