Explanation:
x = 5
A variable x is defined in the global scope and assigned the value 5.
def update_value():
A function named update_value is defined.
The function does not take any arguments.
x = 10
Inside the function, a new variable x is defined locally (within the function's scope) and assigned the value 10.
This x is a local variable, distinct from the global x.
print(x)
The function prints the value of the local variable x, which is 10.
update_value()
The update_value function is called.
Inside the function:
A local variable x is created and set to 10.
print(x) outputs 10.
print(x)
Outside the function, the global x is printed.
The global x has not been modified by the function because the local x inside the function is separate from the global x.
The value of the global x remains 5.
Final Output:
10
5
0 Comments:
Post a Comment