Beginner Python's Boolean type

Source: Internet
Author: User

and Operation : Only two Boolean values are true when the result is true.

True and True    # ==> truetrue and False   ==> Falsefalse and True   ==> falsefalse and false  ==> false

or Operation : evaluates to true as long as there is a Boolean value of true.

True or True    # ==> truetrue or False   ==> truefalse or True   ==>  TrueFalse or False  ==> false

Non-operation : Turns true to false, or false to true:

Not True   # ==> falsenot False  ==> Tru

Boolean operations are used in a computer to make conditional judgments, and the computer can automatically execute different follow-up code, depending on the result of the calculation as true or false.

In Python, Boolean types can also do and, or, and not with other data types, see the following code:

A ='a=t'a=f'

The result of the calculation is not a Boolean type, but the string ' a=t ', which is why?

Because Python regards 0 , 空字符串‘‘ and None is treated as False, other numeric values and non-empty strings are considered True, so:

True and ' a=t ' evaluates to ' a=t ' continue to calculate ' a=t ' or ' a=f ' results or ' a=t '

To explain the above results, it also involves an important law of the and and or operations: short-circuit calculation.

Beginner Python's Boolean type

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.