bool is the abbreviation of Boolean, which has only two values: true (True) and false (False).
The bool function has only one parameter, and returns true or false based on the value of this parameter. Fake.
1. When using the bool function on numbers, 0 returns False, and any other value returns True.
>>> bool(0) False >>> bool(1) True >>> bool(-1) True >>> bool(21334) True
2. When using the bool function on a string, it returns False for a string with no value (that is, None or an empty string), otherwise it returns True.
>>> bool('') False >>> bool(None) False >>> bool('asd') True >>> bool('hello') True
3. The bool function returns False for empty lists, dictionaries and ancestors, otherwise it returns True.
>>> a = [] >>> bool(a) False >>> a.append(1) >>> bool(a) True
4. Use the bool function to determine whether a value has been set.
>>> x = raw_input('Please enter a number :') Please enter a number : >>> bool(x.strip()) False >>> x = raw_input('Please enter a number :') Please enter a number :4 >>> bool(x.strip()) True
The above is the detailed content of What does bool mean in python. For more information, please follow other related articles on the PHP Chinese website!