Python1: boolean type, python1 Boolean Type

Source: Internet
Author: User

Python1: boolean type, python1 Boolean Type

Boolean users determine conditions in conditions or loops. Here we will introduce the Boolean Type in python.

True and False

The python boolean type corresponds to two boolean values: True and False, corresponding to 1 and 0 respectively. See the following example.
>>> True = 1
True
>>> False = 0
True
>>> True + False + 21
22

Boolean expression value

When the following value is used as a Boolean expression, it is regarded as False by the interpreter:
1. None;
2. False;
3. Any numeric type of 0, such as 0, 0.0, and 0j;
4. Any empty sequence, such as '', (), [];
5. Any empty dictionary, such :{};
6. for user-defined class instances, if the class defines the _ bool _ () or _ len _ () method, and this method returns 0 or Boolean value False.
All other values are treated as True by the interpreter.
You can use bool to convert other values to boolean values:
>>> Bool ('')
False
>>> Bool ('this is a test ')
True
>>> Bool (42)
True
>>> Bool (0)
False

Boolean operation

The following is a Boolean operation in python:
1. x or y: if x is false, then y, else x
2. x and y: if x is false, then x, else y
3. not x: if x is false, then True, else False

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.