Python Tutorial Python Advanced Python Resources Python Libraries
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Python - Scope of Variables



A variable can only be accessed in the area in which it is defined, which is called scope of the variable. In Python, there are mainly two types of variable scopes:

  • Local variable
  • Global variable

Local Variable

If a variable is created inside a function, it is called local variable and has local scope. A local variable can only be used inside that function.

Example

In the example below, a local variable called MyStr is created and used inside a function to print it.

def MyPrint():
  MyStr = "Hello World!"
  print(MyStr)

MyPrint()

The output of the above code will be:

Hello World!

Local variable in a nested function

A local variable can not be used outside the function in which it is defined but it can be used in any function inside that function.

Example

In the example below, a local variable called MyStr is created inside the function called MyFunction. Along with this, a function called MyPrint() is also defined inside MyFunction. The local variable MyStr will be available inside MyPrint().

def MyFunction():
  MyStr = "Hello World!"
  def MyPrint():
    print(MyStr)
  MyPrint()

MyFunction()

The output of the above code will be:

Hello World!

Global Variable

If a variable is created outside a function, it is called global variable and has global scope. A global variable can be used anywhere, inside the function and outside the function.

Example

In the example below, a global variable called MyStr is created and used inside the function called MyPrint() to print the global variable.

MyStr = "Hello World!"
def MyPrint():
  print(MyStr)

MyPrint()

The output of the above code will be:

Hello World!

If a variable with same name is created inside the function, it will be a local variable and can be used inside the function only. Any operation performed on local variable will not change the global variable.

Example

In the example below, a global variable called MyStr is created in the main body of the program. A local variable with the same name is also created inside the function called MyPrint(). This local variable can only be used inside the function MyPrint(), therefore the function uses the local variable when it is called in the program. Along with this, any operation performed on the local variable will not change the global variable, therefore when the variable MyStr in printed outside the function, the variable takes global value only which is unaffected by the local variable.

MyStr = "Hello World!"
def MyPrint():
  MyStr = "Hello Python"
  print(MyStr)

MyPrint()
print(MyStr)

The output of the above code will be:

Hello Python
Hello World!

Python global keyword

In Python, a variable created inside a function has local scope and can only be used inside that function. To create a variable with global scope inside a function, Python global keyword is used. A global variable can be used anywhere, inside the function and outside the function.

Example

In the example below, the global keyword is used to create a variable called MyStr with global scope inside the function called MyFunction.

def MyFunction():
  global MyStr
  MyStr = "Python"

MyFunction()
print("Learning", MyStr ,"is fun.")

The output of the above code will be:

Learning Python is fun.

Example

The value of a global variable, which is created outside of a function, can be changed inside the function by referring to the variable using the global keyword.

MyStr = "Java"

def MyFunction():
  global MyStr
  MyStr = "Python"

MyFunction()
print("Learning", MyStr ,"is fun.")

The output of the above code will be:

Learning Python is fun.

Python nonlocal keyword

The Python nonlocal keyword is used to declare a variable which is not local. A nonlocal variable is generally used inside nested functions where the variable does not belong to the inner function.

Example

In the example below, the variable called MyString is declared as nonlocal variable inside the function called InnerFunction. Due to nonlocal scope of variable MyString, its value gets updated to Hello Python! inside inner function and when the function OuterFunction is called in the program, it prints the updated value of MyString.

def OuterFunction():
  MyString = 'Hello World!'
  def InnerFunction():
    nonlocal MyString
    MyString = 'Hello Python!'
  InnerFunction()
  print(MyString)

OuterFunction()

The output of the above code will be:

Hello Python!

Example

In the example below, the variable called MyString is not declared as nonlocal variable. Due to this, the value of MyString is not updated and remains Hello World!.

def OuterFunction():
  MyString = 'Hello World!'
  def InnerFunction():
    MyString = 'Hello Python!'
  InnerFunction()
  print(MyString)

OuterFunction()

The output of the above code will be:

Hello World!

5