Python Home Page


Try It Yourself

You are almost done with the course. Nice job.

Fortunately, we have a couple more interesting problems for you before you go.

As always, run the setup code below before working on the questions.

Exercises

0.

Let's start with a string lightning round to warm up. What are the lengths of the strings below?

For each of the five strings below, predict what len() would return when passed that string. Use the variable length to record your answer, then run the cell to check whether you were right.

1.

There is a saying that "Data scientists spend 80% of their time cleaning data, and 20% of their time complaining about cleaning data." Let's see if you can write a function to help clean US zip code data. Given a string, it should return whether or not that string represents a valid zip code. For our purposes, a valid zip code is any string consisting of exactly 5 digits.

HINT: str has a method that will be useful here. Use help(str) to review a list of string methods.

2.

A researcher has gathered thousands of news articles. But she wants to focus her attention on articles including a specific word. Complete the function below to help her filter her list of articles.

Your function should meet the following criteria:

3.

Now the researcher wants to supply multiple keywords to search for. Complete the function below to help her.

(You're encouraged to use the word_search function you just wrote when implementing this function. Reusing code in this way makes your programs more robust and readable - and it saves typing!)

Keep Going

You've learned a lot. But even the best programmers rely heavily on "libraries" of code from other programmers. You'll learn about that in the last lesson.


Python Home Page

Have questions or comments? Visit the Learn Discussion forum to chat with other Learners.