Python Programming - Variables - Discussion

Discussion Forum : Variables - General Questions (Q.No. 13)
13.
What is the difference between a global variable and a local variable?
Global variables have a shorter lifespan than local variables.
Global variables can only be accessed within a specific function, while local variables can be accessed anywhere in the code.
Global variables can be accessed anywhere in the code, while local variables can only be accessed within a specific function.
There is no difference between a global variable and a local variable.
Answer: Option
Explanation:

In Python, a global variable is a variable that is defined outside of any function and can be accessed from anywhere in the code. A local variable, on the other hand, is a variable that is defined within a function and can only be accessed within that function.

Global variables exist throughout the entire program's execution, while local variables are created when a function is called and are destroyed when the function completes. Therefore, global variables have a longer lifespan than local variables.

Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.