Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In computer programming, logical operators allow you to connect multiple conditions and evaluate compound boolean logic. In Python, logical operations are expressed using clean, plain-English keywords: and, or, and not (rather than cryptic symbols like &&, ||, and ! found in C, Java, or JavaScript).
Beyond basic true/false decisions, Python's logical operators feature two advanced, high-performance mechanisms:
NoneType attribute crashes.and and or return the actual operand object that decided the outcome, enabling elegant fallback and configuration defaults.+-------------------------------------------------------------------------+
| LOGICAL OPERATORS REAL-WORLD ANALOGY |
+-------------------------------------------------------------------------+
1. LOGICAL 'and' (Two-Factor Authentication / 2FA):
- To transfer funds from a bank account:
You need (Valid Password AND Correct SMS OTP).
- If the password is wrong, the server immediately rejects the request.
It does NOT waste time sending or checking an SMS OTP! (Short-Circuit!)
2. LOGICAL 'or' (Payment Mode Acceptance):
- A shopkeeper accepts (UPI QR Code OR Debit Card OR Cash).
- If you successfully pay via UPI, the transaction finishes immediately.
The shopkeeper never asks to swipe your debit card! (Short-Circuit!)
3. LOGICAL 'not' (Flipping Account State):
- The transaction succeeds only if the account is NOT frozen:
can_withdraw = not is_account_frozen
+-------------------------------------------------------------------------+Python's and and or do not enforce rigid boolean casting; they return the determining operand:
and, or, & notBecause and stops immediately if the first operand is falsy, you can use it to write defensive guards that protect subsequent code from fatal crashes:
or (Default Fallbacks)In Python, or returns the first truthy operand it encounters. This is the idiomatic standard for assigning default fallback values:
not > and > orWhen mixing logical operators in a single statement, Python follows strict hierarchy:
notandorand, or, not| Operand A | Operand B | A and B | A or B | not A |
|---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
| Scenario | Anti-Pattern (Don't) | Best Practice (Do) | Why | ||||
|---|---|---|---|---|---|---|---|
| Boolean Keywords | if a && b: or `if a \ | \ | b:` | if a and b: / if a or b: | && and `\ | \ | are syntax errors in Python; use words and, or`. |
| Default Fallbacks | name = user_name if user_name != "" else "Guest" | name = user_name or "Guest" | The or fallback pattern is cleaner, faster, and idiomatic. | ||||
| Redundant Boolean Check | if is_admin is not False: | if is_admin: | Simple truthiness is pythonic and handles general truthy types. | ||||
| Complex Logic | if a and not b or c and d: | if (a and (not b)) or (c and d): | Explicit parentheses prevent subtle operator precedence bugs. |
+-------------------------------------------------------------------------+ | LOGICAL OPERATORS CHEAT SHEET | +-------------------------------------------------------------------------+ - and: Returns True if BOTH are truthy; stops at first falsy - or: Returns True if EITHER is truthy; stops at first truthy - not: Inverts truthiness (not True -> False, not 0 -> True) - Precedence: not > and > or (Always use parentheses for clarity!) - Value Return: x or "Default" returns x if truthy, else "Default" - Short-Circuit:Prevents crashes: if obj is not None and obj.value > 0 +-------------------------------------------------------------------------+
=) with Equality (==)A single equals sign = assigns a value. Two equals signs == checks for equality:
and / orBeginners often write if x == 1 or 2:, which always evaluates to True because non-zero integers are truthy!
A. &&, ||, ! B. AND, OR, NOT C. and, or, not D. &, |, ~
[] or "Default" in Python?A. True B. False C. [] D. "Default"
print(False and (10 / 0 == 1)) in Python?A. It raises a ZeroDivisionError: division by zero B. It prints False without crashing C. It prints None D. It raises a TypeError
A. or B. and C. not D. All three have equal precedence
not "" evaluate to in Python?A. "" B. True C. False D. None
Write a complete Python script that evaluates whether an airline passenger at Indira Gandhi International Airport is cleared for international boarding. The system must test multiple conditional factors—visa validity, security check, baggage weight, and standby priority—using and, or, not, and default fallback values.
In the next lesson, we will continue your Python learning journey with Identity & Membership Operators (6: Operators).
👉 Continue to Next Lesson: Identity & Membership Operators →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Comparison Operators | Identity & Membership Operators |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.