# Python bool() Function with Example

The **bool() function** in Python is a built-in function that we use to convert a given value to a boolean value. This function returns a boolean value of either True or False depending on the input value.

The general syntax of the bool() function in Python is as follows:

bool(x)

In the above syntax, the parameter “x” is the value that needs to be converted to a boolean value. However, it is optional.

## Return Value of bool() Function

Python bool() function takes a specified argument and returns its boolean value either True or False. The return value depends on the passed argument value. The bool() function returns:

- False – if an argument passed to the function is empty, False, 0, 0.0, (), [ ], {}, or None
- True – if an argument passed to the function is any number (besides 0), True or a string.

## Python bool() Function Example

Let’s take some important example programs based on the bool() function.

**Example 1:** Converting an integer to boolean value

# Python program to convert an integer into boolean. x = 10 result = bool(x) print("Boolean value of 10: ", result) y = -10 result = bool(y) print("Boolean value of -10: ", result)

Output: Boolean value of 10: True Boolean value of -10: True

In this example, we have assigned integer values 10, and -10 to the variables x, and y, respectively. Then, we have used the bool() method and passed the values of x and y to this function. The function returned True value for both values and stored them into a variable result.

[adinserter block=”5″]

**Example 2:** Converting an empty list, tuple, set to boolean value

# Python program to convert an empty list, tuple, set into boolean value. empty_list = [] result = bool(empty_list) print("Boolean value of an empty list: ", result) empty_tuple = () result = bool(empty_tuple) print("Boolean value of an empty tuple: ", result) empty_set = {} result = bool(empty_set) print("Boolean of an empty_set: ", result)

Output: Boolean value of an empty list: False Boolean value of an empty tuple: False Boolean of an empty_set: False

We can also use the bool() function in the if statement to check if a value is True or False. Let’s see an example of this.

**Example 3:** Usage bool() function in if statement

# Python program to use bool() function in if statement. num = 10 if bool(num): print("The value of num is True") else: print("The value of num is False")

Output: The value of num is True

**Example 4:** Using bool() function with a string

# Python program to convert a string into boolean value. msg = "Hello, World!" result = bool(msg) print("Boolean of msg: ", result)

Output: Boolean of msg: True

**Example 5:** Using bool() function with a dictionary

# Python program to convert a dictionary into boolean value. dic = {"name": "John", "age": 30} result = bool(dic) print("Boolean of a dic: ", result)

Output: Boolean of a dic: True

We can also use the bool() function with a function in Python. If the function returns a value, the bool() function returns True, otherwise it returns False.

**Example 7:** Using bool() function with a function

def my_function(): return "Scientech Easy" # Calling function. returned_value = my_function() print(bool(returned_value))

Output: True

In this example, we have defined a function my_function that returns a string. Then, we have called the function. Since the function returns a string value, we have stored it into a variable returned_value.

When we have passed the returned value of the variable to the bool() function, it returned True value because my_function returns a value. If the function does not return anything (i.e., it had a return statement with no value), the bool() function would return False value.

These are just a few example programs of how we can use the bool() function in Python programming.

In this tutorial, you have learned about Python bool() function with various examples. Hope that you will have understood the basic concept of bool() function and practiced all example programs.

Thanks for reading!!!