What is the difference between global and local variables in Python?

A global variable is a variable that is accessible globally. A local variable is one that is only accessible to the current scope, such as temporary variables used in a single function definition.


In the given code

q = "I love coffee" # global variable
def f():
    p = "Me Tarzan, You Jane." # local variable
    print p
print q


The output is as follows

Me Tarzan, You Jane.
I love coffee

In the given code, p is a local variable, local to the function f(). q is a global variable accessible anywhere in the module.