Building a Media Collection Program in Python

Building a Media Collection Program in Python

Play this article

Introduction

Welcome to our Python tutorial! In this guide, we'll build a "Media Collection" program using Python. The program allows you to manage and explore your favorite anime and TV series. We'll cover data structures, functions, user input, and more.

Prerequisites

Before we get started, make sure you have Python installed on your computer. You can download the latest version from the official Python website (https://www.python.org/).

1. Basic Setup

1.1 Libraries Used

For this project, we'll only use Python's built-in functions and data structures. No external libraries are required.

1.2 Data Structure

We'll use a list of dictionaries to represent the media collection. Each dictionary corresponds to a single media entry and contains key-value pairs for attributes like title, type, episodes/duration, genre, and rating.

2. Adding Media

2.1 add_media() Function

To add media to our collection, we'll create a function named add_media(). This function will take five parameters: title, media_type, episodes_or_duration, genre, and rating.

def add_media(title, media_type, episodes_or_duration, genre, rating):
    media_entry = {
        "title": title,
        "type": media_type,
        "episodes_or_duration": episodes_or_duration,
        "genre": genre,
        "rating": rating,
    }
    media_collection.append(media_entry)

2.2 Example Usage

Now, let's add some anime and TV series to our media collection:

add_media("Naruto", "Anime", 220, ["Action", "Adventure", "Shounen"], 8.2)
add_media("Breaking Bad", "TV Series", 62, ["Drama", "Crime", "Thriller"], 9.5)

3. Searching Media

3.1 search_by_title() Function

Next, we want to search for media by its title. We'll create a function called search_by_title() that takes the title as input and returns a list of matching media entries (case-insensitive).

def search_by_title(title):
    matches = [media for media in media_collection if media["title"].lower() == title.lower()]
    return matches

3.2 search_by_type() Function

We'll also implement a function called search_by_type() to search for media by its type (either "Anime" or "TV Series").

def search_by_type(media_type):
    matches = [media for media in media_collection if media["type"].lower() == media_type.lower()]
    return matches

3.3 search_by_genre() Function

To search media by genre, we'll create the search_by_genre() function.

def search_by_genre(genre):
    matches = [media for media in media_collection if genre.lower() in [g.lower() for g in media["genre"]]]
    return matches

3.4 search_by_rating() Function

Finally, we'll implement search_by_rating() to search for media with a minimum rating.

def search_by_rating(min_rating):
    matches = [media for media in media_collection if media["rating"] >= min_rating]
    return matches

3.5 Example Usage

Now, let's search for media by different criteria:

# Search media by title
naruto_results = search_by_title("Naruto")

# Search media by type
anime_results = search_by_type("Anime")

# Search media by genre
comedy_tv_series_results = search_by_genre("Comedy")

# Search media by minimum rating
high_rated_results = search_by_rating(9.0)

4. Displaying Media Details

4.1 display_media() Function

To display media details, we'll create the display_media() function. This function takes a list of media entries as input and prints their attributes.

def display_media(media):
    for item in media:
        print("Title:", item["title"])
        print("Type:", item["type"])
        if item["type"].lower() == "anime":
            print("Episodes:", item["episodes_or_duration"])
        else:
            print("Duration (minutes):", item["episodes_or_duration"])
        print("Genre:", ", ".join(item["genre"]))
        print("Rating:", item["rating"])
        print("=" * 30)

4.2 Example Usage

Now, let's display the details of the media we searched for:

display_media(naruto_results)
display_media(anime_results)
display_media(comedy_tv_series_results)
display_media(high_rated_results)

Conclusion

Congratulations! You've successfully built the "Media Collection" program in Python. You can now manage and explore your favorite anime and TV series with ease. Feel free to enhance the program with additional features or customize it according to your preferences.


This tutorial blog walks you through building the "Media Collection" program step-by-step. You've learned about data structures, functions, searching, and displaying media details. Feel free to experiment with the code and explore more functionalities for your media collection management. Happy coding!