This article introduces the Python built-in bool function in detail.
English document:
Classbool
([X])
Return a Boolean value, I. e. oneTrue
OrFalse
.XIs converted using the standard truth testing procedure. IfXIs false or omitted, this returnsFalse
; Otherwise it returnsTrue
.bool
Class is a subclassint
(See Numeric Types-int, float, complex). It cannot be subclassed further. Its only instances areFalse
AndTrue
(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!