Python Boolean

 

In python, the Boolean will represent two values such as True or False and the Boolean types are useful to evaluate whether the defined expressions are True or False.

 

In python, the variables with values either True or False will treat as Boolean-type variables. While assigning the Boolean values to the variables, you need to make sure that the starting letters (T and F) of Boolean values (True, False) are capital. Otherwise, you will get an error because python is a case-sensitive programming language.

 

Following is the example of creating the variables with different Boolean types in python.

 

a = False
b = True
print(type(a)) #<class 'bool'>
print(type(b)) #<class 'bool'>

If you observe the above example, we created two variables (a, b) by assigning Boolean values in python. Here, we used the type() function to know the type of variables.

 

In python, if you try to compare the two variable values, the expressions will be evaluated and return the response as a Boolean value (True or False).

 

Following is the example of comparing the multiple variable values in python.

 

x = 20
y = 30
print(x > y)
print(x < y)
print(x == y)

If you observe the above example, we created two variables (a, b) and evaluating the variable values with multiple expressions.

 

When you execute the above python program, you will get the result as shown below.

 

False
True
False

In python, if you use expressions in conditional statements (if…else), it will return the Boolean values either True or False to perform further operations.

 

Following is the example of using expressions in python conditional statements (if…else).

 

x = 20
y = 30
if x > y:
    print("x is greater than y")
elif x < y:
    print("x is less than y")
else:
    print("x is equals to y")

If you observe the above example, we created two variables (a, b) and using different expressions in python conditional statements (if…else) to print the message based on the expression evaluated values, i.e., either True or False.

 

When you execute the above python program, you will get the result as shown below.

 

x is less than y 

Python bool() Function

In python, the bool() function is useful to evaluate any value or variable and return Boolean values, i.e., either True or False.

 

print(bool("Tutlane"))
print(bool('''Welcome'''))
print(bool(24))
print(bool([1, 2, 4]))
print(bool(-100))
print(bool(44.56))

When you execute the above python program, you will get the result as shown below.

 

True
True
True
True
True
True

If you observe the above result, the python bool() function has returned True for all the values. The python bool() function will always return True for any value except 0, None, FalseAnd empty values such as "", (), {}, [].

 

Following is the example of using the bool() function to return False value in python.

 

print(bool(0))
print(bool(None))
print(bool(False))
print(bool([]))
print(bool(""))
print(bool({}))
print(bool(()))

When you execute the above python program, you will get the result as shown below.

 

False
False
False
False
False
False
False

Function Return Types

In python, you can also use Boolean values as the return type of function. Following is the example of using Boolean values as the return type of function in python.

 

def samplefunction():
    return True
if samplefunction():
    print("Function returned True")
else:
    print("Function returned False")

When you execute the above python program, you will get the result as shown below.

 

Function returned True

This is how we can use Boolean types in python to evaluate expressions based on our requirements.