SD 212 Spring 2023 / Homeworks


This is the archived website of SD 212 from the Spring 2023 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

hw29: To type or not to type

  • Due before the beginning of class on Monday, April 17

Today’s homework is going to show you a little bit more about Python’s type annotations, specifically from the standpoint of what benefits you can get as a programmer from adding type annotations to your code.

This is especially important because, unlike in many other programming languages, in Python typing is completely optional; any program that works with type annotations will also work exactly the same without them. The difference is … well, watch the video to find out!

Video to watch

5 Reasons Why You Should Use Type Hints In Python by a Dutch youtuber and computer scientist named Arjan.

The video is only about 13 minutes long, but it has some nice examples and specific suggestions based on real-world experience.

Questions to answer

  1. What is the first example function computing?

    1. The number of primes within a certain range
    2. The trajectory of a simulated missile
    3. Whether a credit card number is valid
    4. The final score in a bowling match
    5. The mental health benefits of cycling
  2. Which data science library that we have used before in SD211 comes up in the video?

    1. Plotly
    2. Wikipedia
    3. EasyGUI
    4. BeautifulSoup
    5. NumPy
  3. How would we write the type for “a list of integers” in Python?

    1. list[int]
    2. list_int
    3. List<int>
    4. List<Integer>
    5. list: int
  4. Imagine a function ordinal that takes a number and converts it to a string for that ordinal number, so like cardinal(3) would return "third".

    What should the type annotation for such a function look like?

    1. def ordinal(int x): str
    2. def str ordinal(int x)
    3. def ordinal(int: x) -> str
    4. def ordinal(x: int) -> str
    5. def ordinal(x: int): str

Submit command

To submit files for this homework, run one of these commands:

submit -c=sd212 -p=hw29 hw29.md 
club -csd212 -phw29 hw29.md
Download the file hw29.md to fill in and submit for this homework