Lessons
Courses

Logical Operators in Python [ English ]

1. Introduction

Logical operators in Python are used to combine multiple conditions and evaluate them as a single expression. They return a Boolean value (True or False).

They are mainly used in:

  • Decision-making (if, while)
  • Complex conditions

2. Types of Logical Operators

Python has three logical operators:

OperatorNameDescription
andLogical ANDTrue if both conditions are True
orLogical ORTrue if at least one condition is True
notLogical NOTReverses the result

3. Logical AND (and)

Definition

Returns True only if both conditions are True.

Truth Table

ABA and B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Example

a = 10 print(a > 5 and a < 20)print(a > 5 and a < 8)

Output

TrueFalse

4. Logical OR (or)

Definition

Returns True if at least one condition is True.

Truth Table

ABA or B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Example

a = 10 print(a > 5 or a < 8)print(a < 5 or a < 8)

Output

TrueFalse

5. Logical NOT (not)

Definition

Reverses the Boolean value.

Truth Table

Anot A
TrueFalse
FalseTrue

Example

a = 10 print(not(a > 5))

Output

False

6. Using Logical Operators in Conditions

age = 20 if age >= 18 and age <= 60: print("Eligible")

7. Short-Circuit Evaluation

Python uses short-circuiting:

  • In and: stops if first condition is False
  • In or: stops if first condition is True

Example

print(False and True) # second condition not checkedprint(True or False) # second condition not checked

8. Logical Operators with Non-Boolean Values

Logical operators can return actual values (not just True/False).

print(0 and 5)print(5 and 10) print(0 or 5)print(5 or 10)

Output

01055

9. Practical Example

username = "admin"password = "1234" if username == "admin" and password == "1234": print("Login successful")else: print("Invalid credentials")

10. Important Points

  • Used to combine conditions
  • Return True or False
  • Support short-circuit evaluation
  • Can work with non-boolean values

11. Summary

  • Logical operators are used for decision-making.

  • Types include:

    • and → both conditions must be True
    • or → at least one condition must be True
    • not → reverses result
  • Useful in if statements and loops.

  • Supports short-circuit evaluation.