Nearby lessons

37 of 108

Python - Logical Operators

Introduction

Logical operators are used to combine multiple conditions.

The result of logical operators is generally a Boolean value:

  • True
  • False

Logical Operators List

Operator Meaning
and Logical AND
or Logical OR
not Logical NOT

1. AND Operator

The and operator returns True only when both conditions are True.

If any one condition is False, the result becomes False.

Truth Table - AND Operator

Condition 1 Condition 2 Result
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Example - AND Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Example - AND with Relational Operators

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Explanation

The first condition a < b is True.

The second condition b > 10 is also True.

Since both conditions are True, the final result is True.

Example - AND (False Case)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False

Explanation

The first condition is True.

The second condition 20 < 10 is False.

Since one condition is False, the final result is False.

2. OR Operator

The or operator returns True if at least one condition is True.

Truth Table - OR Operator

Condition 1 Condition 2 Result
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Example - OR Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Example - OR with Relational Operators

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Explanation

The first condition a > b is False.

The second condition b > 10 is True.

Since one condition is True, the final result is True.

Example - OR (False Case)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False

Explanation

Both conditions are False.

Therefore, the final result is False.

3. NOT Operator

The not operator reverses a Boolean value.

Truth Table - NOT Operator

Input Result
TrueFalse
FalseTrue

Example - NOT Operator

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False
True

Example - NOT with Condition

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False

Explanation

The condition a > 5 is True.

The not operator reverses it to False.

Logical Operators with Numbers

In Python:

  • 0 is treated as False.
  • Any non-zero value is treated as True.

Example - AND with Numbers

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
20
0

Example - OR with Numbers

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20

Important Rule

AND Operator:

  • Returns the first False value.
  • If all values are True, it returns the last value.

OR Operator:

  • Returns the first True value.
  • If all values are False, it returns the last value.

Short Circuit Evaluation

Python supports Short Circuit Evaluation.

If the final result can be determined from the first condition, Python does not evaluate the remaining conditions.

Example - Short Circuit with AND

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
False

Why No Error?

The first value is False.

Python already knows the final result will be False, so it skips evaluating 10 / 0.

Example - Short Circuit with OR

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True

Why No Error?

The first value is True.

Python skips the second expression because the final result is already known.

Using Logical Operators in if Statement

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Eligible

Using Logical Operators in Login Validation

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Login Successful

Difference Between AND and OR

AND OR
Both conditions must be True. At least one condition must be True.

Real World Usage

Logical operators are commonly used in:

  • Login systems
  • Decision making
  • Input validation
  • Searching
  • Filtering data

Important Notes

  • Logical operators combine multiple conditions.
  • The result is generally True or False.
  • Python supports short circuit evaluation.
  • The not operator reverses a Boolean value.
  • When used with numbers, 0 is treated as False and non-zero values are treated as True.

Quick Summary

Operator Purpose
and Both conditions must be True.
or At least one condition must be True.
not Reverses a Boolean value.

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Assignment Operators