KnowledgeBoat Logo

Computer Science

What is the difference between local and global variable ?

Python Functions

5 Likes

Answer

Local variableGlobal variable
A local variable is a variable defined within a function.A global variable is a variable defined in the 'main' program.
They are only accessible within the block in which they are defined.They are accessible from anywhere within the program, including inside functions.
These variables have local scope.These variables have global scope.
The lifetime of a local variable is limited to the block of code in which it is defined. Once the execution exits that block, the local variable is destroyed, and its memory is released.Global variables persist throughout the entire execution of the program. They are created when the program starts and are only destroyed when the program terminates.

Answered By

1 Like


Related Questions