Python bool Function with Examples

The bool function is among the built-in functions in Python that produces a Boolean result based on the parameters provided. It returns either the Boolean values True or False when a single argument is passed. The Python bool method transforms a given value into a Boolean (either True or False) by employing the conventional truth-testing methodology.

Python bool Function Syntax

It has the following syntax:

Example

bool([value])

Parameters

Passing a value to bool is not a requirement. If no value is provided, the function bool will return False by default.

Typically, the function bool accepts one argument.

Return

The bool function produces the following outcomes:

  • Yields False when the value is either absent or evaluates to false, and when the condition is not satisfied.
  • Yields True when the value is true and the condition is fulfilled.
  • Python bool Function Example 1

Let’s consider an example to illustrate the use of the bool function in Python.

Example

# Python program example for bool() Function

# Creating an empty list

test = []

# Printing the value of the list and its boolean value

print(test, 'is', bool(test))

# Creating a list with one item (0)

test = [0]

# Printing the value of the list and its boolean value

print(test, 'is', bool(test))

# Creating a float value of 0.0

test = 0.0

# Printing the value of the float and its boolean value

print(test, 'is', bool(test))

# Creating a None value

test = None

# Printing the value of None and its boolean value

print(test, 'is', bool(test))

# Creating a boolean value of True

test = True

# Printing the value of True and its boolean value

print(test, 'is', bool(test))

# Creating a string value

test = 'Easy string'

# Printing the value of the string and its boolean value

print(test, 'is', bool(test))

Output:

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

Let’s consider an additional example to illustrate the use of the bool function in conjunction with if-else statements in Python.

Example

num = 12

if num > 15:

    print(" number is greater than 15")

else:

    print(" number is less than or equal to 15")

Output:

Output

number is less than or equal to 15

Explanation:

In the code provided, whether the condition evaluates to true or false determines which corresponding block of code will be executed.

Let us examine the process of explicitly converting a value into a Boolean in the following example.

Using bool function to convert the condition to a boolean value

Example:

Example

num = 12

if bool(num > 15):

    print(" number is greater than 15")

else:

    print(" number is less than or equal to 15")

Output:

Output

number is less than or equal to 15

Python bool Function Example 3

  1. Utilizing the bool function to determine if a value evaluates to True or False

Example:

Example

val = " C# Tutorial "

if bool(val):

    print("True")

else:

    print("False")
  1. To Determine the Boolean Value of an Empty String

Example:

Example

emp_str = ""

if bool(emp_str):

    print("True")

else:

    print("False")
  1. To Determine if a value of zero is evaluated as True or False

Example:

Example

zero_val = 0

if bool(zero_val):

    print("True")

else:

    print("False")

Output:

Conclusion:

In summary, the bool function serves as a valuable tool in Python for handling boolean values. It can effectively convert any given value directly into a boolean representation, and it can also be employed to determine whether a value evaluates to true or false.

Input Required

This code uses input(). Please provide values below: