Scope of Variables in Python | Global and Local Variables in Python

Variables have no restrictions on the number of times they can be used in a program. However, the restriction is around where exactly in the program are they being used. Variables can only be used in the area where they are defined and hence scope of variables in Python is restricted to that area. Here area means the area of code. Python supports two scopes of a variable and they are discussed below.

Local Variables in Python

Prerequisite: A function is a piece of code or series of statements that performs a specific task in the entire program. Functions can be user-defined or inbuilt. We can define/declare variables in user-defined (Defined by us) functions.

By default, all the variables declared inside a function are considered as local variables in Python. This means the scope of these variables is limited to that function and they cannot be accessed or used outside that function. The below examples will help you get a better idea.

Example 1:

# This is a function used to print a.
# Function starts here
def f(): 
a = "Learning Python on FACE Prep"
print (a)
# Function ends here

f() #function is being called. This is happening outside the function
Output: Learning Python on FACE Prep

Example 2:

# This is a function used to print a.
# Function starts here
def f(): 
a = "Learning Python on FACE Prep"
print (a)
# Function ends here

f() #function is being called. This is happening outside the function
print (a)
Output: 
Learning Python on FACE Prep
Name error: Name 'a' is not defined.

Explanation: Here, since a is defined/declared within the function f(). When the function is called, a get’s printed. But when a is used outside the function, it throws an error as it is not accessible outside the function.

Haunting Question: How can I access this local variable ‘a’ outside its function. (You will know this soon)

Global Variables in Python

Global variables are variables which are accessible anywhere and everywhere in the program. This means a global variable can be used inside and outside a function as well. 

# This is a function used to print a.
# Function starts here
def f(): 
print (a)
# Function ends here

a = "Learning Python on FACE Prep"
f() #function is being called
Output: Learning Python on FACE Prep

Since you now know the scope of variables in Python, very likely that you might have these questions in mind.

Questions on Scope of Variables in Python

1) How to make a local variable global in Python?

Now, to answer the above question, one can declare variables with the keyword “global” to make the local variables globally accessible.

# This is a function used to print a.
# Function starts here
def f(): 
global a 
a = "Learning Python on FACE Prep"
print (a)
# Function ends here

f() #function is being called. This is happening outside the function
print (a)
Output: 
Learning Python on FACE Prep
Learning Python on FACE Prep

Explanation: Here the statement “global a” has been used to declare ‘a’ as a global variable and hence the print (a) statement outside the function doesn’t throw an error this time.

2) Can I give the same name to my local and global variables? 

Yes, you can name your local and global variables the same. Well, in that case, let’s see the output.

Case 1: When your local variable is not declared global

# This is a function used to print a.
# Function starts here
def f(): 
a = "I love Python"
print (a)
# Function ends here

a = "Learning Python on FACE Prep"
f() #function is being called
print (a)
Output:
I love Python
Learning Python on FACE Prep

Case 2: When your local variable is declared global

# This is a function used to print a.
# Function starts here
def f(): 
global a  #local variable is declared global

a = "I love Python"
print (a)
# Function ends here
Output:
I love Python

Explanation: Here, since your local variable is explicitly declared as a global variable, it overwrites all other variables with the same name.