Lecture notes · Programming in Python · Lecture 12

List Comprehension, Functions as Objects, Testing, and Debugging

MIT 6.100L Intro to CS and Programming using Python, lecture 12, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part shows how Python can build a new list from an old one in a single line, called a list comprehension.

Key ideas

Words to know

list comprehension
A short one-line way to write a loop that builds a new list, with the square brackets holding the rule for each item.
iterable
Anything Python can step through one item at a time, like a list, a tuple, or a range.
range
A way to make a series of whole numbers. For example, range(6) gives 0, 1, 2, 3, 4, 5. It stops before the end number.
expression
A piece of code that works out a value, such as e squared.

Example

Picture a cookie line where a worker takes every cookie off a tray, puts frosting on each one, and only sends the ones that pass a check onto a new tray. The list comprehension works the same way. It takes each item, applies one step to it, and keeps only the items that pass the test.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part shows how list comprehensions build lists in one line, and how default parameters let a function have a standard setting that callers can change.

Key ideas

Words to know

list comprehension
A short way to write a loop that builds a new list in one line, inside square brackets.
condition
A test written after the loop, such as 'if this is true', that decides which items are kept.
default parameter
An input to a function that already has a standard value, so the caller does not have to give one.
epsilon
The small amount of closeness we want, here how near the answer must be before we stop guessing.

Example

Think of a coffee shop order form where the size is already set to medium. If you do not say a size, you get medium, but you can ask for small or large when you want something different. A default parameter works the same way: it has a standard value that a caller can skip or change.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part explains why a function should take its settings as parameters, how default parameters let the author pick a fallback value, the rules for calling functions with them, and how a function is stored as an object in memory.

Key ideas

Words to know

global variable
A variable that lives outside any function, so any code in the program can read or change it.
parameter
A named input that a function expects when it is called, like x or epsilon.
default parameter
A parameter that already has a value set in the function definition, so the caller can skip it.
alias
A second name that points to the same object in memory, so both names lead to the same thing.

Example

Think of a coffee shop where the barista makes a medium latte unless you say otherwise. The medium size is the default, and you only need to name a different size if you want one. A default parameter works the same way: the function uses its standard value unless the caller gives a different one.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part shows that a function can hand back another function as its result, and how Python finds the right variables when that returned function runs.

Key ideas

Words to know

function object
A function stored as a thing in memory that a name can point to. Giving it another name or passing it around does not run it.
environment
The private workspace Python makes for each function call, holding that call's variables. Each new call gets a new one.
scope
The area of a program where a variable name can be found and used. A variable inside a function is usually only visible inside that function.
formal parameter
A variable name in a function's definition that waits for a value. When the function is called, the value passed in is matched to this name.

Example

Think of a workshop that builds a custom tool with a fixed setting already inside it, then hands the finished tool out the door. The tool still works with the setting it was built with, even though the workshop is closed to the customer. Calling make_prod(2) is like ordering the tool, and calling the result with 3 is like using it.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:00

This part shows how a function can hand back another function, why that helps organize and control code, and why testing and debugging are worth learning.

Key ideas

Words to know

function object
A function treated like any other value, so it can be returned, saved in a variable, and called later.
scope
The area of a program where a name is visible and can be used, such as inside one function or at the top level.
decomposition
Breaking a big problem into smaller pieces, such as separate functions, so each piece is easier to write and read.
flow of control
The order in which a program runs its steps, including when a function starts and when it finishes.

Example

Think of a vending machine that gives you a ticket for a snack instead of the snack itself. You hold the ticket now and use it later, when you are ready. Returning a function works the same way: you get the function back now and decide when to run it.

3 checked practice questions for this part are in the app.

Part 6 · 50:00–1:00:00

This part teaches how to test code by writing down inputs and expected outputs, how to check for bugs after fixing them, and two ways to pick good test cases.

Key ideas

Words to know

unit testing
Testing one small function on its own with many different inputs.
regression testing
Rerunning old tests after a change, to make sure the change did not break anything that already worked.
black box testing
Testing a function by looking only at its written description, not at how it is built inside.
glass box testing
Testing a function by reading its code and making sure every path through it gets tested.

Example

Suppose the same five lines of code are copied into five places in a program. If you fix a mistake in one copy, you may forget the other four, and the bug stays in them. If you put those lines in one function and call it five times, you only need to fix it once.

3 checked practice questions for this part are in the app.

Part 7 · 1:00:00–1:10:00

This part explains how to test loops and conditionals more thoroughly, and gives a systematic way to find bugs when a test fails.

Key ideas

Words to know

glass box testing
Writing tests by looking inside the code and making sure each path through it, like each if-branch or loop, gets tested.
boundary condition
A value right at the edge of a rule, such as the exact number where an if-statement switches from one answer to another. These edge values are often where bugs hide.
logic error
A bug where the program runs without crashing but gives the wrong answer. You cannot spot it by looking at one line alone.
bisection method
A way to find a bug by splitting the code in half again and again. You check the middle, then go to the half that still has the problem.

Example

Imagine a 100-page report with one wrong number somewhere. You check page 50. If it is right, the mistake must be in pages 51 to 100, so you check page 75 next. Each check cuts the search area in half, so you find the mistake much faster than reading every page.

3 checked practice questions for this part are in the app.

Part 8 · 1:10:00–1:15:33

This part shows how to find and fix a bug by using print statements to check values step by step, then retesting the code that already worked.

Key ideas

Words to know

print statement
A line of code that displays the current value of something on screen, so you can see what the program is doing at that moment.
parentheses
The round brackets ( ) you write after a function's name to tell Python to run it. Without them, you only refer to the function and do not run it.
mutable
Able to be changed after it is created. A list is mutable, so you can add, remove, or reorder its items.
alias
A second name that points to the same object in memory. Changing the object through one name also changes it through the other name.

Example

Think of a shared notebook. If you write 'copy this page' by just giving a friend the same notebook, any change they make also shows up in your copy. Making a real photocopy means each of you can change your own version without affecting the other, which is what copying x does here.

3 checked practice questions for this part are in the app.

← Lecture 11: Aliasing and Cloning · Lecture 13: Exceptions and Assertions →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York