Step-by-Step Explanation:
-
person = "Bob"
A variable person is assigned the string value "Bob". -
Calling change_name(person)
The function change_name is called with person as the argument. So inside the function, name becomes "Bob" (a copy of the string). -
Inside change_name
This line:reassigns the local variable name to the string "Alice".
However, this does not change the original variable person outside the function.-
In Python, strings are immutable.
-
When you pass person to the function, you pass a copy of the reference to "Bob".
-
Reassigning name to "Alice" just makes the local variable name point to a different string.
-
The original person variable still points to "Bob".
-
-
print(person)
This prints "Bob" because the original person was never changed outside the function.
✅ Final Output:
Key Concept:
In Python, immutable types like strings cannot be changed in-place.
Reassigning a function argument only affects the local copy, not the original variable.


0 Comments:
Post a Comment