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

Loading editor…

Example - AND with Relational Operators

Loading editor…

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)

Loading editor…

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

Loading editor…

Example - OR with Relational Operators

Loading editor…

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)

Loading editor…

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

Loading editor…

Example - NOT with Condition

Loading editor…

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

Loading editor…

Example - OR with Numbers

Loading editor…

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

Loading editor…

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

Loading editor…

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

Loading editor…

Using Logical Operators in Login Validation

Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.