Lambda Functions, Tuples, and Lists
MIT 6.100L Intro to CS and Programming using Python, lecture 9, 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 freeWatch or read the original lecture: MIT OpenCourseWare.
Part 1 · 0:00–10:00
This part explains lambda functions, which are short, nameless functions you can write in one line when you only need one.
Key ideas
- In Python, functions are objects, so you can pass a function into another function as a parameter, just as you would pass a number. The apply example used this idea. 0:55
- A lambda function is a quick way to write a simple function in one line without giving it a name. It is best for tasks that are only one step and that you only need once. 2:46
- A lambda has four pieces: the word lambda, the input names, a colon, and the body. The body is written without a return keyword, because the result of the body is sent back automatically. 3:29
- You can use a lambda right where you need it, such as by putting the whole lambda inside a function call. If you want to reuse it, though, you would have to copy and paste it, because it has no name to call again. 7:01
- When a function is called, Python creates a new environment, which is a separate space that holds the names and values for that call. Parameters are matched to the values passed in, and then the function body runs. 9:41
Words to know
- anonymous function
- A function that has no name. You create it and use it in the same place.
- lambda
- The keyword that tells Python you are writing a short, nameless function in one line.
- parameter
- An input name listed in a function's definition, which receives a value when the function is called.
- environment
- The space Python makes for each function call, which keeps track of the names and values used during that call.
Example
Think of a lambda like a sticky note with a quick instruction written on it, such as 'add 2 to whatever number you get.' You use the note once, right where you need it, and then throw it away. A named function is more like a recipe card in a binder, which you keep so you can use it again and again.
3 checked practice questions for this part are in the app.
Part 2 · 10:00–20:00
This part traces how a function that takes another function works step by step, then introduces tuples, a new kind of container that holds an ordered list of items.
Key ideas
- When a function is called, Python matches each formal parameter (the name in the function's definition) to an actual parameter (the value passed in). In do_twice, n gets 3 and fn gets the lambda function. 10:18
- Each function call gets its own scope, which is a separate space that holds that call's names. If a call needs a value it does not have yet, it waits on hold while a new call runs, and that is how the two lambda calls stack up. 12:11
- The inner call takes its input from the scope above it, so the inner fn(n) becomes 9. That 9 then goes into the outer call, which returns 81. 13:18
- A tuple is an indexable ordered sequence, meaning it holds a series of objects in a fixed order, and you can pick out each one by its position. Tuples work much like strings, so the same indexing and slicing ideas apply. 16:56
- To make an empty tuple, write just () with nothing inside. To make a tuple with one item, you must put a comma after that item, because without the comma, the parentheses just group the value. 18:44
Words to know
- formal parameter
- A name listed in a function's definition that stands in for a value. For example, n in do_twice.
- scope
- A private space where a function keeps its names and values while it runs. Each call gets a new one.
- tuple
- A container that holds several items in a set order. You write it with parentheses, like (1, 2, 3).
- indexable
- Able to pick out one item by its position number, starting at 0 for the first item.
Example
Think of a tuple as a row of numbered lockers, where each locker holds one thing and the order of the lockers never changes. You can open locker 0, then locker 1, and so on. A function scope is like a separate desk for each job: when one job needs a result from another job, it pauses at its desk until the answer comes back.
3 checked practice questions for this part are in the app.
Part 3 · 20:00–30:00
This part explains tuples, a way to hold a fixed group of values in order, and shows how they can swap variables and return several values from a function.
Key ideas
- A tuple is made by putting values in parentheses, separated by commas. Its items, called elements, can be different types, such as integers, strings, floats, and Booleans. 20:21
- Once a tuple is created, you cannot change one of its elements. You can make a new tuple based on the old one, but the original stays the same. 22:09
- A variable name is just a label pointing at an object. If you give the name a new value, the old object stays in memory and the name points somewhere else. 23:12
- A tuple can hold other tuples. To reach a nested value, you chain indexes from left to right, such as seq[3][0]. 25:03
- Tuples let you swap two variables in one line, as in x, y = y, x. They also let a function return several values, because the whole tuple counts as one object. 27:19
Words to know
- tuple
- A fixed group of values written in parentheses and separated by commas. Once made, its values cannot be changed.
- element
- One item inside a tuple, string, or other group of values.
- concatenate
- To join two things end to end. Joining two tuples with plus makes one longer tuple.
- iterate
- To go through a group one item at a time, for example with a for loop.
Example
Think of a tuple like a sealed box with labeled slots. You can look at what is in each slot, but you cannot swap out an item inside. If you want different contents, you pack a new box instead.
3 checked practice questions for this part are in the app.
Part 4 · 30:00–40:00
This part shows how a function can hand back several answers at once using a tuple, how to count vowels and consonants with a loop, how a star lets a function take any number of inputs, and how lists are written.
Key ideas
- A function can return several results together by packing them into one tuple. The tuple is a single object that holds the values in order. 30:00
- You can split a tuple into separate variables in one line, such as quote, rem = 2, 1. Python gives the first name the first value and the second name the second value. 31:26
- To count vowels and consonants, loop through each character in the string and check whether it appears in a string of vowels. Add one to the vowel count if it does, and to the consonant count if it does not. 34:08
- A star before a parameter name, like *args, lets a function accept any number of inputs. Python gathers those inputs into a tuple for you. 37:15
- A list is written with square brackets instead of parentheses. Many of the things you can do with a list work the same way as with a tuple. 39:26
Words to know
- tuple
- A fixed group of values held together in one object, written with parentheses, such as 3, 1.
- quotient
- The whole-number answer you get when one number is divided by another, such as 3 when you divide 10 by 3.
- parameter
- A name in a function's definition that stands for an input the function will receive when it is called.
- list
- An ordered group of values written with square brackets, similar to a tuple but written differently.
Example
Think of a lunch bag that holds a sandwich, an apple, and a juice box. You hand over one bag, but it carries three things inside, just as a function returns one tuple that carries several values. When you open the bag, you can take each item out and use it separately.
3 checked practice questions for this part are in the app.
Part 5 · 40:00–45:18
This part shows how lists are made, how they are similar to tuples and strings, and how loops can go through list items directly.
Key ideas
- A list is made with square brackets instead of the parentheses used for tuples. An empty pair of brackets makes a list with no items in it. 40:23
- Most things you can do with lists work the same way as with tuples and strings. The only real difference shown here is the brackets. 40:44
- A loop can go through the items in a list one by one, without using index numbers. This version is easier to read, and the lesson calls it more Pythonic, meaning written the way Python programmers usually write it. 41:05
- Writing a short comment under the for loop helps you keep track of what the loop variable holds at each step. You can then spend your thinking on other parts of the code. 43:16
- The same loop can do different jobs with a small change. Adding up numbers in a list and adding up the lengths of strings in a list need only a one-line change inside the loop. 44:00
Words to know
- element
- One item inside a list, tuple, or string. For example, 8, 3, and 5 are the three elements of a list of numbers.
- index
- The position number of an item in a list, written inside square brackets to pick that item out.
- loop variable
- The name that holds one item at a time while a loop runs. In a for loop, it takes on each value in turn.
- Pythonic
- Written in the clear, short style that experienced Python programmers prefer.
Example
Think of a row of mailboxes. Looking at each mailbox in order is like a loop that goes through the items directly. Writing down the mailbox number first and then walking to it is like using an index, which takes more steps and is harder to follow.
3 checked practice questions for this part are in the app.
← Lecture 7: Decomposition, Abstraction, and Functions · Lecture 10: Lists and Mutability →
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