DEV Community

Cover image for What are local variables and global variables in Python?
narendra8989
narendra8989

Posted on

What are local variables and global variables in Python?

Global Variables:

Variables declared outside a function or in global space are called global variables. These variables can be accessed by any function in the program.

Local Variables:

Any variable declared inside a function is known as a local variable. This variable is present in the local space and not in the global space.

Example:

1
2
3
4
5
6
a=2
def add():
b=3
c=a+b
print(c)
add()
Output: 5

When you try to access the local variable outside the function add(), it will throw an error.

Meet the Experts For better Guidence : https://nareshit.com/python-online-training/

Top comments (0)