Variable Scope & Memory Visualizer

Created by Sam Li

Code Execution

1. global_var = "Earth"
2.
3. def explore():
4. local_var = "Mars"
5. print(global_var)
6. print(local_var)
7.
8. explore()
9. print(global_var)
10. print(local_var)
Click 'Next' to start execution.

Terminal Output

> System Ready...

Memory Visualizer

Global Scope (Heap/Data Segment)

Theory & Concept

Global Variables

Declared outside any function. They live in the "outer box" (Global Scope / Heap / Data Segment) and can be accessed by ANY code, inside or outside functions. They exist until the program ends.

Example: A player's total score in a game that needs to be updated from multiple different levels.

Local Variables

Declared inside a function. They live in an "inner box" (Local Scope / Stack). They are created when the function starts and DESTROYED (memory freed) when the function ends. Outside code cannot see them.

Example: A temporary counter used inside a specific loop or calculation that isn't needed anywhere else.