Introduction to Logical Operators in Python

Introduction to Logical Operators in Python

5 mins read935 Views Comment
Updated on Sep 13, 2023 18:34 IST

In Python, operators are special characters or symbols that are used to perform arithmetic or logical operations on certain values and variables. These values that the operators operate on are known as Operands.

2022_06_Untitled-design-3.jpg

In this article, we will focus on logical operators and understand how they perform computations in Python through examples. So, without further ado, let’s get started!

Learn more about Python; read our blog – What is Python?

Also, explore Conditional Statements in Python

We will be covering the following sections today: 

What are Logical Operators? 

Logical Operators are used on conditional statements in Python as they evaluate the conditions (True or False) between operands. They are also referred to as Boolean Operators because they automatically convert operands to a Boolean, if they were not already, for evaluation.  

They perform three operations – Logical AND, Logical OR, and Logical NOT.  

Now, let’s discuss each logical operator in detail: 

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

Free
6 weeks
– / –
2 weeks
– / –
16 weeks
1.7 K
3 months
– / –
– / –
4.24 K
2 weeks
3 K
3 weeks
– / –
4 months

What are Logical AND Operator? 

The logical AND operator returns True if all the operands (conditions) are true. If even one of them is false, it returns False. 

2022_06_image-203.jpg

 

Let’s understand with a few examples in Python – 

Example 1: 

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #logical AND \n \n
\n \n
x \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n \n 13 \n \n
y \n \n <span style="color: #66cc66">\n =  \n <span style="color: #ff4500">\n 10 \n
z  \n <span style="color: #66cc66">\n =- \n <span style="color: #ff4500">\n 10    \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n if x  \n <span style="color: #66cc66">\n > y  \n <span style="color: #ff7700;font-weight:bold">\n and y  \n <span style="color: #66cc66">\n > z: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "x is the greatest number" \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n else: \n
   \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "x is not the greatest number" \n <span style="color: black">\n ) \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n if x  \n <span style="color: #66cc66">\n >  \n <span style="color: #ff4500">\n 0  \n <span style="color: #ff7700;font-weight:bold">\n and y  \n <span style="color: #66cc66">\n >  \n <span style="color: #ff4500">\n 0  \n <span style="color: #ff7700;font-weight:bold">\n and z  \n <span style="color: #66cc66">\n >  \n <span style="color: #ff4500">\n 0: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "All numbers are positive" \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n else: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Atleast one number is negative" \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66">\n \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

2022_06_image-206.jpg

In the above code, we used the AND operator between the operands to check if all the conditions are true: 

  • In the first case, both conditions are true, i.e., x>y and y>z. Hence, we print the if statement. 
  • In the second case, however, the third condition is false. i.e., z < 0. Hence, we print the else statement because all the conditions are not satisfied.  

Let’s look at another example – 

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #logical AND \n \n
\n \n
pie \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n \n 3.14 \n \n
pie \n \n <span style="color: #66cc66">\n >  \n <span style="color: #ff4500">\n 1  \n <span style="color: #ff7700;font-weight:bold">\n and pie  \n <span style="color: #66cc66">\n <  \n <span style="color: #ff4500">\n 3 \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66">\n \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

2022_06_image-207.jpg

As the value of the pie is clearly greater than 3, the second condition in the above code is false. Hence, False is displayed in the output. 

Check out the best Python Courses online

Point to be noted: 

Keep in mind that while using the AND operator, if the first condition is evaluated to be false, the result is False right away and the further conditions are not evaluated. 

Logical OR Operator 

The logical OR operator returns True if either of the operands (conditions) is true. If both conditional statements are false, it returns False. 

2022_06_image-209.jpg

Let’s understand with a few examples in Python – 

Example 1: 

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #logical OR \n \n
\n \n
a \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n \n 13 \n \n
b \n \n <span style="color: #66cc66">\n =- \n <span style="color: #ff4500">\n 10 \n
c  \n <span style="color: #66cc66">\n =  \n <span style="color: #ff4500">\n 0 \n
   \n
\n <span style="color: #ff7700;font-weight:bold">\n if a  \n <span style="color: #66cc66">\n > c  \n <span style="color: #ff7700;font-weight:bold">\n or b  \n <span style="color: #66cc66">\n > c: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "One of the numbers is greater than c" \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n else: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "c is the greatest number" \n <span style="color: black">\n ) \n
\n
\n <span style="color: #ff7700;font-weight:bold">\n if b  \n <span style="color: #66cc66">\n >  \n <span style="color: #ff4500">\n 0  \n <span style="color: #ff7700;font-weight:bold">\n or c  \n <span style="color: #66cc66">\n >  \n <span style="color: #ff4500">\n 0: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Either of the numbers is greater than 0" \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n else: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Both numbers are not greater than 0" \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66">\n \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

2022_06_image-210.jpg

In the above code, we used the OR operator between the operands to check if either of the conditions is true: 

  • In the first case, the first expression is true, i.e., a>c. Hence, we print the if statement. 
  • In the second case, however, both expressions are false because b < 0 and c = 0. Hence, we print the else statement because none of the conditions are satisfied.  

Point to be noted: 

Keep in mind that while using the OR operator, if the first condition is evaluated to be true, the result is True right away and the further conditions are not evaluated. 

