Last updated on March 17, 2023
In Python, Boolean contains two values that are true or false. In programming it is very common to use Booleans, it helps you find out if an expression is true or false.
Uses of Boolean in Comparing Two values
Let’s compare two identical integer values through the equal operator.
print(40 == 40);
// Output: True
The above example returns the Boolean value true because both integers are identical. Now let’s compare non-identical integer values with the same operator that we used in the previous example.
print(40 == 30);
// Output: False
In this example, we are comparing two different integer values which return the false Boolean value in the output.
Uses of Boolean in Functions
You can also use Boolean in function which means the return value can be a Boolean. Look at the below example,
def anyFunction():
return True
print(anyFunction())
// Output: True
In this example, the function simply returns a true Boolean value. That’s why the print() function returns True in the output we passed the function.
Test Values with bool() Function
The bool() function takes a value as an argument and returns a Boolean value. For example, if you pass 1 into the bool() function then it would return its Boolean value which will be True.
print(bool(1));
// Output: True
If we pass zero (0) into bool() function then it would return False. Look at the below example,
print(bool(0));
// Output: False