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.
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.
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.
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).
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.
In python, the bool()
function is useful to evaluate any value or variable and return Boolean values, i.e., either True
or False
.
When you execute the above python program, you will get the result as shown below.
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
, False
And empty values such as ""
, ()
, {}
, []
.
Following is the example of using the bool() function to return False value in python.
When you execute the above python program, you will get the result as shown below.
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.
When you execute the above python program, you will get the result as shown below.
This is how we can use Boolean types in python to evaluate expressions based on our requirements.