Path: blob/master/lessons/lesson_02/code/python-foundations/python-movies-lab.ipynb
1904 views
Python Review With Movie Data
_Author: Kiefer Katovich and Dave Yerrington (San Francisco)
In this lab, you'll be using the IMDb movies
list below as your data set.
This lab is designed to help you practice iteration and functions in particular. The normal questions are more gentle, and the challenge questions are suitable for advanced/expert Python students or those with programming experience.
All of the questions require writing functions and using iteration to solve. You should print out a test of each function you write.
1) Load the provided list of movies
dictionaries.
2) Filtering data by IMDb score.
2.1)
Write a function that:
Accepts a single movie dictionary from the
movies
list as an argument.Returns
True
if the IMDb score is greater than 5.5.
2.2 [Challenge])
Write a function that:
Accepts the
movies
list and a specified category.Returns
True
if the average score of the category is higher than the average score of all movies.
3) Creating subsets by numeric condition.
3.1)
Write a function that:
Accepts the list of movies and a specified IMDb score.
Returns the sublist of movies that have scores greater than the one specified.
3.2 [Expert])
Write a function that:
Accepts the
movies
list as an argument.Returns the
movies
list sorted first by category and then by movie according to category average score and individual IMDb score, respectively.
4) Creating subsets by string condition.
4.1)
Write a function that:
Accepts the
movies
list and a category name.Returns the movie names within that category (case-insensitive!).
If the category is not in the data, prints a message that says it does not exist and returns
None
.
Recall that, to convert a string to lowercase, you can use:
4.2 [Challenge])
Write a function that:
Accepts the
movies
list and a "search string."Returns a dictionary with the keys
'category'
and'title'
whose values are lists of categories that contain the search string and titles that contain the search string, respectively (case-insensitive!).
5) Multiple conditions.
5.1)
Write a function that:
Accepts the
movies
list and a "search criteria" variable.If the criteria variable is numeric, return a list of movie titles with a score greater than or equal to the criteria.
If the criteria variable is a string, return a list of movie titles that match that category (case-insensitive!). If there is no match, return an empty list and print an informative message.
5.2 [Expert])
Write a function that:
Accepts the
movies
list and a string search criteria variable.The search criteria variable can contain within it:
Boolean operations:
'AND'
,'OR'
, and'NOT'
(can have/be lowercase as well, we just capitalized for clarity).Search criteria specified with the syntax
score=...
,category=...
, and/ortitle=...
, where the...
indicates what to look for.If
score
is present, it indicates scores greater than or equal to the value.For
category
andtitle
, the string indicates that the category or title must contain the search string (case-insensitive).
Return the matches for the search criteria specified.