Scope of a variable in Python is the region or block of code where the variable is created.
Scope of a variable is the region of code where the variable is accessible.
Types of Scopes:
- Local Variable
- Global Variable
1. Local Scope
These are the variables that are created and accessible inside the function. These variables belong to local scope for the function inside which they are created.
def aFun(): var = "I m a local variable" print(var) aFun() print(var)
I m a local variable Traceback (most recent call last): File "f:/vscode/python/main.py", line 5, in <module> print(var) NameError: name 'var' is not defined
2. Global Scope
These are the variables that are created in the main body of the python code and not inside a function. These variables belong to the global scope.
These variables are available within any scope, local and global.
var = "I m a global variable" def aFun(): print(var) aFun() print(var)
I m a global variable I m a global variable
Function inside Function
If a function is inside another function then it can access the variable which is created inside the parent function but outside the child function.
def parentFun(): var = "I m a variable" print(var) def childFun(): print(var) childFun() parentFun()
I m a variable I m a variable
Same name with different scopes
There may be cases like when you have two variables of the same name but in different regions or scopes.
In that case, python assumes that these variables are totally different from each other and if you make changes in one variable then that change will not affect the other scope variable.
var = "I m outside function" def parentFun(): var = "I m inside function" print(var) parentFun() print(var)
I m inside function I m outside function
Python provides global keyword to create a variable that can be accessed globally.
We should use global keyword to create a global variable inside a function.
def myFunc(): global x x = "A global variable" print(x) myFunc() print(x)
A global variable A global variable
Use the global keyword to make changes or to use a global variable inside a function.
x = "A global variable" print(x) def myFunc(): global x x = "I m a variable" print(x) myFunc() print(x)
A global variable I m a variable I m a variable
Auto Login with Python
So today we will learn how to auto login in a tab using Python. So, let’s first see…
Adding three matrices in Python
In python, it’s very easy to deal with matrices due to its simple syntax and we create matrices…
Simple Music Player Using Python
Hello friends, do you know that there are many ways to make music players from simple to advanced…
Changing Screen Size: Tkinter
Hello guys welcome again to our site where you get amazing source code absolutely free with explanation So,…
GUI Age Calculator
Hello friends, today we will make an age calculator using python’s GUI library tkinter so, let’s start. Python…
Displaying Images in tkinter
Hello guys, as usual we are sharing one more source code using which you can display images inside…