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 freeWatch 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
- A string is a sequence of characters, such as letters, digits, symbols, or a space. You make one by putting the characters inside quotation marks, and Python treats upper and lower case letters as different. 4:52
- The plus sign joins two strings into one new string. Python does not add a space for you, so you must add one yourself if you want it. 6:59
- The star sign repeats a string a set number of times. It works between a string and a number, in either order, but not between two strings. 8:05
- Python never changes an existing object. When you assign a new value to a name, Python creates a new object and points the name at it. 3:43
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
- Python can change a string like '3' into the number 3 using int. This change is called a cast. 10:21
- The len command counts how many characters are in a string, and it gives you that count as a number. 12:14
- Positions start counting at 0, so the first character is at index 0. Negative indexes count from the right, so index -1 is the last character. 14:01
- If you ask for a position past the end of a string, Python gives an index error. 15:26
- A slice takes a piece of a string using a start, a stop, and a step. The stop position is not included, and if you leave out the step, it is 1 by default. 17:11
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
- A slice like s[3:6] takes characters starting at index 3 and stops just before index 6, so index 6 is never included. 20:00
- The step sets how many positions to move each time. A step of 1 takes every character, and a step of 2 skips every other one. 20:43
- A negative step works from right to left. An empty slice with a step of -1 gives the whole string in reverse. 21:07
- Strings are immutable, which means you cannot change a letter inside them. You make a new string and point your name at it instead. 27:14
- Typing 3 + 2 into the shell shows an answer, but the lesson says this is not true output. Real input and output comes next. 29:22
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
- Typing an expression in the shell shows its value, but a program file shows nothing unless you tell Python to print it. The shell only peeks at values; a file needs explicit output. 30:41
- The print command wraps an expression so Python shows its result, and each print puts its result on a new line. This keeps the output clean because you choose exactly what the user sees. 31:21
- A print statement can show several objects on one line. Separating them with commas makes Python put a space between each one, and it works for any type. 33:09
- Concatenation glues strings together with no automatic spaces, and you cannot join a string to an integer without converting the integer first with str. Trying it gives a type error. 33:51
- The input command pauses the program, waits for the user to type something and press Enter, then replaces itself with what was typed. Whatever the user types is always saved as a string, even if it looks like a number, so int is needed to do math with it. 37:51
Words to know
- 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
- input() pauses the program and waits for the user to type something. Whatever they type can then be used in the program, such as in a print statement. 41:07
- A comma between items in print adds a space between them. To control spacing exactly, you can join pieces of text with a plus sign and add a space yourself. 42:53
- Newton's method finds roots by making a better guess each time. This part only computes one next guess from a starting guess, using the formula: the guess minus the function value divided by the derivative at that guess. 44:24
- Python does not work out derivatives on its own in this example, so the derivative (3 times the guess squared) is typed into the code by hand. 45:30
- An f-string starts with the letter f before the quotes. Anything inside curly brackets is calculated first, and the result is printed in place, so you do not need to join or convert pieces by hand. 48:51
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
- Until now, programs ran straight through, one line after another, with no way to skip lines, repeat lines, or make a choice. 50:21
- Branching adds decision points. If one value is less than another, the program runs one piece of code, and otherwise it runs a different piece, so some code can be skipped. 50:43
- A single equal sign (=) assigns a value to a variable. A double equal sign (==) asks Python whether two things are equal. 51:28
- A comparison such as 2 < 3 does not give a number. It gives a Boolean value, either True or False. The lesson says 2 < 3 evaluates to True. 52:34
- The word "and" gives True only when both parts are True. The word "or" gives True when at least one part is True. In Python you must type the words and and or, not symbols. 55:02
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
- Booleans are true-or-false values, and they let a program choose between doing one thing and doing another. The lesson's everyday example is a midnight free-food email: go get the food, or go to sleep. 1:00:21
- An if statement starts a decision point. If its condition is true, Python runs the indented lines under it. If not, Python skips them and goes on with the main program. 1:02:29
- Python knows which lines belong to a decision by their indentation. Every line indented under the if (and its colon) is part of that block, and the block runs all at once. 1:03:30
- An elif checks another condition only if the earlier ones were false. Python runs just the first true block, and an else at the end runs only if nothing above it was true. 1:05:45
- A line that is not indented under an if is not part of that if. It always runs after the decision is made, even when the condition was false. The quick check at the end shows this mistake. 1:09:45
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
- You can nest conditionals, which means putting an if statement inside the code block of another if. The inner check only runs when the outer condition is true. 1:10:23
- Python makes you indent code, so the layout shows which lines belong to which block. This helps you trace what the code does and find bugs faster. 1:12:09
- Once one branch of an if, elif, or else chain runs, the rest of the chain is skipped. Separate if statements are checked on their own, so more than one can run. 1:17:30
- Ana found a bug by running the program with a test input. Removing the equal sign from the 'too high' check fixed it, which shows why you should run and test your code early and often. 1:15:07
- An else is a shorter way to write the last case when you already know no other options are left. An elif with the same test would work the same way. 1:17:08
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