The visibility of a variable determines its lifetime, and two types of visibility are considered in Python, namely local and global variables.
Local variables
Local variables are defined inside a function and not accessible outside it.
# Example function with two local variables
def example (a):
b = 2
print(a, b)
# Call it and try to access variables
example(1)
print(a, b) # -> NameError: name 'a' is not defined
Global variables
Global variables are defined outside all functions and accessible anywhere in the code.
# Define a global variable
a = 1
# Example function with a local variable, also named "a"
def example ():
a = 2
print(a)
# See what we got
print(a)
example()
print(a)