Global scope of variables

Lesson Details:
June 29, 2020


I: Introduction

A: Learning basics of programming is fun and easy task. It’s an interesting way to make computers do your bidding. Programming is a process of writing instructions for the computer to execute, in the form of source code that can be run through a compiler or interpreter. Computer languages are used to create programs that tell the computer what to do, but there are other languages that are used to communicate with other devices, such as printers and modems. The most common language used today is the English language. Computer languages use words and symbols to represent actions or operations. The language is not unlike human languages in many ways, but unlike human languages, computer languages don’t use natural languages to describe concepts.

II: Body

A: Global scope of variables

The scope of a variable is the area of code in which it is defined. Local variables are only available within the block of code in which they are defined. Global variables are available anywhere in the program after they are declared. If you have two blocks of code that each define a variable named “x” then things can get confusing, because there could be two different variables, or one variable could be accessible from both blocks. Defining your variables in the global scope makes them easier to find, because you will know where they can be found throughout the entire program. Variables defined within functions are local variables by default. You can make them global by using the global keyword when defining them. There is also a nonlocal keyword that can be used in Python 3.x when defining variables in function blocks.

III: Conclusion

loader
Course content