🎓 EASYTUTORGUIDE · Complete Python Course

Very beginner → professional Python · 60 chapters · 900 topics

EasyTutorGuide

Chapter 19: Scope, Namespaces and Memory

Complete Python lesson for very beginners. Technical words are explained in simple language, and every outline topic includes a practical example, expected output, steps, and practice.

Python CourseVery Beginner Friendly15 TopicsExamples + Output20 Q&A
Chapter 19 · 15 topics
100%

Chapter Overview

This Python tutorial chapter covers Scope, Namespaces and Memory through 15 connected topics. Work through the examples in order, check the expected output, and complete the practice after each topic.

  • 19.1 Local Scope
  • 19.2 Global Scope
  • 19.3 Enclosing Scope
  • 19.4 Built-in Scope
  • 19.5 LEGB Rule
  • Plus 10 additional Python topics in this chapter.

19.1 Local Scope

Local Scope is part of Scope, Namespaces and Memory. In simple language, it means the part of a program where a name is available.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Local Scope (the part of a program where a name is available).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Local Scope”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.2 Global Scope

Global Scope is part of Scope, Namespaces and Memory. In simple language, it means the part of a program where a name is available.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Global Scope (the part of a program where a name is available).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Global Scope”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.3 Enclosing Scope

Enclosing Scope is part of Scope, Namespaces and Memory. In simple language, it means the part of a program where a name is available.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Enclosing Scope (the part of a program where a name is available).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Enclosing Scope”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.4 Built-in Scope

Built-in Scope is part of Scope, Namespaces and Memory. In simple language, it means the part of a program where a name is available.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Built-in Scope (the part of a program where a name is available).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Built-in Scope”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.5 LEGB Rule

LEGB Rule is part of Scope, Namespaces and Memory. In simple language, it means a Python idea used while learning scope, namespaces and memory.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: LEGB Rule (a Python idea used while learning scope, namespaces and memory).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “LEGB Rule”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.6 global

global is part of Scope, Namespaces and Memory. In simple language, it means a Python idea used while learning scope, namespaces and memory.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: global (a Python idea used while learning scope, namespaces and memory).

Python / Practical Example

count = 0
def add_one():
    global count
    count += 1
add_one()
print(count)

Expected Output

1

Step-by-Step Explanation

  1. count begins in global scope.
  2. global tells the function that assignment targets that global name.
  3. Use global state sparingly because it makes dependencies harder to track.
Practice: Re-type the example for “global”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.7 nonlocal

nonlocal is part of Scope, Namespaces and Memory. In simple language, it means a Python idea used while learning scope, namespaces and memory.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: nonlocal (a Python idea used while learning scope, namespaces and memory).

Python / Practical Example

def outer():
    count = 0
    def inner():
        nonlocal count
        count += 1
    inner()
    return count
print(outer())

Expected Output

1

Step-by-Step Explanation

  1. count belongs to the enclosing function.
  2. nonlocal lets inner() rebind that enclosing name.
  3. outer() returns the changed value.
Practice: Re-type the example for “nonlocal”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.8 Namespaces

Namespaces is part of Scope, Namespaces and Memory. In simple language, it means a mapping that connects names to objects.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Namespaces (a mapping that connects names to objects).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Namespaces”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.9 Name Resolution

Name Resolution is part of Scope, Namespaces and Memory. In simple language, it means a Python idea used while learning scope, namespaces and memory.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Name Resolution (a Python idea used while learning scope, namespaces and memory).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Name Resolution”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.10 Object References

Object References is part of Scope, Namespaces and Memory. In simple language, it means a value with data and behavior.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Object References (a value with data and behavior).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Object References”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.11 Reference Counting

Reference Counting is part of Scope, Namespaces and Memory. In simple language, it means a Python idea used while learning scope, namespaces and memory.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Reference Counting (a Python idea used while learning scope, namespaces and memory).

Python / Practical Example

import sys
value = []
print(sys.getrefcount(value) >= 2)

Expected Output

True

Step-by-Step Explanation

  1. Python tracks references to objects.
  2. getrefcount() is an implementation-oriented inspection tool and includes a temporary reference from the call itself.
  3. Normally you should manage object lifetimes by writing clear code rather than manually counting references.
Practice: Re-type the example for “Reference Counting”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.12 Garbage Collection

Garbage Collection is part of Scope, Namespaces and Memory. In simple language, it means a Python idea used while learning scope, namespaces and memory.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Garbage Collection (a Python idea used while learning scope, namespaces and memory).

Python / Practical Example

