Topic: Learning notes
MIT 6.100L: Strings, I/O, and Branching
Study notes for MIT OpenCourseWare 6.100L Lecture 2, covering strings, console input and output, comparison operators, and conditional branching.
Welcome to your Python learning journey! This article will guide you through the three core pillars of programming: working with text, communicating with users, and teaching a program to “think” and make decisions. The content is based on MIT 6.100L and aims to give beginners a solid practical foundation.
I. String handling: more than text, it is a sequence of characters
In Python, a string is an ordered, case-sensitive sequence of characters. Letters, numbers, spaces, or special symbols wrapped in single or double quotes are treated as string objects.
- Key questions answered in this section:
- Where does indexing start? In computer science, indexing always starts at 0. The first character of “abc” is at index 0.
- How do you extract substrings? Use slicing syntax
[start:stop:step]. Python includesstartbut excludesstop(up to stop - 1). - What is “immutability”? Once created, strings cannot be modified. If you want to change “car” to “bar”, you must create a new string instead of editing the original characters.
Note: Needs your input [Benchmark data] When processing large-scale data in real projects, using the
+operator versus the.join()method can show a significant performance gap. Consider adding relevant benchmark results here.
II. The program’s “breath”: enabling real interaction with input and output
A useful program needs to accept external information and return results. That is input and output.
- Key questions answered in this section:
- Why do scripts need
print()? In an interactive shell, expressions display automatically, but in a .py file only values wrapped byprint()are output to the screen. - How do you handle user input? Data read by
input()is always a string. If you need arithmetic, perform type casting, such asint()orfloat(). - What is the advantage of f-strings? This is the most powerful formatting tool since Python 3.6. Add
fbefore the string and wrap variables in{}to compute them at runtime.
- Why do scripts need
Note: Needs your input [Version info] In recent Python versions (e.g., 3.10+), f-strings support convenient debugging syntax like
f"{var=}". Consider noting version differences and the benefits of the newer syntax here.
III. Logic branching: giving a program the ability to decide
When a program is no longer linear and can choose paths based on conditions, it gains intelligence.
- Key questions answered in this section:
- What is the difference between
=and==? A single=is assignment, while==tests for equality. - How do you combine logic? Use
and(both true),or(either true), andnot(negate truth values) to handle complex conditions. - Why is indentation critical? Python uses indentation rather than braces to define code blocks. Correct indentation determines which lines belong to an
iforelseblock. - How does
if-elif-elsework? The program checks conditions in order and executes the first block that is true, then skips the remaining branches.
- What is the difference between
Note: Needs your input [Project context / Practice exercise] Based on your experience, provide a nested-branching exercise (for example, a simple ATM withdrawal verification flow) to help readers practice logical decisions.
Expert Insight: A Small Debugging Tip
MIT emphasizes a core idea: “Debug early, debug often.” Do not write a long block of code and then run it once. Build and test one feature at a time, and use the shell to experiment as you go.
Analogy: String handling is like dealing with ingredient labels: you can trim or sort them, but you cannot alter the original label. Input/Output is the ordering conversation with customers. Branching logic is the chef’s decision recipe: if a customer is allergic (if), swap ingredients; otherwise (else), cook as usual.
External Learning Resources
If you want to dive deeper into the fundamentals covered here, visit MIT OpenCourseWare: