[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


Boolean in Python

Published on
3,171 Points
Last Modified:
In this article we will see how to use Boolean in Python where it can be used to take any decisions if it is true or false , yes or no,  1 or 0.

 What is Boolean?

It is just an integer that can have 1 ,0,-1 or floats like 0.32, 3.32 etc. The output of the decisions can be true or false. 

In many cases during writing code or implementing any comparison to consider any decision we usually use boolean in any programming language. 

Here we will see some basic examples on how we can use them on Python.  Before we use it let us see the type of True and False. 

print type(True)
print type(False)

Once you run the above code you will see the output as result shown below:

<type 'bool'> 
<type 'bool'>

Bool is nothing but represents as Boolean. 

Now let us see some examples of assigning True and False to some variables and verify some comparisons. 



In the above code we have assigned the Boolean values to "x" and "y" variables and printing it. 


Now we can see that the first variable x is displayed as True and y displayed as False because we specifically assigned these values. 

In another case let us compare the variables assigning some numbers and validate if the conditions are true or false to make a decision. 

Say my input is expected as " 999 "on a variable called "a" and I want to take a decision to proceed or to stop on my program. 

print a==998

 The output of the above code will print as False because "==" is used to compare the assigned value i.e. 999 against 998 which is not correct.


Now let us compare it with the same number :

print a==999

Now we can see the result is True. Let us also try to use it with other comparisons. 

print a==999
print a <1200
print a >2000

Now based on the assigned value to variable a is compared with different numbers by validating if is the same or less then the compared value or greater then the assigned value. 

This is a basic explanation on how to use Boolean in Python programming language. 

Thank you for reading my article, please feel free to leave me some feedback or to suggest any future topics.

I'll be looking forward to hearing from you – Swadhin Ray (Sloba)

For more information about me, please check out my Experts Exchange Profile page.

Author:Swadhin Ray

Featured Post

Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Screencast - Getting to Know the Pipeline

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month