Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Global Variable and Keyword in Python
February 18, 2021
Global Variable and Keyword
A global variable can be used anywhere in the program as its scope is the entire program. If the value of a global variable is changed anywhere in the program then the global variable value is changed for the entire program.
A global keyword is a keyword that allows users to modify a variable outside of the current scope. It is used to create global variables from a non-global scope i.e inside a function.
Syntax :
global myVariable
Example :
#global variable
a=10
b=20
#function
defupdate():
#global keyword
global a
#value global variable a gets changed
a=100
#b will behave as a local variable here
b=200
print(” Inside the function”)
#accessing variables
print(a)
print(b)
#calling function
update()
print(“\n Outside the function”)
#accessing variables
print(a)
print(b)
Output :
Inside the function
100
200
Outside the function
100
20
Login/Signup to comment