Check out the most commonly asked Python Interview Questions and Answers

Let’s look at another example to understand this – 

Example 2: 

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #logical OR \n \n
\n \n
pie \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n \n 3.14 \n \n
pie \n \n <span style="color: #66cc66">\n >  \n <span style="color: #ff4500">\n 1  \n <span style="color: #ff7700;font-weight:bold">\n or pie  \n <span style="color: #66cc66">\n <  \n <span style="color: #ff4500">\n 3 \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66">\n \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

2022_06_image-213.jpg

As the value of the pie is clearly greater than 1, the first condition in the above code is true. So, there is no need to evaluate the second condition and True is displayed in the output. 

Logical NOT Operator 

The logical NOT operator considers a single conditional statement. If the operand is true, it returns False and vice-versa. 

2022_06_image-216.jpg

Let’s understand with a few examples in Python – 

Example 1: 

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #logical NOT \n \n
\n \n
num \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n \n 30 \n \n
\n \n
\n \n <span style="color: #ff7700;font-weight:bold">\n if  \n <span style="color: #ff7700;font-weight:bold">\n not  \n <span style="color: black">\n (num% \n <span style="color: #ff4500">\n 3  \n <span style="color: #66cc66">\n ==  \n <span style="color: #ff4500">\n 0  \n <span style="color: #ff7700;font-weight:bold">\n and num% \n <span style="color: #ff4500">\n 5  \n <span style="color: #66cc66">\n ==  \n <span style="color: #ff4500">\n 0 \n <span style="color: black">\n ): \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "30 is not divisible by either 3 or 5" \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n else: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "30 is divisible by both 3 or 5" \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #66cc66"> \n </span style="color: #ff4500"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

2022_06_image-217.jpg

In the above code, we used the NOT operator with the combination of AND operator to check if both the conditions are true: 

  • If both expressions are true – that is, if the AND operator returns True, then the NOT operator would return False.  
  • Else, either of the expressions is false – that is, the AND operator returns False. Hence, the NOT operator would return True. 

Let’s look at another example – 

Example 2: 

 
\n \n <pre class="python" style="font-family:monospace">\n \n <span style="color: #808080;font-style: italic">\n \n #logical NOT \n \n
\n \n
pie \n \n <span style="color: #66cc66">\n \n = \n \n <span style="color: #ff4500">\n \n 3.14 \n \n
\n \n
\n \n <span style="color: #ff7700;font-weight:bold">\n if \n <span style="color: #ff7700;font-weight:bold">\n not \n <span style="color: #ff4500">\n 3.14: \n
     \n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Boolean value of pie is True (pie is not 3.14)" \n <span style="color: black">\n ) \n
\n <span style="color: #ff7700;font-weight:bold">\n else: \n
\n <span style="color: #ff7700;font-weight:bold">\n print \n <span style="color: black">\n ( \n <span style="color: #483d8b">\n "Boolean value of pie is False (pie is 3.14)" \n <span style="color: black">\n ) \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: black"> \n </span style="color: #483d8b"> \n </span style="color: black"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff4500"> \n </span style="color: #ff7700;font-weight:bold"> \n </span style="color: #ff7700;font-weight:bold">\n \n </span style="color: #ff4500">\n \n </span style="color: #66cc66">\n \n </span style="color: #808080;font-style: italic">\n \n </pre class="python" style="font-family:monospace">
Copy code

Output:

2022_06_image-224.jpg

As the value of pie is 3.14, the NOT operator would return False in the output. 

You can also explore – Python Projects for Beginners

Precedence of Logical Operators 

If there are multiple operators at work, Python evaluates them in order of their predefined precedence: 

Logical Operator  Precedence 
not  High 
and  Medium 
or  Low 

So, expressions are grouped based on the highest precedent operator first, followed by the groupings for lower precedent operators, and so on.  

Point to be noted: 

If multiple operators of the same precedence are at work, Python always evaluates the conditional statements from left to right. 

Consider the below examples: 

  • a or b or c would be evaluated as a or (b or c) 
  • a and b or c and d would be evaluated as (a and b) or (c and d) 
  • a and b and c or d would be evaluated as ((a and b) and c) or d 
  • not a and b or c would be evaluated as ((not a) and b) or c 

Summary Table: 

Operator Name  Operator  Description  Syntax  Example 
Logical AND  and  Returns True if all operands are true, else False.  x and y  X = True, Y = False Returns False 
Logical OR  or  Returns True if even one operand is true, else False.  x or y  X = True, Y = False Returns True 
Logical NOT  not  Complements the operand – returns True if the operand is false, and vice-versa.  not x  X = True, Returns False 

Endnotes 

Hope this article was helpful for you to understand how to use logical operators. We learned about the three logical operators – AND, OR, and NOT. We also discussed their order of precedence. Want to learn more about Python and practice Python programming? Explore related articles here

Top Trending Tech Articles:
Career Opportunities after BTech Online Python Compiler What is Coding Queue Data Structure Top Programming Language Trending DevOps Tools Highest Paid IT Jobs Most In Demand IT Skills Networking Interview Questions Features of Java Basic Linux Commands Amazon Interview Questions

Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio