How to declare global variable in Python
Variables that are defined and declared outside a function is called global variable. Such variables can be used inside as well as outside the function.
a = 5 def test(): print ("Value of a is ",a) test()
Value of a is 5
If we create a variable with the same name as that of a global variable inside a function, this variable will be local, that is it can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value.
a = 5 def test(): a = 10 print ("Value of a is ", a) test() print ("Value of a is ", a)
Value of a is 10 Value of a is 5
Please login to comment
View all comments