Global & Local Variable in Python

时间:2022-11-01 06:43:45

Following code explain how 'global' works in the distinction of global variable and local variable.

 var = 'Global Variable'
print(var) def func1():
var = 'Local Variable'
print(var) def func2():
print(var) def func3():
global var
print (var)
var = 'Global Variable Changed in Function' func1()
func2()
func3() print(var)

Expected Result:

Global Variable

Local Variable

Global Variable

Global Variable

Global Variable Changed in Function