Python Booleans

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

// 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.

// Output: True

If we pass zero (0) into bool() function then it would return False. Look at the below example,

// Output: False

Written by
I am a skilled full-stack developer with extensive experience in creating and deploying large and small-scale applications. My expertise spans front-end and back-end technologies, along with database management and server-side programming.

Share on:

Related Posts

Tags: Python,