Table of contents
1.
Introduction
2.
Types of Logical Operators
3.
Syntax and Usage
3.1.
Basic Syntax
3.2.
Operator Precedence
4.
Examples of Logical Operators
4.1.
Example 1: Using and Operator
4.2.
Python
4.3.
Example 2: Using or Operator
4.4.
Python
4.5.
Example 3: Using not Operator
4.6.
Python
5.
Combining Logical Operators
5.1.
Python
6.
Frequently Asked Questions
6.1.
What are logical operators in Python?
6.2.
How do and, or, and not operators work in Python? 
6.3.
What is the precedence of logical operators in Python? 
7.
Conclusion
Last Updated: Aug 25, 2025
Easy

Python Logical Operators

Author Pallavi singh
0 upvote

Introduction

Python logical operators are used to combine conditional statements. They allow you to create more complex conditions that evaluate to either True or False. 

Python Logical Operators

Understanding how these operators work is essential for writing effective and logical expressions in Python programming.

Types of Logical Operators

  1. and Operator
     
  2. or Operator
     
  3. not Operator

Syntax and Usage

Basic Syntax

Logical operators in Python are straightforward to use:

  • and: Returns True if both statements are true.
     
  • or: Returns True if one of the statements is true.
     
  • not: Returns the opposite of the statement.

Operator Precedence

Understanding operator precedence helps in evaluating complex expressions:

  • not has the highest precedence, followed by and, and then or.

Examples of Logical Operators

Example 1: Using and Operator

  • Python

Python

# Example of 'and' operator

a = 10

b = 5

c = 20

if a > b and c > a:

   print("Both conditions are true")

else:

   print("At least one condition is false")
You can also try this code with Online Python Compiler
Run Code


Output:

Both conditions are true

Example 2: Using or Operator

  • Python

Python

# Example of 'or' operator

x = 10

y = 5

if x > 5 or y > 10:

   print("At least one condition is true")

else:

   print("Both conditions are false")
You can also try this code with Online Python Compiler
Run Code


Output

At least one condition is true

Example 3: Using not Operator

  • Python

Python

# Example of 'not' operator

flag = False

if not flag:

   print("Flag is False")

else:

   print("Flag is True")
You can also try this code with Online Python Compiler
Run Code


Output

Flag is False

Combining Logical Operators

You can combine multiple logical operators to create more complex conditions:

  • Python

Python

# Combining logical operators

num = 15

if num > 0 and num % 2 == 0:

   print("Number is positive and even")

else:

   print("Number is either negative or odd")
You can also try this code with Online Python Compiler
Run Code


Output

Number is either negative or odd

Frequently Asked Questions

What are logical operators in Python?

Python logical operators (and, or, not) are used to combine conditional statements.

How do and, or, and not operators work in Python? 

and returns True if both conditions are true. or returns True if at least one condition is true. not returns the opposite boolean value.

What is the precedence of logical operators in Python? 

not has the highest precedence, followed by and, and then or.

Conclusion

Python logical operators are fundamental tools for creating flexible and efficient decision-making in programming. By mastering these operators and understanding their nuances, you can write clearer, more concise, and error-free code.

Live masterclass