Local & Global Variable

A local variable is declared inside a function or block. It is accessible only within the function where it is defined.

>>> def mysample():
...     x = "This is a sentence"
...     print(x)
... 
>>> mysample()
This is a sentence

A global variable is declared outside of all functions, usually at the top level of a script. It is accessible throughout the entire program, including inside functions with conditions.

>>> x = "This is a sentence"
>>> def method1():
...     print(x)
... 
>>> def method2():
...     print(x)
... 
>>> method1()
This is a sentence
>>> method2()
This is a sentence

Local variables can help run applications faster, however global variables can be better for memory management.