Different examples of local and global scope.
Whenever we access a variable from within a program or function , python follows name resolution rule also known as LEGB rule. That is for every name reference python does the following to resolve it
It checks within its Local environment (LEGB) if it has a variable with the same name ; if yes python uses its value. If not it moves to step (ii)
Python now checks in Enclosing Environment (LEGB) if their is a variable with same name uses it otherwise moves to step (iii)
Python now checks the global environment (LEGB) whether their is variable with same name or not, if yes python uses it , if not then moves to step (iv)
Finally python checks it in Built in environment (LEGB) that contains built in functions and variables of Python.
Note: If python doesnt found in any of these LEGB rule then Python reports and error.
variable_name not defined.
#LEGB #globalscope #localscope