Step-by-step Explanation:
-
x = 5
You define a variable x and assign it the integer value 5. -
Calling double(x)
You pass x to the function double. -
Inside the function double(n)
n receives a copy of the value of x (which is 5).
The line n *= 2 is the same as n = n * 2, so n becomes 10.However, this change only affects the local variable n, not the original x.
-
Back in the main program
After the function call, x is still 5 because:-
Integers are immutable in Python.
-
Assigning a new value to n inside the function does not change x.
-
✅ Final Output:
Key Concept:
In Python, immutable objects like integers, strings, and tuples are passed by value (actually, by object reference, but since they can't change, it's like by value). So any changes inside a function don't affect the original variable.


0 Comments:
Post a Comment