0Pricing
Learn AI with Python · Lesson

Scope and Lifetime of Variables

Understand how variable scope impacts your code and prevents bugs.

Scope and Lifetime of Variables is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Scope and Lifetime of Variables

Scope and lifetime define where and how long a variable exists in your program. Understanding these concepts is crucial for writing efficient and error-free code.

In this lesson, you’ll learn about global and local variables, as well as the concept of variable lifetime.

Scope and Lifetime of Variables — illustration 1

2

What is Scope?

Scope refers to the region of a program where a variable is accessible. Variables can have either global or local scope:

  • Global Scope: The variable is accessible throughout the entire program.
  • Local Scope: The variable is accessible only within the function where it is defined.

3

4

Global Variables

A global variable is declared outside of any function and can be accessed from anywhere in the program:

# Example of a global variable
x = 10  # Global variable
def print_x():
    print(x)

print_x()

5

Local Variables

A local variable is declared inside a function and is only accessible within that function:

# Example of a local variable
def calculate_square():
    x = 5  # Local variable
    return x * x

print(calculate_square())

6

Variable Lifetime

The lifetime of a variable refers to how long the variable exists in memory:

  • Global Variables: Exist as long as the program runs.
  • Local Variables: Exist only during the execution of the function.

7

Modifying Global Variables

To modify a global variable inside a function, use the global keyword:

# Modifying a global variable
x = 10

def update_x():
    global x
    x = 20

update_x()
print(x)  # Outputs: 20

8

Practical Example

Here’s an example that demonstrates global and local variables:

# Example of global and local variables
x = 100

def print_variables():
    y = 50  # Local variable
    print("Global variable x:", x)
    print("Local variable y:", y)

print_variables()

9

10

Common Mistakes with Scope

Here are some common mistakes to avoid:

  • Trying to access a local variable outside its function.
  • Modifying a global variable without using the global keyword.
  • Overwriting a global variable with a local variable of the same name.

11

What Did We Learn?

In this lesson, you learned:

  • The difference between global and local variables.
  • How to use the global keyword to modify global variables.
  • The importance of variable scope and lifetime.

Great job! Let’s move to the next lesson.

Scope and Lifetime of Variables — illustration 11

Frequently asked questions

Is the “Scope and Lifetime of Variables” lesson free?

Yes — the full text of “Scope and Lifetime of Variables” is free to read here on the web, and the Learn AI with Python course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Scope and Lifetime of Variables”?

Understand how variable scope impacts your code and prevents bugs. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “Scope and Lifetime of Variables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What is a Function?
  2. Return Values
  3. Scope and Lifetime of Variables
  4. Built-in Functions vs. User-Defined Functions
  5. Importing Modules
← Back to Learn AI with Python