← CS50: Introduction to Computer Science

Week 6: Python

The same problems, solved again in far fewer lines, and an honest look at what that convenience costs and what it buys.

8 topics · 28 questions

Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.

Why Python After C

  1. How it worksHow does Python's interpreter run your code line by line instead of compiling it all up front the way C does?
  2. Common mistakeWhy isn't Python simply a slower, easier version of C?
  3. Going furtherWhat happens to a program's development speed versus its running speed when you switch from C to Python?
Your turn1 of 2
A task takes 363 lines of C code, with each Python line replacing about 6 lines of the equivalent C. About how many lines does the Python version take?

Python Syntax and Dynamic Typing

  1. How it worksHow does Python figure out a variable's type at runtime instead of requiring you to declare it up front?
  2. Worked exampleWalk me through what happens when a variable x is set to 5, then reassigned to the string hello, in Python.
  3. Common mistakeWhy isn't indentation in Python just a style preference the way it is in C?
Your turn1 of 2
A Python variable is created once and then reassigned 28 more times, each time to a new value. How many total values has that variable held over the course of the program, counting its initial value?

Python Lists vs. C Arrays

  1. Worked exampleWalk me through appending the value 9 to the Python list 1, 2, 3 and inspecting the result.
  2. Common mistakeWhy isn't a Python list stored the exact same way in memory as a C array?
  3. Going furtherWhat happens behind the scenes in memory when a growing Python list runs out of its currently allocated space?
Your turn1 of 2
A Python list's underlying array has a capacity of exactly 140 slots, which is now full. Appending one more element doubles the capacity. What is the new capacity after doubling?

Python Strings and Slicing

  1. How it worksHow does slice notation, a colon between two indices, pull out a substring by position?
  2. Worked exampleFind the result of slicing the string PYTHON with the slice from index 1 to index 4.
  3. Going furtherWhat happens when a slice's ending index is simply left blank in Python?
Your turn1 of 2
Find the number of characters returned by slicing a string using the slice from index 3 to index 15 (exclusive of 15).

Exceptions and Error Handling

  1. How it worksHow does a try-except block catch an error and let the program keep running instead of crashing?
  2. Common mistakeWhy isn't wrapping every single line of code in a try block good defensive programming?
  3. Worked exampleWalk me through a try-except block that catches a ValueError when converting the text abc to an int.
  4. Going furtherWhat happens when an exception is raised inside a try block that has no matching except clause?
Your turn1 of 2
A script wraps 22 separate risky operations in one big try block, and the 2-th operation (counting from 1) raises an exception first. How many of the operations after it in the same try block get skipped as a result?

Python Dictionaries

  1. How it worksHow does a Python dictionary store key-value pairs so that looking one up takes roughly constant time?
  2. How it worksHow does a Python dictionary's built-in hashing do the exact job that a hand-built hash table does in C?
  3. Worked exampleWalk me through building a dictionary that maps CAT to 4 and DOG to 3, then looking up CAT.
  4. Common mistakeWhy isn't a Python dictionary the same thing as a Python list with named indices?
Your turn1 of 2
A dictionary already holds 35 key-value pairs, and 20 more distinct keys are added to it. How many key-value pairs does the dictionary hold afterward?

Libraries and Imports

  1. How it worksHow does an import statement pull in code someone else already wrote, tested, and packaged?
  2. Going furtherWhat happens to a program's reliability when it depends on a library that gets updated after the program ships?
  3. Ties togetherHow does importing a library in Python connect to including a header file like stdio.h in C?
Your turn1 of 2
A program imports 7 separate libraries, and each library defines 20 functions the program can call. How many total imported functions does the program have access to?

The Cost of Python's Convenience

  1. How it worksHow does Python's interpreter re-check each variable's type on every pass through a loop, unlike compiled C?
  2. Common mistakeWhy isn't 'Python is just slower C' actually the right way to think about the tradeoff?
  3. Worked exampleFind roughly how many times slower a pure Python loop runs compared to the same loop compiled in C, based on typical benchmarks.
  4. Going furtherWhat happens to the speed gap between Python and C once a Python program calls into a C-optimized library?
  5. Ties togetherHow does the speed-versus-convenience tradeoff reappear later when comparing writing SQL by hand to using an ORM?
Your turn1 of 2
Find roughly how many times slower a pure Python loop runs compared to the same loop compiled in C, based on typical benchmarks, where C takes 7 milliseconds and Python takes 332 milliseconds for the identical task.