Python – Scope of Variables

Python – Scope of Variables: A variable may only be accessible inside the scope of the variable, which is the region in which it is declared. There are primarily two types of variable scopes in Python:

  • Local variable
  • Global variable

Local Variable

A Python – Scope of Variables defined within a function is referred to as a local variable and has a local scope. Only within that function may a local variable be utilised.

Example

A local variable named MyStr is created and utilised inside a function to print it in the example below.

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

MyPrint()

The output of the above code will be:

Hello World!

A local variable in a nested function

A local variable can’t be utilised outside of the function where it’s defined, but it may be used within that function.

Example

A local variable named MyStr is created inside the function MyFunction in the example below. In addition, a function named MyPrint() is defined within MyFunction. Inside MyPrint, the local variable MyStr will be accessible ().

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

MyFunction()

The output of the above code will be:

Hello World!

Global Variable

A global variable is one that is generated outside of a function and has global scope. A global variable can be referenced both within and outside of the function.

Example

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

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

MyPrint()

The output of the above code will be:

Hello World!

If you create a variable with the same name inside the function, it will be a local variable that can only be used within the function. Any action on a local variable has no effect on the global variable.

Example

A global variable named MyStr is created in the main body of the programme in the example below. Within the function MyPrint, a local variable with the same name is likewise created (). Because this local variable can only be used within the function MyPrint(), it is utilised by the function when it is invoked in the programme. Furthermore, any operation on the local variable has no effect on the global variable; as a result, when the variable MyStr is written outside the function, the variable takes just the global value, 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

A variable created inside a function in Python has local scope, meaning it can only be used within that function. The Python global keyword is used to define a variable with global scope inside a function. A global variable can be referenced both within and outside of the function.

Example

The global keyword is used in the example below to create a variable called MyStr with global scope within the function 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 established outside of a function can be altered inside the function by using the global keyword to refer to the variable.

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

To declare a variable that is not local, use the Python nonlocal keyword. In nested functions, where the variable does not belong to the inner function, a nonlocal variable is commonly used.

Example

The variable MyString is declared as a nonlocal variable inside the function InnerFunction in the example below. Because variable MyString has a nonlocal scope, its value is updated to Hello Python! within the inner function, and when the function OuterFunction is invoked in the programme, the modified value of MyString is printed.

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

The variable MyString is not specified as a nonlocal variable in the example below. As a result, the value of MyString does not change, and it 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!

Also Read: Java – Label Statement

Leave a Reply

Your email address will not be published.