Lecture notes · Programming in Python · Lecture 2

Strings, Input/Output, and Branching

MIT 6.100L Intro to CS and Programming using Python, lecture 2, 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

Ana Bell introduces strings, which are text stored as objects, and shows how Python joins them with plus and repeats them with star.

Key ideas

Words to know

string
A piece of text stored as one object in Python, written inside quotation marks.
concatenate
To join two or more strings end to end to make one longer string.
case sensitive
Treating capital and lowercase letters as different. So 'Me' and 'me' are not the same string.
bind
To attach a name to an object, so that using the name gets you that object.

Example

Think of a string like a row of letter tiles on a table. Putting two rows end to end makes one longer row, but nobody adds a gap between them unless you place one there yourself. Making copies of the same row and lining them up is like using the star sign.

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

Part 2 · 10:00–20:00

This part shows how to measure a string, pick out single characters by their position, and cut out pieces of a string.

Key ideas

Words to know

index
The position number of a character in a string. Counting starts at 0, not 1.
slice
Taking a piece of a string by writing square brackets with numbers in them, such as start, stop, and step.
cast
Changing a value from one type to another, such as turning the string '3' into the number 3.
expression
A piece of code that Python works out to give one single value.

Example

Think of a row of numbered lockers where the first locker is number 0. Asking for locker 2 gets you the third one from the left, and asking for locker -1 gets the last one, as if you were counting from the other end. A slice is like taking a block of lockers from one number up to, but not including, another number.

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

Part 3 · 20:00–30:00

This part explains how to pull out pieces of a string using start, stop, and step, and why strings can't be changed once they exist.

Key ideas

Words to know

slice
A piece of a string that you cut out by giving a start, a stop, and an optional step.
index
The position number of a character in a string. Counting usually starts at 0.
step
How far to move each time you pick the next character. A step of 2 skips one character between picks.
immutable
Cannot be changed after it is created. To get a different version, you make a new one.

Example

Think of a row of numbered lockers, starting at 0. Saying 'open lockers 3 up to 6' means you visit lockers 3, 4, and 5, but not 6, just like a slice that stops before its stop index. Changing a letter in a string is like trying to swap the contents of a locker that is sealed shut, so you build a new row instead.

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

Part 4 · 30:00–40:00

This part shows how a program shows output with print, how it gets typed answers from the user with input, and why those answers arrive as text that may need converting.

Key ideas

Words to know

print
A command that tells Python to show a value or result on the screen as program output.
input
A command that pauses the program, shows a message, and waits for the user to type something and press Enter.
cast
Changing a value from one type to another, such as turning the text 3 into the number 3 with int, or the number 3 into text with str.
concatenate
Joining strings end to end into one longer string, without adding any spaces between them.

Example

Think of a cashier who asks a customer for a number. Whatever the customer says is written on a paper slip as text, even if it is the digits 3, so the cashier must convert the slip into a real number before doing any math with it.

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

Part 5 · 40:00–50:00

This part shows how a Python program can ask the user for input, print text with spaces in the right places, and use f-strings to print mixed text and calculations.

Key ideas

Words to know

f-string
A way to write text in Python that puts a letter f before the quotes, so you can put calculations inside curly brackets and print them in the middle of the sentence.
expression
Something Python can work out to get a value, like a number, a variable, or a math calculation.
concatenate
To join pieces of text together end to end, like sticking words together into one longer piece of text.
cast
To change a value from one kind to another, such as turning a number into text so it can be joined with other text.

Example

Think of a form letter with blanks, like Dear ____, your order of ____ is ready. Each blank gets filled in with the right value when you print it. An f-string works the same way: the curly brackets are the blanks, and Python fills them in before showing the sentence.

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

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

This part introduces decision points in code, which let a program choose between paths, and shows how comparisons produce True or False answers.

Key ideas

Words to know

Boolean
A type of value that is either True or False, named after a mathematician who studied logic.
assignment
Storing a value in a variable so you can use it later, done with a single equal sign.
cast
Changing a value from one type to another, such as turning text the user typed into a whole number.
operator
A symbol or word that works on values, such as == for checking equality or < for checking which is smaller.

Example

Think of getting into a concert. If the rule is that you need a ticket and an ID, that is like "and": both must be true before you get in. If the rule is that you can pay with cash or a card, that is like "or": one true option is enough.

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

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

This part shows how a program can make decisions using if, elif, and else, and why indentation decides which lines belong to each decision.

Key ideas

Words to know

Boolean
A value that is either true or false. Programs use Booleans to decide what to do next.
conditional
A piece of code that makes a decision. It checks a condition and runs different commands depending on the answer.
indentation
The spaces at the start of a line. In Python, indentation shows which lines belong together as one block.
elif
Short for 'else if.' It checks another condition after an if has been found false.

Example

Think of getting a free-food email at midnight. You check one thing: is there free food? If yes, you go get it. If no, you go to sleep. Only one of those two choices happens, and the rest of your night goes on the same way either way.

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

Part 8 · 1:10:00–1:18:51

This part shows how to put decisions inside other decisions, why Python's indentation helps you read code, and how to choose between if, elif, and else.

Key ideas

Words to know

conditional
A piece of code that makes a choice. It checks if something is true and runs different code depending on the answer.
code block
A group of lines that belong together under a condition. In Python, these lines are the ones indented under an if, elif, or else.
indentation
The spaces at the start of a line. In Python, indentation shows which lines are part of the same block.
cast
To change a value from one type to another, such as turning a whole number into a decimal number, so the computer compares them the way you want.

Example

Think of a store door sign: if the store is open, then you check whether you have a ticket before going in. The ticket check only matters when the store is open, just as an inner if only runs when the outer if is true.

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

← Lecture 1: Introduction to CS and Programming Using Python · Lecture 3: Iteration →

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