Local and Global variable
All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable.
The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python.
- Local Variable
- Global Variable
- When we create a variable inside a function, it’s local by default.
- When we define a variable outside of a function, it’s global by default. You don’t have to use the global keyword.
- We use global keyword to read and write a global variable inside a function.
- Use of global keyword outside a function has no effect.