Global & Local Variable in Python

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

 

 

 1 var = Global Variable
 2 print(var)
 3 
 4 def func1():
 5     var = Local Variable
 6     print(var)
 7 
 8 def func2():
 9     print(var)
10 
11 def func3():
12     global var
13     print (var)
14     var = Global Variable Changed in Function
15 
16 func1()
17 func2()
18 func3()
19 
20 print(var)

 

 

Expected Result:

Global Variable

Local Variable

Global Variable

Global Variable

Global Variable Changed in Function

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。