Skip to content

Visualize Python code and understand recursion

Reviewed

Short answer

Python Studio's visualizer records a Python run so you can move through execution events, inspect variable snapshots, and select call-stack frames. Use Visualize instead of Run Python for a recording. Small examples with a clear stopping condition are easiest to understand.

Open this example in the editor

Opens as a new program; running code requires JavaScript.

How do I visualize a Python program?

A visualizer helps explain why code produced a result, not just what it printed. Open a short program, prepare any input, then select Visualize. The Visualizer console records line, call, and return events up to its safety limits.

Move through the recorded events with the visualizer controls. The selected event shows the corresponding variables and source line. Moving backward reads an earlier snapshot: it does not reverse the program's real side effects or run code backward.

  1. 01Open the factorial example as a new program.
  2. 02Select Visualize and wait for recording to finish.
  3. 03Move through events to watch n change as functions call themselves.
  4. 04Select a caller in the call stack to inspect that invocation's locals and source location.

What happens in a recursive factorial function?

Recursion means a function calls itself with a smaller or simpler problem. Factorial multiplies a positive integer by the factorial of the preceding integer. The base case returns 1 for n equal to 0 or 1.

For factorial(4), calls descend through n values 4, 3, 2, and 1. Then the return values combine in the opposite direction: 1, 2, 6, and finally 24. This is an explanation of the algorithm; return-value previews are not a promised visualizer feature.

Python
def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)

print(factorial(4))
Output
24

Why does each recursive call have its own variables?

Each function invocation has a separate stack frame. Several frames can therefore contain a variable named n with different values at the same moment. Selecting a suspended caller shows its local values rather than the newest call's values.

A source-line highlight refers to the selected frame's location, which can differ from the innermost running function. Read both the selected frame and its variables before interpreting a snapshot.

Compare n in the first and last calls of factorial. The names match, but they belong to different invocations. A return removes the innermost frame and resumes its caller.

What if recursion never reaches a base case?

Every recursive path needs to reach a stopping condition. If n grows instead of shrinking, or the base case excludes a possible value, calls may continue until Python raises RecursionError or an execution limit stops the run.

Test a small input before trying larger values. The factorial example assumes a non-negative integer; its simple n <= 1 condition is not input validation for all possible mathematical inputs.

Check the error shown by the recording rather than assuming the last captured event represents successful completion. Fix the stopping condition, then record again.

What are the recording limits?

Python Studio captures at most 500 trace events and uses bounded snapshots to control memory use. Variables or large values may be abbreviated or omitted. A missing preview does not prove a variable was deleted.

Custom objects are shown conservatively so inspecting them does not call arbitrary user-defined representation methods. Snapshots are a learning aid, not a complete object or memory debugger.

Editing code or input closes the old visualization because it no longer matches the source. Run Visualize again after changes. Use short loops and modest recursion depth; tracing large programs can reach the event limit before the program finishes.

Official sources