USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Scope of Variables (Local vs Global)

Lesson 21/35 | Study Time: 60 Min

When you create a variable in Python, it does not automatically exist everywhere in your program. Where a variable can be accessed and used depends on where it was created, this concept is called scope.

Understanding scope is essential for writing predictable, bug-free code. Without it, variables from different parts of your program can unexpectedly interfere with each other, causing errors that are difficult to trace, a particularly important concern in larger AI projects.

What is Scope?

Scope refers to the region of a program where a variable is recognized and accessible. Python determines this at the time the variable is created.

There are two primary scopes to understand:

Local Variables

A local variable is created inside a function and exists only within that function. Once the function finishes executing, the variable is gone.


result only exists inside calculate(). Trying to access it outside causes an error.

Another example:

Both functions have their own message variable — they are completely independent of each other.

Global Variables

A global variable is defined outside all functions and can be read from anywhere in the program, including inside functions.

Global variables are useful for values that need to be shared across multiple functions — such as configuration settings or constants.

Modifying a Global Variable Inside a Function

By default, you cannot modify a global variable from inside a function. Python treats any assignment inside a function as creating a new local variable.

To modify a global variable inside a function, you must explicitly declare it using the global keyword.

The global Keyword

The global keyword tells Python that a variable inside a function refers to the global version, not a new local one.


Best Practice: Use global sparingly. Overusing it makes code harder to debug and maintain. A better approach is to pass values as parameters and return results.

Local vs Global — When Names Conflict

When a local variable shares the same name as a global variable, the local one takes priority inside the function. The global remains unchanged.

The global name is never touched — the function works with its own local copy.

The LEGB Rule — How Python Finds Variables

Python searches for a variable in a specific order known as the LEGB rule:


Python moves through each level in order and uses the first match it finds.



Best Practices for Managing Scope

Relying too heavily on global variables leads to code that is difficult to trace and maintain. These habits keep your code clean:

1. Pass data as parameters rather than using global variables.

2. Return results from functions instead of modifying globals.

3. Use global only for true constants — values that never change, like MAX_LIMIT = 100.

4. Keep functions self-contained — a function should work with its own data.




Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.