Variable Scope
Variable Scope
The scope of a variable is where you can access it.
Global Variable
A global variable is defined outside of functions and is visible to all functions that are defined after it.
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?
Last updated
Was this helpful?