Q. What is the difference between local variable and global variable? Also give a suitable Python code to illustrate both.


Answer =

The differences between a local variable and global variable have been given below.


Local Variable:-
It is a variable which is declared within a function or within a block.
It is accessible only within a function/block program.

Global Variable:-
It is a variable which is declared outside all the functions.
It is accessible throughout the program in which it is declared.


For example, in the following code x, xCubed are global variables and n and cn

def cube (n) :
    cn = n * n * n
    return cn
x = 10
xCubed = cube (x)
print (x, "cubed 15", xCubed)

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post