<

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

x

Logical Operators in Python

Published on
3,226 Points
226 Views
Last Modified:
Editors:
In this article we will see what is logical operator and how we use them in Python programming.

Logical Operators:


There are basically three types of logical operators as below: 

  • And
  • Or
  • Not


There above operators semantics or meaning is the same as their meaning in English.  To demonstrate we can take one simple example like a number can be greater then 2 and less then 5, so if we consider this example then we know that there could be only two possible numbers comes in between i.e. 3 and 4. So now let us run this example and verify the results by passing different numbers. 


my_input = raw_input("Input any number less than 10 >>>")
print("Number entered was ") + my_input
my_input = int(my_input)
if (my_input > 2 and my_input < 5):
    result = True
else:
    result = False
print result

We have entered say 3 then the output of the above code will print as "True" because the condition satisfies that the passed number is greater than 2 and less than 3.  The above code is a simple example of how to use "AND" logical operators in Python. 


Input any number less than 10 >>> 3
Number entered was 3 True
   


Now let us take similar example passing the same number as 3 but using "OR" condition stating can be less then 2 or less then 5 if any one of the condition matches then the result should be true. 


my_input = raw_input("Input any number less than 10 >>>")
print("Number entered was ") + my_input
my_input = int(my_input)
if (my_input < 2 or my_input < 5):
    result = True
else:
    result = False
print result

Below is the result when we passed "3" using OR logical operator to the conditions. 


Input any number less than 10 >>> 3
Number entered was 3 
True
   

And below if the False output as 6 does not fall under any criteria. 


Input any number less than 10 >>> 6
Number entered was 6 
False
   


Now let us take an example on how to use "Not" logical operators.  Say we have a result from some output and now our variable contains the value as  4 and let's call the variable name as "abc". 


Below is my condition to validate if the value from "abc" is present in the list? 


abc = 4
if abc not in [3,7,5] :
    print("condition met")
else:
    print("condition not met")

Result as below.

 

  condition met
   


Now we know how to use logical operators in Python programming. This is a basic explanation on how to use Logical Operators 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.


Edited by: Andrew Leniart

0
Author:Swadhin Ray
0 Comments

Featured Post

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Next Article:

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month