APDR

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

  1. Predict. Commit to an answer before the program runs.
  2. Rate your confidence. A sure but wrong answer marks an idea to revisit first.
  3. 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 def are left out of the main program’s frame to save room.
  4. 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.

Or try your own

The number is i for is_even and a for f (b is 3); func_a, func_b and func_c are the lecture’s program.

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.

Or try your own

These are the lecture’s scope examples; the main program sets x, calls the function, and prints.

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.

Or try your own

get_data runs on the pairs (x, 'a'), (y, 'b') and (x + y, 'a').

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.

Or try your own

Python with whole numbers, strings, lists, tuples, def, if, for and the list methods of the lecture; up to 16 lines.

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.

Or try your own

Try b = a against b = a[:], and a = a + [1] against a += [1].

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.

Or try your own

Whole numbers separated by commas, up to 8 each.

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).