Scope of Variables in Python

Scope of Variables in Python

In Python, scope of variables is the area in which they are defined. They are accessible in that defined area only which is also known as their scope. There are majorly two types of variable scope in Python and they are:

  • Local Variables
  • Global Variables

 

Local Variables

Variables created inside a function are called local variables and have a local scope which means that they can only be accessed inside the function they are defined in and not outside it.

Example of Local Scope of Variables:

# Python program to exemplify local variables

def Print():
  var_str = "StudyExperts!"
  print(var_str)

Print()

Output:

StudyExperts!

Local Variable in a Nested Function

A function can be defined inside another function in Python which makes it a nested function. A variable created in the outer function can used inside the inner function, but a variable created inside the inner function cannot be used in the outer function or anywhere else in the program.

Example:

 

# Python program to exemplify local variables in a nested function

def Function():
  var_str = "StudyExperts!"
  def Print():
    print(var_str)
  Print()

Function()

Output:

StudyExperts!

 

Global Variables

Variables that are created outside a function in Python are called global variables and have a global scope which means that they can be accessed from anywhere in the program; even from inside a function.

Example of Global Scope of Variables:

# Python program to exemplify global variables

var_Str = "StudyExperts!"
def Print():
  print(var_Str)

Print()

Output:

StudyExperts!

Example:

If a global and a local variables have same names, then when used inside the function, values of local variable is used and when used outside the function, value of global variable is used.

# Python program to exemplify global variables

var_str = "StudyExperts!"
def Print():
  var_str = "Python Programming Tutorial!"
  print(var_str)

Print()
print(var_str)

Output:

Python Programming Tutorial!
StudyExperts!

 

 

Python global Keyword

If a global variable and a local variable have same names, then when used inside the function, value of local variable is used. So, to use the value of global variable, global keyword is used. It can also be used to create a global variable inside a function. It is done be preceding variable name with the keyword global.

Example:

 

# Python program to exemplify global keyword

def Function():
  global var_str
  var_str = "Python"

Function()
print(var_str ,"Tutorials!")

Output:

Python Tutorials!

 

 

Python nonlocal Keyword

It is used to declare a variable which is not local and is generally used inside a nested function stating that the nonlocal variable does not belong to the inner function.

Example:

 

# Python program to exemplify nonlocal keyword

def OuterFunction():
  var_str = 'StudyExperts!'
  def InnerFunction():
    nonlocal var_str
    var_str = 'Python Tutorials!'
  InnerFunction()
  print(var_str)

OuterFunction()

Output:

Python Tutorials!

 

Share this post

Leave a Reply

Your email address will not be published.