Python bool() Function

The bool() function is one of the built in methods in python which outputs the Boolean value depending on the input parameters. The Boolean values True or False are returned from the single argument. The python bool() method converts value to boolean (True or False) using the standard truth testing procedure.

Syntax

Parameters

It is not mandatory to pass value to bool(). If you do not pass the value, bool() returns False.

In general , bool() takes a single parameter value.

Return

The bool() returns:

  • Returns False if the value is omitted or false and the condition is not met.
  • Returns True if the value is true and condition is met.

Python bool() Function Example 1

Code

Output:

[] is False
[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True

Python bool() Function Example 2

Using bool() function with if-else statements.

Code

Output:

number is less than or equal to 15

In the above code, If the condition is true or false, then the respective block is executed. Now, lets see the explicit conversion of value into Boolean value in the below example.

# Using bool() function to convert the condition to a boolean value

Code

Output:

number is less than or equal to 15

Python bool() Function Example 3

1. Using bool() function to check if a value is True or False

Code

Output:

True

2. To Check if an empty string is True or False

Code

Output:

False

3. To Check if a zero value is True or False

Code

Output:

False

Conclusion:

All in all, the bool() function is a useful asset in Python for working with boolean values. It very well may be utilized to switch any value over completely to a boolean value expressly, and it can likewise be utilized to check in the event that a value is true or false.






Latest Courses