Scope and Lifetime of Variables

 
The scope of a variable refers to the domain of a program wherever it is declared. A function's arguments and variables are not accessible outside the defined function. As a result, they only have a local domain.

The period of a variable's existence in RAM is referred to as its lifetime. Variables within a function have the same lifespan as the function itself.


Code:

#defining a function to print a number.  
def number( ):  
    num = 30  
    print( "Value of num inside the function: ", num)  
  
num = 20  
number()  
print( "Value of num outside the function:", num)  


Output:
Value of num inside the function:  30
Value of num outside the function: 20