Functions, Tuples, and Lists Lab
Algorithms, Programming, and Data Representation · Module 92313 · Chapter 7
Predict what a short program prints, then step through it the way Python runs it: the running line, a frame of names for every call, the lists and tuples those names point at, and the output.
How the lab works
- Predict. Commit to an answer before the program runs.
- Rate your confidence. A sure but wrong answer marks an idea to revisit first.
- Reveal. Watch the program run line by line. Each name is drawn in its frame; an arrow means it points at a list or tuple on the right, and two arrows to one box mean one object with two names. Functions made with
defare left out of the main program’s frame to save room. - Explore. Press Explore freely at any time, or finish the four challenges, to run your own values or your own short program.
7.3 return and print: what a call hands back
A call is worth what its return hands back, and None without one; printing inside a function gives the caller nothing. Watch the Return value appear in each call’s frame.
In the sandbox, pick a program and its number below.
7.4 Scope: which x?
Every call opens its own frame. A name the function assigns lives there; a name it only reads comes from the main program. Watch which frame each x lands in.
In the sandbox, choose a function and the value of x below.
7.6 Tuples: pack, unpack, swap
A tuple bundles several values into one immutable object, which is how a function returns more than one value. Watch the right side being built before any name on the left is bound.
In the sandbox, choose a program and two numbers below.
7.8 List operations: change it or copy it
Methods such as append, remove and sort change the list in place; +, slices and sorted build a new one. Watch whether a new box appears or the old one changes.
In the sandbox, edit the program below; it runs as you type.
7.9 Aliasing and cloning: one list or two?
A name points at a list; it does not hold a copy. Two arrows to one box are aliases, and a change through either shows through both. A clone is a second box.
In the sandbox, edit the program below; it runs as you type.
7.10 Looping and removing: the hidden index
A for loop over a list keeps a hidden index. Removing an element shifts the rest left while the index moves on, so one element is never checked. Watch the marker under the list.
In the sandbox, set L1 and L2 below.
Your results
Personal study lab based on the Chapter 7 guide and the supplied “07_abstraction_functions_tupels_lists.pdf”, written in my own words; every program runs by Python 3’s rules in a small interpreter checked against Python itself. Not an official FAU publication; check course details against the current notes. Made by Thanate Amornjiraporn (Focus).