Learn what variable scopes are all about and get familiar with the ‘LEGB’ rule. You will also deal with scenarios where you’ll get to see the global and nonlocal keywords in action.

If you’re familiar with Python or any other programming language, you’ll undoubtedly know that variables need to be defined before they can be used in your program. In this tutorial, you will start with variable initialization. Next, you will get familiar with the boundary of variables within a program - its “scope”. You will learn about the four different scopes with the help of examples: local, enclosing, global, and built-in. These scopes together form the basis for the LEGB rule used by the Python interpreter when working with variables. Then after, you will revisit some examples with additional complexity to open the way for the global keyword followed by the nonlocal keyword.

#python #web-development #machine-learning

Scope of Variables in Python
2.15 GEEK