Pass path: Learn Python indentation rules, difference between == and is, dynamic typing, and venv setup.
Top path: Explain CPython memory management with PyObject headers, reference counting, and Python byte-code compilation (.pyc).
Part 1: Core Concepts (2 Marks)
★ Dynamic Typing in Python: Variables do not have declared data types; types are bound dynamically to objects in heap memory at runtime, allowing variable names to bind to different object types across execution.
★ Identity (is) vs Equality (==): == checks value equality (whether values of two objects are equivalent via __eq__); is checks memory identity (whether both variables reference the exact same memory address via id(a) == id(b)).
★ Virtual Environment (venv): An isolated directory tree containing a specific Python interpreter and independent set of installed libraries, preventing version conflicts between project dependencies.
Part 2: Structural Frameworks (10 Marks)
Python Architecture, Dynamic Typing, and the id() Function
- How Python Executes Code:
- Source Code (
.py) is parsed by CPython into an Abstract Syntax Tree (AST). - AST is compiled into platform-independent Python Bytecode (
.pycfiles stored in__pycache__). - Python Virtual Machine (PVM) interprets bytecode into CPU machine instructions.
- Source Code (
- Memory Identity and Integer Interning:
# Variables are references to objects in memory
x = 256
y = 256
print(x == y) # True (same value)
print(x is y) # True (small integer interning: -5 to 256 share memory)
a = 1000
b = 1000
print(a == b) # True (same value)
print(a is b) # False in REPL (distinct memory objects allocated)
print(id(a), id(b))
- Operators Summary:
- Identity:
is,is not - Membership:
in,not in - Floor Division:
//(truncates towards negative infinity) - Exponentiation:
**
- Identity:
Part 3: Distinction Tables
| Criteria | Identity Operator (is) |
Equality Operator (==) |
|---|---|---|
| Comparison Target | Checks physical memory address (id(a) == id(b)) |
Checks data contents/values (a.__eq__(b)) |
| Overloadable | No; identity check cannot be overridden by user classes | Yes; custom classes override __eq__ method |
| Best Practice Use | Comparing singletons (e.g. if val is None:) |
Comparing data values (strings, numbers, lists) |
| Speed | Extremely fast (single pointer address comparison) | Can be slow (recursively checks collection items) |
Part 4: Worked Problems & Exam Traps
- Easy: What does the
id()function return in Python?- Answer: An integer representing the unique identity of an object, corresponding to its actual memory address in CPython.
- Exam-level: Explain why
a = [1, 2]; b = [1, 2]; a == bevaluates toTruewhilea is bevaluates toFalse.- Answer:
a == bcompares the contents of the lists, which are identical[1, 2]. However, lists are mutable objects, so Python allocates two distinct heap memory buffers; hence their memory addresses differ (id(a) != id(b)), makinga is bfalse.
- Answer:
- Twist variant: What does
5 // -2evaluate to in Python?- Answer:
-3(NOT-2), because Python’s floor division rounds towards negative infinity ().
- Answer:
Students lose marks here because they use is to compare string values (e.g. if name is "Ram":), which fails when string interning boundaries are exceeded; value comparison must always use ==.