AlphaCodingSkills

Python - Scope of Variables


Advertisements

Previous Page Next Page

A variable can only be accessed in the area in which it is defined, which is called scope of the variable.

Local Scope

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 below example, a local variable called MyString is created and used inside the function called MyPrint() to print it.

def MyPrint():
  MyString = "Hello World!."
  print(MyString)

MyPrint()

Output

Hello World!.

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 below example, a local variable called MyString is created inside the function called MyFunction. Along with this, a function called MyPrint() is also defined inside MyFunction. The local variable MyString is available inside MyPrint().

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

MyFunction()

Output

Hello World!.

Global Scope

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 below example, a global variable called MyString is created and used inside the function called MyPrint() to print the global variable.

MyString = "Hello World!."
def MyPrint():
  print(MyString)

MyPrint()

Output

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 below example, a global variable called MyString 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 MyString in printed outside the function, the variable takes global value only which is unaffected by the local variable.

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

MyPrint()
print(MyString)

Output

Hello Python

Hello World!.

Variable Name

There are some reserved keywords in Python which cannot be used as variable name. Along with this, rules for creating Python variable name are listed below:

  • It must start with a letter or the underscore character
  • It cannot start with a number.
  • It can only contains alpha-numeric characters and underscores (A-Z, a-z, 0-9, and _ ).

Please note that Python is a case-sensitive language. Hence, variables in Python are also case-sensitive.

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 below example, the global keyword is used to create a variable called MyString with global scope inside the function called MyFunction.

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

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

Output

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.

MyString = "Java"

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

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

Output

Learning Python is fun.

Previous Page Next Page