What are local variables and global variables in Python?

A local variable is a variable that is defined within a specific function and is only accessible within that function. It cannot be accessed by other functions within the program.

In contrast, a global variable is a variable that is declared outside of any function, allowing it to be accessed by all functions in the program

g=4 #global variable def func_multiply(): l=5 #local variable m=g*l return m func_multiply()

Output: 20

If you attempt to access the local variable outside the func_multiply function, you will encounter an error.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *