<

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

x

Boolean in Python

Published on
3,109 Points
109 Views
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. 


x=True
y=False

print(x)
print(y)

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


True
False
   

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. 


a=999
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.


  False
   

Now let us compare it with the same number :


a=999
print a==999
  True
   

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


a=999
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.



0
Comment
Author:Swadhin Ray
0 Comments

Featured Post

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Join & Write a Comment

Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. In addition to the name of the font, it shows the font type and whether or not the font is embedded in the PDF f…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month