Variables in Python are dynamically typed reference labels bound to objects stored in heap memory. Assigning a variable does not copy data; it creates a reference to an underlying object.
flowchart LR
subgraph Stack Scope
x["x"]
y["y"]
end
subgraph Heap Memory
obj1["int Object: 42"]
obj2["list Object: [1, 2, 3]"]
end
x --> obj1
y --> obj2
_.totalAmount and total_amount are distinct).snake_case for variables and function names (user_session_token).UPPER_SNAKE_CASE for module-level constants (MAX_RETRY_ATTEMPTS = 5).from typing import List
# Constant definition
MAX_REQUEST_LIMIT: int = 100
def demonstrate_variable_references() -> None:
# Value assignment & object identity (id())
a: int = 500
b: int = a # 'b' points to the same integer object in heap
print(f"a = {a}, id(a) = {id(a)}")
print(f"b = {b}, id(b) = {id(b)}")
print(f"a is b: {a is b}")
# Reassigning 'a' creates a reference to a new integer object
a = 600
print(f"After reassigning a -> 600: a = {a}, b = {b}")
print(f"a is b: {a is b}")
# Mutable object behavior
list_one: List[int] = [1, 2, 3]
list_two: List[int] = list_one
list_two.append(4)
print(f"Modified list_two affects list_one: {list_one}")
if __name__ == "__main__":
demonstrate_variable_references()
is vs ==: == checks value equality (are contents equal?), whereas is checks object identity (do variables reference the exact same memory address?).x, y = 10, 20 or extended unpacking first, *rest = [1, 2, 3, 4].Create two variables containing identical lists [10, 20]. Compare them using both == and is. Print and explain why == returns True while is returns False.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With