CS50 Week 6 Explained: Python After C
After five weeks of C, Week 6 feels like taking off heavy boots. Python does in a few lines what took you pages. The lecture opens by rewriting the Week 5 spell checker in about twenty lines. But the ideas haven't changed: you're doing the same things with far less ceremony, and now you know what's happening underneath.

Jump to the key moments
- 0:43Python
- 6:13Speller in Python
- 16:51Functions
- 38:16Data types
- 1:02:55Object-oriented programming
- 1:32:58Exceptions
- 1:50:20Lists
- 1:54:08Dictionaries
- 2:08:35sys
- 2:14:02csv
- 2:22:04pip
What changes from C
- No compile step you have to run. You run
python hello.py, and the interpreter handles the rest. - No declared types, no semicolons, no curly braces. Indentation is the syntax: the body of an
ifor a loop is whatever's indented under it. - No manual memory management. Lists grow by themselves, and nothing needs freeing.
- Integers don't overflow. Python's integers grow as big as they need to. Floats are still imprecise, though, because that's a property of binary, not of C.
i++doesn't exist. Usei += 1.
The types you'll use constantly
str (text), int, float, bool (True or False), list (a growable array), tuple (a fixed group of values), dict (keys mapped to values, which is a hash table built into the language) and set (unique values). Most Python programs are built from lists and dicts.
Objects and methods
In Python, values come with their own functions, called methods. Instead of passing a string to toupper, you write name.upper(). The lecture introduces this briefly as object-oriented programming, and you'll lean on it constantly: .lower(), .strip(), .split(), .append().
Exceptions
When something goes wrong, such as converting "cat" into an int, Python raises an exception instead of returning a special value. You catch it with try and except ValueError and ask again. It's cleaner than C's "check every return value", but only if you catch the specific exception you expect.
Libraries: sys, csv, and pip
sys.argv gives you command-line arguments (as argc and argv did in C), and sys.exit(1) sets the exit status. The csv module reads spreadsheet-style files, and csv.DictReader gives you each row as a dictionary keyed by the column headings. pip installs other people's libraries. The ecosystem of those libraries is a big part of why Python is so widely used, AI included.
Problem Set 6: what it asks
Most of this set is "Sentimental": re-solving earlier problems in Python so you can see the two languages side by side.
- Hello in Python.
- One of Mario (less) or Mario (more).
- One of Cash or Credit.
- Readability.
- DNA: identify whose DNA a sequence is by counting short repeated patterns and matching the counts against a CSV database.
Where people get stuck
- Translating C line by line. Resist it. Write the Python version from your understanding of the problem, not from your C file. It'll be shorter, and it'll read like Python.
- Cash in Python: input arrives in dollars this time. Convert to cents and round before doing any coin arithmetic, or floating-point imprecision will cost you a coin.
- DNA's types: everything read from a CSV is a string. Comparing the string "4" with the integer 4 is always false, so convert before comparing.
- DNA's counting: you want the longest run of consecutive repeats of a pattern, not the total number of times it appears. The distribution code includes a helper for this, so read it before writing your own.
Check yourself
What does Python use instead of curly braces to group code?
Indentation. The lines indented under an if, for or def are its body.
Can a Python int overflow the way a C int does?
No. Python integers grow as big as they need to. Floats still have limited precision, though.
Which built-in type is Python's version of the hash table you built in Speller?
The dictionary (dict). Sets are hash-based too.
How do you ask again when a user types letters where you wanted a number?
Loop, and inside the loop try converting with int(). Catch ValueError and ask again. Break out of the loop once the conversion succeeds.
Python is the language of AI. See the full journey from Python to building with AI in Cocoon's programmes.
See Cocoon programmes →