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.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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.
Python / Practical Example
count = 0
def add_one():
global count
count += 1
add_one()
print(count)Expected Output
1Step-by-Step Explanation
- count begins in global scope.
- global tells the function that assignment targets that global name.
- Use global state sparingly because it makes dependencies harder to track.
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.
Python / Practical Example
def outer():
count = 0
def inner():
nonlocal count
count += 1
inner()
return count
print(outer())Expected Output
1Step-by-Step Explanation
- count belongs to the enclosing function.
- nonlocal lets inner() rebind that enclosing name.
- outer() returns the changed value.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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.
Python / Practical Example
import sys
value = []
print(sys.getrefcount(value) >= 2)Expected Output
TrueStep-by-Step Explanation
- Python tracks references to objects.
- getrefcount() is an implementation-oriented inspection tool and includes a temporary reference from the call itself.
- Normally you should manage object lifetimes by writing clear code rather than manually counting references.
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.
Python / Practical Example
import sys
value = []
print(sys.getrefcount(value) >= 2)Expected Output
TrueStep-by-Step Explanation
- Python tracks references to objects.
- getrefcount() is an implementation-oriented inspection tool and includes a temporary reference from the call itself.
- Normally you should manage object lifetimes by writing clear code rather than manually counting references.
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.
Python / Practical Example
items = [1, 2]
items[0] = 9
print(items)Expected Output
[9, 2]Step-by-Step Explanation
- Lists are mutable.
- Assign a new value to an existing position.
- The same list object now contains changed data.
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.
Python / Practical Example
original = [[1], [2]]
shallow = original.copy()
shallow[0].append(9)
print(original)Expected Output
[[1, 9], [2]]Step-by-Step Explanation
- copy() makes a shallow copy of the outer container.
- Nested mutable objects can still be shared.
- Use deepcopy() only when a fully independent nested copy is actually needed.
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.
Python / Practical Example
x = 10
def f():
x = 20
return x
print(x, f())Expected Output
10 20Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
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
- Choose three topics from this chapter and re-type their examples without copying and pasting.
- For each example, change one input and predict the output first.
- Explain five technical terms from this chapter in your own beginner-friendly words.
- Create one small program that combines at least two chapter topics.
- 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.
- Choose three topics from this chapter.
- Write or adapt a small Python example using those topics.
- Predict the output before running the code.
- Test at least one different input.
- 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.