Python built-in functions (6) -- bool, python built-in function bool
English document:
-
Class
bool([
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