Python is neither pass-by-value nor pass-by-reference.
It uses a mechanism called pass-by-assignment (also widely referred to as pass-by-object-reference or call-by-sharing).
In Python, every single value is an object, and every variable name is just a label (reference) pointing to that object in memory. When you pass an argument to a function, Python copies the label, not the object itself.
Passing Immutable Objects (Acts like Pass-by-Value): Objects like integers, floats, strings, and tuples cannot be changed after they are created. If you try to modify an immutable object inside a function, Python creates a completely new object somewhere else in memory and moves the local variable name to point to it. The original variable outside the function remains untouched.
def modify_number(x):
print(f"Inside (before): x points to ID {id(x)}")
x = 100 # Creates a NEW integer object; x now points to 100
print(f"Inside (after): x points to NEW ID {id(x)}")
num = 10
print(f"Outside (before): num points to ID {id(num)}")
modify_number(num)
print(f"Outside (after): num is still {num}")
• The value of num remains 10.
• Reassigning x = 100 inside the function simply breaks the link between x and 10, attaching x to 100 instead.If you pass a MUTABLE object (Lists, Dictionaries, Sets): It behaves like pass-by-reference. Since the object can be modified in-place, using methods that alter the object (like
.append()or.update()) will directly change the original object living outside the function.
def append_item(my_list):
my_list.append(4) # Modifies the original object in place
numbers = [1, 2, 3]
append_item(numbers)
print(f"Outside: numbers is now {numbers}")
• numbers becomes [1, 2, 3, 4].
• Because both the outside variable numbers and the inside parameter my_list point to the exact same block of memory, changing the contents inside reflects everywhere.