To fully understand how Python code executes, we need to visualize what happens in the global space, the call stack where call frames live, and the heap space where objects (folders) live.

Lessons - watch before lecture

  1. Memory Areas (5 minutes) Global space, call stack, and heap space
  2. Modules in Memory (5 minutes) What happens in memory when you import a module?
  3. Functions in Memory (6 minutes) The function definition goes into a folder, too.
  4. Global Variables (6 minutes) A function can access global variables but cannot (should not) modify their values.
  5. Optional: Objects Everywhere (7 minutes)

Lecture materials

Slides: individual slides for viewing, 6-up layout for printing
These slides have been updated after lecture to answer some students' questions during lecture. See slides 24 - 27. Slides 33 to 38 correspond to the pre-lecture videos and were not discussed during lecture.

Example: v1.py, v2.py, v3.py, shape.py
To download the above .py files, right-click (Windows) or command-click (Macs) on the filename and then select "Save link as".
This way you choose where the files will be saved, instead of having your computer save to the default folder (from which you will later have to move your files for organization).

Lecture Recording

Answers to the questions on slides 27, 31, and 39: See the lecture slides file above for the questions. After solving the problems yourself, you can check your answers here. Here is a short video explaining the answer to Question 2 (slide 31). Reminder: Although you can use Python Tutor to visualize code execution, you must also learn to draw the diagrams yourself in order to demonstrate that you truly understand the concept.