Use of global key word:
In Python, global keyword allows the programmer to modify the variable outside the current scope. It is used to create a global variable and make changes to the variable in local context. A variable declared inside a function is by default local and a variable declared outside the function is global by default. The keyword global is written inside the function to use its global value. Outside the function, global keyword has no effect.
Example :
c = 10 # global variable
def add(): global c
c = c + 2 # global value of c is incremented by 2
print("Inside add():", c)
add()
c=15 print("In main:", c)
output:
Inside add() : 12
In main: 15