🐍
Python
Python — Confusions, Labs, Gotchas & Mock Interview
🐍
🐍
Python · Section 5 of 5

Python — Confusions, Labs, Gotchas & Mock Interview

🔒

This section is locked

Unlock every deep-dive, lab, mock interview, and memory map across all 10 topics.

View Plans — from ₹299/month

Already have a plan? Sign in

Python — Confusions, Labs, Gotchas & Mock Interview

💡 Interview Tip
Goal: After this page, you should NEVER struggle with Python interview concepts. Where to run: any Python 3.9+ REPL, Jupyter, or https://pythontutor.com (visualizer).

Memory Map

🧠 PYTHON MASTERY → MUTABLE-SCOPE-ITER
PYTHON MASTERYMUTABLE-SCOPE-ITER
───────────────────────────────────
MMutable vs Immutable (list/dict/set vs tuple/str/int)
UUnpacking (*, **, walrus, tuple/list)
TTruthiness ([] is falsy, None is falsy, 0 is falsy)
AArguments (default, *args, **kwargs, positional vs keyword)
BBinding (closures, late-binding, nonlocal, global)
LList vs Dict vs Set complexity (O(n) vs O(1))
EEquality (is vs → → , hash, __eq__)

SECTION 0: TOP 8 PYTHON CONFUSIONS — Cleared Forever

Confusion 1: is vs ==

python — editable
a = [1, 2, 3]
b = [1, 2, 3]
a == b     # True  — values are equal
a is b     # False — different objects in memory!

c = a
c is a     # True  — same object (c is a reference)

Rule:

  • == → VALUES equal (uses __eq__)
  • is → SAME OBJECT in memory (identity check)

When to use is:

python — editable
if x is None:       # ✅ correct — None is a singleton
    ...
if x == None:       # ❌ works but discouraged (triggers __eq__)
    ...

Gotcha — small-int caching:

python — editable
a = 256
b = 256
a is b     # True (CPython caches -5 to 256)

a = 257
b = 257
a is b     # False! (or True, depending on context — DO NOT RELY ON THIS)

Interview answer: "Use is only for None, True, False, or intentional identity checks. Use == for value equality."

Confusion 2: Mutable Default Arguments (THE classic bug)

python — editable
def add_item(item, target=[]):      # ❌ DANGEROUS
    target.append(item)
    return target

print(add_item(1))   # [1]
print(add_item(2))   # [1, 2]       ← WAIT WHAT?
print(add_item(3))   # [1, 2, 3]    ← default is SHARED across calls!

Why? Default values are evaluated ONCE, when the function is DEFINED. The same list object is reus