🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

Logical Operators

AI & DATA SCIENCE // logical-operators

Logical operators (and, or, not) combine or invert boolean expressions, and Python's and/or also short-circuit, returning one of the original operands rather than always a plain True/False.

Syntax

a and b
a or b
not a

Deep Dive Course

Unlike some languages where and/or always produce a boolean, Python's `and` returns the first operand if it's falsy, otherwise the second operand, and `or` returns the first operand if it's truthy, otherwise the second — both short-circuit, meaning the second operand isn't even evaluated if the result is already determined by the first. This makes a pattern like falling back to a default value with `or` idiomatic in Python.

1Understanding Logical Operators

Unlike some languages where and/or always produce a boolean, Python's and returns the first operand if it's falsy, otherwise the second operand, and or returns the first operand if it's truthy, otherwise the second — both short-circuit, meaning the second operand isn't even evaluated if the result is already determined by the first. This makes a pattern like falling back to a default value with or idiomatic in Python.

💡

Using `or` for a fallback value is a common idiom, but it treats any falsy value, like 0, an empty string, or an empty list, as 'missing', not just None — check explicitly for None instead when only that specific value should trigger the fallback.

editor.html
print(True and False)
print(0 or "fallback")
print(not True)
localhost:3000

2Practical Example

Here is a real-world application of Logical Operators showing how it is used in production Python code.

editor.html
user = None
name = user and user.get("name")
print(name)
localhost:3000

3Best Practices

Follow these guidelines when working with Logical Operators:

1. Use a or default for concise fallback values, but only when a falsy value like 0 or an empty string isn't itself meaningful for a

2. Rely on short-circuiting to avoid calling an expensive or error-prone function unnecessarily

3. Use not for boolean inversion instead of comparing explicitly to False

⚠️

Tip: Using `or` for a fallback value is a common idiom, but it treats any falsy value, like 0, an empty string, or an empty list, as 'missing', not just None — check explicitly for None instead when only that specific value should trigger the fallback.

editor.html
print(True and False)
print(0 or "fallback")
print(not True)
localhost:3000

Examples

Example 01Basic Usage
print(True and False)
print(0 or "fallback")
print(not True)
Example 02Advanced Example
user = None
name = user and user.get("name")
print(name)

Best Practices

  • Use a or default for concise fallback values, but only when a falsy value like 0 or an empty string isn't itself meaningful for a
  • Rely on short-circuiting to avoid calling an expensive or error-prone function unnecessarily
  • Use not for boolean inversion instead of comparing explicitly to False

Interview Question

Why does an expression like `0 or 'fallback'` return the string 'fallback' instead of a boolean?

Hint: Think about what value Python's `or` actually returns, not just whether the expression is 'true'.

Python's `or` doesn't return a plain boolean — it evaluates the first operand, and if it's falsy, like 0, it returns the second operand exactly as-is, without converting it to True/False. Since 0 is falsy, that expression evaluates and returns the string 'fallback' itself, which is what makes this pattern useful for supplying default values.

Exercises

MediumPractice using Logical Operators in a real scenario.
View Solution
print(True and False)
print(0 or "fallback")
print(not True)

Frequently Asked Questions

Why does an expression like `0 or 'fallback'` return the string 'fallback' instead of a boolean?

Python's `or` doesn't return a plain boolean — it evaluates the first operand, and if it's falsy, like 0, it returns the second operand exactly as-is, without converting it to True/False. Since 0 is falsy, that expression evaluates and returns the string 'fallback' itself, which is what makes this pattern useful for supplying default values.

Related Functions

comparison-operatorsbooleansconditional expressions