Skip to main content
In this lesson, we explore how variable scoping works in Python. By understanding the difference between local and global variables, you can write clearer and more effective code. This guide covers how variables declared inside a function are confined to that function, and when the global keyword is required.

Local Variables

Variables defined inside a function are local to that function. They cannot be accessed outside of the function’s scope. Consider the following example:
Here, the variable result is local to the input_number function. Trying to reference result elsewhere in your code will lead to an error because its scope is strictly within the function.

Global Variables and Shadowing

It is possible to use variables that are declared outside a function (global variables) within the function body. However, if a variable with the same name exists both globally and locally, the local variable will take precedence—a concept known as shadowing.

Example of Variable Shadowing

In this example, the function uses the local variable num (which is 50) instead of the global num.

Example Using Global Variable

If no local variable is defined, the function will use the global variable:
Python first looks in the function’s local scope; if it doesn’t find the variable there, it then checks the global scope.

Using the Global Keyword

By default, variables declared inside a function are not accessible outside of it. To modify a global variable within a function, you must declare it as global using the global keyword.
Declaring a variable as global allows you to modify it inside a function, making it accessible in the global scope after the function call.
Here’s an example:
After calling input_number(), you can access the variable own_num from the global scope:

Practice Exercises

Now that you understand local and global variable scopes in Python, try practicing these concepts with some hands-on exercises. Experiment with variable shadowing and the global keyword to see how scope affects your code. For further learning, check out the following: That’s it for this lesson. Happy coding!

Watch Video