Python built-in Method - bool()

The bool() method is a built-in function in Python that returns a Boolean value (True or False) based on the truthiness of the input argument. The truthiness of an object in Python is determined by its inherent boolean value, which is False for certain objects (such as None, False, 0, and empty containers like [], {}, and ''), and True for all other objects.

Here is the syntax for bool() method:

bool(x)
Source:w‮gi.ww‬iftidea.com

where x is any object that needs to be converted to a boolean value.

Here are some examples of how to use bool():

print(bool(True))     # Output: True
print(bool(1))        # Output: True
print(bool([]))       # Output: False
print(bool('hello'))  # Output: True
print(bool(None))     # Output: False

In the first example, the boolean value True is already passed as an argument to the bool() method, so it simply returns True. In the second example, the integer value 1 is truthy, so the bool() method returns True. In the third example, the empty list [] is falsy, so the bool() method returns False. In the fourth example, the non-empty string 'hello' is truthy, so the bool() method returns True. In the fifth example, the None object is falsy, so the bool() method returns False.

The bool() method can be useful when you need to determine the truthiness of an object, for example when validating user input or checking the status of a variable.