← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 8 of 16

Python Logical Operators

Logical operators combine yes-or-no questions, like saying 'you can play if you finished homework AND cleaned your room'. and, or, and not decide whether the whole statement is true.
Syntax
python
result = condition1 and condition2
result = condition1 or condition2
result = not condition

The and Operator

and evaluates to True only when both operands are true; if either side is false, the whole expression is false, making it the natural choice for combining two conditions that must *both* hold.

Example: The and Operator

python
print(True and False)
print(5 > 2 and 3 > 1)

The or Operator

or evaluates to True if at least one operand is true, and only fails when both sides are false — the right tool whenever any one of several conditions being met should trigger a result.

Example: The or Operator

python
print(True or False)
print(5 > 10 or 3 > 1)

The not Operator

not flips a boolean value: not True becomes False and vice versa, most often used to invert a condition cleanly rather than rewriting the comparison itself in its opposite form.

Example: The not Operator

python
print(not True)

Combining Logical Operators

Combining and, or, and not in one expression can get ambiguous quickly, so wrapping sub-conditions in parentheses — (a and b) or c — makes the intended evaluation order explicit to both Python and the next person reading the code.

Example: Combining Logical Operators

python
a, b, c = True, False, True
print((a and b) or c)

Short-circuit Evaluation

Python short-circuits logical evaluation: in a and b, if a is already false, b is never evaluated at all, and in a or b, if a is already true, b is skipped.

This isn't just an optimization — code sometimes deliberately relies on it, like obj and obj.value to avoid an error on a None object.

Example: Short-circuit Evaluation

python
obj = None
print(obj and obj.upper())
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing if x == 1 or 2:, which is always truthy because 2 alone is true; write x == 1 or x == 2 or x in (1, 2).
  2. Using &&, || or ! from other languages, which are SyntaxErrors; Python uses and, or, not.
  3. Assuming or returns True/False; "" or "default" returns the string "default".

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.