Boolean in Python

Swadhin Ray
Edited by: Andrew Leniart
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.

Swadhin Ray

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.