Python built-in bool functions

Source: Internet
Author: User
This article introduces the Python built-in bool function in detail.

English document:

Classbool([X])

Return a Boolean value, I. e. oneTrueOrFalse.XIs converted using the standard truth testing procedure. IfXIs false or omitted, this returnsFalse; Otherwise it returnsTrue.boolClass is a subclassint(See Numeric Types-int, float, complex). It cannot be subclassed further. Its only instances areFalseAndTrue(See Boolean Values ).

Note:

1. Boolean values that return True or False

2. if the parameter is set to the default value, False is returned.

>>> Bool () # unspecified parameter False

3. Use a standard logical test expression for parameter conversion

3.1 When a Boolean type is passed in, the return value is based on the original value.

>>> bool(True)True>>> bool(False)False

3.2 if a null string is input, False is returned; otherwise, True is returned.

>>> bool('')False>>> bool('0')True

3.3 when a value is input, False is returned for the value 0; otherwise, True is returned.

>>> bool(0)False>>> bool(1)True>>> bool(-1.0)True

3.4 if the number of elements is null when objects such as tuples, lists, and dictionaries are input, False is returned; otherwise, True is returned.

>>> Bool () # empty tuples False >>> bool (0,) # Non-empty tuples True >>> bool ([]) # Empty list False >>> bool ([0]) # Non-empty list True >>> bool ({}) # empty dictionary False >>> bool ({'K ': 'V'}) # Non-empty dictionary True

The above is a detailed description of the built-in Python bool function. For more information, see other related articles in the first PHP community!

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.