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
What is the first example function computing?
- The number of primes within a certain range
- The trajectory of a simulated missile
- Whether a credit card number is valid
- The final score in a bowling match
- The mental health benefits of cycling
Which data science library that we have used before in SD211 comes up in the video?
- Plotly
- Wikipedia
- EasyGUI
- BeautifulSoup
- NumPy
How would we write the type for “a list of integers” in Python?
list[int]
list_int
List<int>
List<Integer>
list: int
Imagine a function
ordinal
that takes a number and converts it to a string for that ordinal number, so likecardinal(3)
would return"third"
.What should the type annotation for such a function look like?
def ordinal(int x): str
def str ordinal(int x)
def ordinal(int: x) -> str
def ordinal(x: int) -> str
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