Variable Scope

Variable Scope

The scope of a variable is where you can access it.
One way to think about the scope of a variable is where it lives and who can see it in a program.

Global Variable

A global variable is defined outside of functions and is visible to all functions that are defined after it.
scope.py
x = 10
def main():
print(x)
main()
What is the output of this program?

Local Variable

A local variable is defined within a function. Another way to think about it is, a variable that is declared inside a function, stays inside the function.
def main():
x = 10
print(x)
main()
What is the output of this program?

Local vs Global Variable

x = 10
def main():
x = 11
print(x)
main()
What is the output of this program?