Lecture notes · Programming in Python · Lecture 7

Decomposition, Abstraction, and Functions

MIT 6.100L Intro to CS and Programming using Python, lecture 7, 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 explains how big jobs get split into small, reusable pieces, and how hiding the details inside each piece (abstraction) makes that possible.

Key ideas

Words to know

abstraction
Hiding the details of how something works, so you only need to know what it takes in and what it gives back.
decomposition
Breaking a big job or program into smaller separate parts that fit together.
function
A named, reusable piece of code that performs one task. You give it inputs and it gives back an output.
specification (docstring)
A written description at the top of a function that explains its inputs, what it does, and its output. It works like a contract for anyone who uses the function.

Example

Think of a TV remote. You press the power button and the TV turns on, and you do not need to know how the circuits make that happen. The remote's buttons are the interface, and the inside is hidden, just like a function's docstring tells you what to put in and what you get back.

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

Part 2 · 10:00–20:00

This part explains how to define a function in Python, how to write its docstring and body, and how a return statement sends a result back to whoever uses it.

Key ideas

Words to know

function
A named block of code you can reuse. You write it once, then run it as many times as you need.
parameter
An input that a function expects. It is listed inside the parentheses after the function's name. The lesson also calls these arguments.
docstring
A note written in triple quotes at the top of a function. It tells the user what the inputs are, what the function does, and what it gives back.
return
The keyword that ends a function and sends a value back to whoever used it, so that value can be used elsewhere.

Example

Think of a function like a recipe card. Writing the card does not cook anything. The cooking only happens when someone follows the recipe, and the finished dish is what they hand back. Each time you want that dish, you follow the card again, so you only write the recipe once.

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

Part 3 · 20:00–30:00

This part explains how a function is run after you write it, and why you need a function call and a print statement to see any result.

Key ideas

Words to know

function call
Using a function by typing its name, parentheses, and any inputs, which tells Python to run it now.
return value
The one answer a function hands back when it finishes, which then takes the place of the function call in the code.
formal parameter
The placeholder name written in a function's definition, like i, which stands for whatever value the caller gives later.
expression
A piece of code that Python works out into a single value, such as 3 plus 2 becoming 5.

Example

Think of a recipe card in a kitchen drawer. Writing the card does not make any food, and the card is only useful when someone follows it. When you do follow it, the finished dish is what you get back, and that dish takes the place of the cooking step.

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

Part 4 · 30:00–40:00

This part shows how to write a function that gives back a answer with return, how a function is stored and used as a black box, and how to plan a function on paper before you write code.

Key ideas

Words to know

function
A named piece of code you can run again whenever you need it by calling its name.
return
The word that sends a value back out of a function so the rest of the program can use it.
expression
A piece of code that gets worked out into a single value, like a function call that gives back true or false.
remainder
The amount left over after you divide one number by another. For example, 7 divided by 2 leaves a remainder of 1.

Example

Think of a vending machine. You put in money and press a button, and a snack comes out. You do not need to know how the machine moves the snack inside to use it. A function works the same way: you give it inputs, and it gives you back an output.

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

Part 5 · 40:00–45:45

This part shows how to build a small, working piece of code step by step, then use a function so you can reuse it many times.

Key ideas

Words to know

abstract
To hide the details of a task behind a simple name, so you can use it without thinking about every step.
loop variable
A variable, often named i, that changes each time through a loop so the loop can visit each value in turn.
edge cases
Special or unusual inputs that can break code if you do not plan for them, such as the first or last value in a range.
specification
A written description of what a function should do, including its inputs and what it should give back.

Example

Think of a coffee machine. The company designs and builds the machine once, which is like creating a function. Then you press the button every morning to make a cup, which is like running the function. You do not rebuild the machine each time you want coffee.

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

← Lecture 6: Bisection Search · Lecture 9: Lambda Functions, Tuples, and Lists →

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