Lecture notes · Programming in Python · Lecture 4

Loops over Strings, Guess-and-Check, and Binary

MIT 6.100L Intro to CS and Programming using Python, lecture 4, 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 loops can stop early with a break statement, and it starts a small coding exercise that counts even numbers using a loop.

Key ideas

Words to know

break statement
A command that makes the program leave a loop early, before the loop would normally end.
condition
A yes-or-no test in the code. A while loop keeps going as long as its condition is true.
nested loop
A loop placed inside another loop, so the inner loop runs again each time the outer loop runs.
remainder
What is left over after dividing one number by another. For example, 5 divided by 2 leaves a remainder of 1.

Example

Imagine you are checking a long list of names for one particular name. Once you find it, you stop reading the list and put it down. A break statement works the same way: it stops the loop as soon as the job is done, and the rest of the list is never checked.

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

Part 2 · 10:00–20:00

This part shows how a for loop can go through the letters of a word directly, and how a short cheerleader program uses two loops together.

Key ideas

Words to know

index
The position number of an item in a sequence. In a string, the first letter is at index 0.
loop variable
The name you give to the value a for loop holds on each pass through the loop. You can pick any name you like.
case sensitive
Capital and lowercase letters count as different. So a lowercase 'd' is not the same as a capital 'D'.
f-string
A way to put a variable's value into a line of printed text, using an f in front of the quotation marks. It was covered in lecture 2.

Example

Imagine a teacher calling the roll. She could say 'student number 1, student number 2,' then look up each name. Or she could just read each name straight from the list. The second way is shorter and easier to follow, just like looping over the letters directly.

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

Part 3 · 20:00–30:00

This part shows how a loop can repeat a task a set number of times, and how to count unique letters in a string by keeping a record of letters already seen.

Key ideas

Words to know

loop variable
The variable, like i in a for loop, that takes a new value each time the loop repeats.
Boolean
A value or expression that is either true or false. The word not flips a Boolean, so true becomes false and false becomes true.
append
To add one new item to the end of a list or string.
pass
A line of code that does nothing. It is used when a branch of an if statement should be left empty.

Example

Think of a guest list at a party where you only want each name written down once. Before you write a name, you check the list. If the name is already there, you skip it. If not, you add it. When you finish, the number of names on the list is the number of different guests.

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

Part 4 · 30:00–40:00

This part teaches guess-and-check, a simple method that tries every possible answer in order until it finds the right one or runs out of options.

Key ideas

Words to know

exhaustive enumeration
Trying every possible answer, one at a time, until you find the right one or have tried them all.
infinite loop
A loop that never stops because its stopping condition is never met. It can freeze a program.
perfect square
A number you get by multiplying a whole number by itself, such as 4 (2 times 2) or 9 (3 times 3).
keyword
A word that Python reserves for its own use, such as pass or while. You cannot use it as a variable name.

Example

Imagine you are looking for a friend's house on a street by checking house numbers one at a time, starting from the lowest. If you reach a number higher than the one you want, you know the house is not on this street, so you can stop searching. The stopping point saves you from checking every house on the map.

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

Part 5 · 40:00–50:00

This part shows how to check values one by one with a loop, and how to use a true-or-false flag to remember what happened and act on it at the end.

Key ideas

Words to know

flag
A variable that holds either true or false, used to remember whether something happened.
Boolean
A word for a value that is either true or false, and the kind of value a flag holds.
for loop
A loop that goes through a list of values in order, one at a time, and then stops by itself.
break
A command that stops a loop right away, even if it has more values to go through.

Example

Think of a teacher calling names from a class list to find one student. The teacher goes down the list in order, and when the name is found, they stop calling. If they reach the end without finding it, they announce 'not found' once, not after every name.

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

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

This part shows how to use true-or-false flags to track events in a loop, and how a for loop can solve a cube root problem by checking every possible guess.

Key ideas

Words to know

Boolean flag
A variable that is either true or false (or 0 or 1) and changes when something happens in the code, so you can check later whether it happened.
Guess-and-check
A method where you try possible answers one at a time until you find the right one.
Absolute value
A number's size without its sign. For example, the absolute value of -5 is 5.
Break
A command that stops a loop right away, even if it has not finished going through all its values.

Example

A teacher checks a class roster by writing 'absent' next to every name at the start. When a student answers, the teacher changes that name to 'present.' At the end, the teacher reads out only the names still marked absent, instead of making an announcement after every name.

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

Part 7 · 1:00:00–1:13:09

This part shows how a computer can solve puzzles by trying values, why it stores decimal numbers in a way that can cause tiny errors, and how binary numbers work.

Key ideas

Words to know

float
A number with a decimal point, like 0.1 or 3.5, which stands for a real number.
binary
A way of writing numbers using only 0 and 1. Each place stands for a power of 2 instead of a power of 10.
floating point error
A tiny mistake that happens when a computer stores or calculates with decimal numbers, which can grow into a big surprise after many steps.
remainder
The amount left over after dividing one whole number by another. Dividing by 2 leaves a remainder of 0 for even numbers and 1 for odd numbers.

Example

Think of a light switch. It is either on or off, and that is easy for a machine to tell apart. Binary works the same way: each digit is either off (0) or on (1), so a row of switches can stand for any whole number.

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

← Lecture 3: Iteration · Lecture 6: Bisection Search →

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