Published on

3,337 Points

Logical Operators in Python - by Swadhin Ray

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*

Enjoy this complimentary article view.

Get unlimited access to our entire library of technical procedures, guides, and tutorials written by certified industry professionals.

Get 7 days freeClick here to view the full article

COLLABORATE WITH CERTIFIED PROFESSIONALS

Experts Exchange is a tech solutions provider where users receive personalized tech help from vetted certified professionals. These industry professionals also write and publish relevant articles on our site.

Ask questions about what you read

If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.

Learn from the best.