import sys
value = []
print(sys.getrefcount(value) >= 2)

Expected Output

True

Step-by-Step Explanation

  1. Python tracks references to objects.
  2. getrefcount() is an implementation-oriented inspection tool and includes a temporary reference from the call itself.
  3. Normally you should manage object lifetimes by writing clear code rather than manually counting references.
Practice: Re-type the example for “Garbage Collection”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.13 Mutable Arguments

Mutable Arguments is part of Scope, Namespaces and Memory. In simple language, it means a value supplied when a function is called.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Mutable Arguments (a value supplied when a function is called).

Python / Practical Example

items = [1, 2]
items[0] = 9
print(items)

Expected Output

[9, 2]

Step-by-Step Explanation

  1. Lists are mutable.
  2. Assign a new value to an existing position.
  3. The same list object now contains changed data.
Practice: Re-type the example for “Mutable Arguments”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.14 Copying Objects

Copying Objects is part of Scope, Namespaces and Memory. In simple language, it means a value with data and behavior.

It is one building block of scope, namespaces and memory and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Copying Objects (a value with data and behavior).

Python / Practical Example

original = [[1], [2]]
shallow = original.copy()
shallow[0].append(9)
print(original)

Expected Output

[[1, 9], [2]]

Step-by-Step Explanation

  1. copy() makes a shallow copy of the outer container.
  2. Nested mutable objects can still be shared.
  3. Use deepcopy() only when a fully independent nested copy is actually needed.
Practice: Re-type the example for “Copying Objects”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

19.15 Memory Best Practices

Memory Best Practices is part of Scope, Namespaces and Memory. In simple language, it means a Python idea used while learning scope, namespaces and memory.

It helps you understand where time or memory is being used so you can improve real bottlenecks instead of guessing. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.

Technical words in simple language: Memory Best Practices (a Python idea used while learning scope, namespaces and memory).

Python / Practical Example

x = 10
def f():
    x = 20
    return x
print(x, f())

Expected Output

10 20

Step-by-Step Explanation

  1. Read the example from top to bottom.
  2. Identify the value, object, or operation related to this topic.
  3. Change one small input and predict the result before running it.
Practice: Re-type the example for “Memory Best Practices”, change one safe input, predict the result before running it, and explain in one sentence why the result changed.

Common Beginner Mistakes

  • Copying code without predicting what each line does.
  • Ignoring the first useful error message or traceback location.
  • Mixing tabs/spaces or changing indentation accidentally.
  • Using data of the wrong type for an operation.
  • Trying to learn many advanced variations before mastering one small working example.

Chapter Practice

  1. Choose three topics from this chapter and re-type their examples without copying and pasting.
  2. For each example, change one input and predict the output first.
  3. Explain five technical terms from this chapter in your own beginner-friendly words.
  4. Create one small program that combines at least two chapter topics.
  5. Keep notes about errors you made and what fixed them.

Mini Project / Challenge

Create a small Python exercise that combines at least three ideas from Scope, Namespaces and Memory. Start with a tiny working version, test it, then improve it one step at a time.

  1. Choose three topics from this chapter.
  2. Write or adapt a small Python example using those topics.
  3. Predict the output before running the code.
  4. Test at least one different input.
  5. Write two sentences explaining what the program does and what you learned.

20 Questions & Answers

1. What is the main goal of Chapter 19?

The goal is to understand scope, namespaces and memory through small explanations, examples, and practice.

2. Should I memorize every command or method?

No. Understand the pattern, practice the common form, and learn how to read documentation when you need exact details.

3. Why are the examples small?

Small examples isolate one idea at a time, which makes errors easier to understand and fix.

4. What should I do when an example gives an error?

Read the last part of the traceback, check spelling and indentation, confirm the required package or file exists, and compare the input types with what the operation expects.

5. Why should I predict output before running code?

Prediction forces you to reason about the program instead of only copying it.

6. What should I remember about Local Scope?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

7. What should I remember about Global Scope?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

8. What should I remember about Enclosing Scope?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

9. What should I remember about Built-in Scope?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

10. What should I remember about LEGB Rule?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

11. What should I remember about global?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

12. What should I remember about nonlocal?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

13. What should I remember about Namespaces?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

14. What should I remember about Name Resolution?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

15. What should I remember about Object References?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

16. What should I remember about Reference Counting?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

17. What should I remember about Garbage Collection?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

18. What should I remember about Mutable Arguments?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

19. What should I remember about Copying Objects?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.

20. What should I remember about Memory Best Practices?

Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.