Code Explanation:
1. Global Variable Declaration
x = 100
A global variable x is defined and set to 100.
2. Function Definition: func
def func():
A function func() is defined. It contains:
a local variable x = 200
an inner function inner() that prints 300
calls to inner() and print(x)
3. Inside func()
x = 200
A new local variable x is declared inside func(), and its value is set to 200.
This does NOT affect the global x (x = 100 outside remains unchanged).
def inner(): print(300)
A nested function inner() is defined.
When called, it will print 300.
inner()
Calls the inner function.
Output:
300
print(x)
Prints the local variable x from func(), which is 200.
Output:
200
4. Call func()
func()
Executes all the code inside the function:
inner() → prints 300
print(x) inside func() → prints 200
5. Final Line: Print Global x
print(x)
This is outside of any function.
Refers to the global variable x = 100
Output:
100
Final Output:
300
200
100
.png)

0 Comments:
Post a Comment