Nearby lessons

32 of 159

Python - Logical Operators

📌 What You Will Learn
  • Define logical operators and how they combine conditions
  • Use the and, or, and not operators
  • Read and apply truth tables for logical operators
  • Explain how Python treats numbers as truthy or falsy
  • Understand short-circuit evaluation

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

🐍Code Cell
1print(True and True)
2print(True and False)
Output
True
False

Example - AND with Relational Operators

🐍Code Cell
1a = 10
2b = 20
3 
4print(a < b and b > 10)
Output
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)

🐍Code Cell
1print(10 > 5 and 20 < 10)
Output
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

🐍Code Cell
1print(True or False)
2print(False or False)
Output
True
False

Example - OR with Relational Operators

🐍Code Cell
1a = 10
2b = 20
3 
4print(a > b or b > 10)
Output
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)

🐍Code Cell
1print(10 < 5 or 20 < 10)
Output
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

🐍Code Cell
1print(not True)
2print(not False)
Output
False
True

Example - NOT with Condition

🐍Code Cell
1a = 10
2 
3print(not(a > 5))
Output
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

🐍Code Cell
1print(10 and 20)
2print(0 and 20)
Output
20
0

Example - OR with Numbers

🐍Code Cell
1print(10 or 20)
2print(0 or 20)
Output
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

🐍Code Cell
1print(False and 10 / 0)
Output
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

🐍Code Cell
1print(True or 10 / 0)
Output
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

🐍Code Cell
1age = 25
2citizen = True
3 
4if age >= 18 and citizen:
5 print("Eligible")
Output
Eligible

Using Logical Operators in Login Validation

🐍Code Cell
1username = "admin"
2password = "1234"
3 
4if username == "admin" and password == "1234":
5 print("Login Successful")
Output
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
📝 Key Takeaways
  • Logical operators combine multiple conditions
  • and returns True only when both conditions are True
  • or returns True when at least one condition is True
  • not reverses a Boolean value
  • 0 is treated as False and any non-zero value as True
  • Short-circuit evaluation skips unnecessary conditions

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8