Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Functions in python
  5. Global and local variables

Global and local variables

Global variables – The variable whose value is accessible from any block is called Global variable.

The Scope of the global variable is the entire program. Whatever the variables we are declaring upon the user defined function is known as the global variable.

e.g.,

a= 20
def funct():
    global a
    print(a)

funct()
print(a)
Output- 20  
        20

Local variable– The variable which is defined and accessed in a particular block and cannot be accessed outside that block is known as Local variable.

The scope of the local variable is within the function where the variable is declared. Whatever the variable is declared inside the user defined function is known as the local variable.

e.g.,

def difference(x, y):
    difference = x - y
    return difference

print(difference(10,3))
Output- 7

This is all about global and local variable in python.

Was this article helpful to you? Yes No

How can we help?