Introduction to Python Variable Scope
In the Python programming language, a variable’s scope is that piece of code where that variable is visible or accessible. To be more precise, Not all variables are accessible from each and every part of a program. Moreover, sometimes the scope is duration defined as well. A part of the program where the variable is accessible is defined as the python’s scope.
Based on the scope itself, variables are clustered in numerous categories mentioned below:
- Local Variables
- Enclosed Variable
- Global Variables
- Built-in Variables
The level of scope increases in the same order from the local variable having the least scope to the Built-in variable having the widest scope within a python program.
How do we define a variable in Python?
Suppose we define a variable x and assign it a value 10, then this is how it’s done:
x = 10
Let’s try to print this variable using the print function:
print(x)
Output:
We can also define multiple variables within a single line as well.
Example
Python program to define multiple variables at once
Code:
a,b,d = 1,2,3
print(a)
print(b)
print(d)
Output:
Types of Python Variable Scope
Let’s discuss these in detail with some examples:
1. Local Variables
A local variable can be reached only within the scope where it’s defined. For Example, If we have defined a variable in a function. Then that variable will be accessible only in that function. The other piece of code may or may not access that function, depending upon the scope of that function.
Let’s take up an example in order to understand this in detail:
Example
Python Program to understand the usage of local variables
Code:
def fun():
s = "This is a local variable"
print(s)
fun()
Output:
How do local variables behave and work?
- In this program, we have defined a function named fun() wherein a variable named ‘s’ having the value as “This is a local variable”. is defined.
- It’s then followed by a print statement within the function itself.
- If we call the function, the control is transferred to the function where the variable named ‘s’ gets initialized and printed.
- As soon as the function exits and passes the control back to the main program, the variable ‘s’ is no longer in scope.
This is how a local variable behaves, and is scope lies in local to the piece of code where it’s defined.
What will happen if we try to print the variable ‘s’ outside of the function?
Let’s try the same.
Example
Python Program to understand the usage of local variables
Code:
def fun():
s = "This is a local variable"
print(s)
print(S)
This will throw an error as the print statement outside the function fun() can not access the variable ‘s’ whose scope is solely local to the function fun().
The error will be:
2. Global Variables
It refers to the variables declared outside a function but can be accessed within the function as well, providing it with a global nature of scope.
Let’s take up an example in order to understand this in detail:
Example
Python Program to understand the usage of global variables
Code:
def fun():
s = "This is a local variable"
print (s)
#this is the global scope
s = "This is global scope"
fun()
print (s)
Output:
How do global variables behave and work?
- In this program, we have defined a function named fun() wherein a variable named ‘s’ having the value as “This is a local variable”.
- It’s then followed by a print statement within the function itself.
- If we call the function, the control is transferred to the function where the variable named ‘s’ gets initialized and printed.
- Outside the function, we have defined another variable with the same name: ‘ s.’
What will get printed if we print the variable ‘s’ inside the function and what if the same is printed outside the function fun()?
- The scope of the variable again defines this.
- Yes, the local variable overrides the global variable’s scope, so if we print s inside the function, the value of ‘s’ local to that function will get printed.
- Whereas outside the function, the global variable’s value will get printed if we print ‘s’ outside the function.
This is how a global variable behaves and is overridden if any local variable is defined with the same name.
3. Enclosed Variable
In a nutshell, If the scope of a variable is neither local nor global, then it ends up having an enclosed scope.
4. Built-in Variables
It’s the widest level of scope that comes into the picture. It includes all the variables that get loaded when the python shell starts. For Example, We do not need to import functions such as print(). These variable does not require to be declared explicitly as they have already been defined within the programming language.
Recommended Articles
This is a guide to Python Variable Scope. Here we discuss how we define a variable in Python and the types of Variable Scope and respective examples. You can also go through our other suggested articles to learn more –
40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses