Blog Detail

September 3, 2023

Python Sets Exercises

  • Imagine you are developing a scheduling application that allows users to create events and manage their availability. You have two sets representing the available time slots for two different users: User A and User B. Each set contains the time slots as integers:

    user_a_availability = {9, 10, 11, 12}
    user_b_availability = {14, 15, 16, 17}

    Now, you want to check if there are any overlapping time slots between User A and User B.

                    user_a_availability = {9, 10, 11, 12}
    user_b_availability = {14, 15, 16, 17}
    
    result = user_a_availability.''(user_b_availability)
    
    print("Are all the time slots available?", result)
  • Suppose you are developing a movie recommendation system. You have two sets containing the genres preferred by two different users: User A and User B. Each set represents the genres as strings:

    user_a_genres = {“Action”, “Drama”, “Comedy”, “Adventure”}
    user_b_genres = {“Comedy”, “Romance”, “Thriller”}

    Now, you want to retrieve the combined set of genres liked by both User A and User B.

                    user_a_genres = {"Action", "Drama", "Comedy", "Adventure"}
    user_b_genres = {"Comedy", "Romance", "Thriller"}
    
    all_genres = user_a_genres.''(user_b_genres)
    
    print(all_genres)
  • Imagine you are building a recommendation system for a movie streaming service. You have two sets representing the movie genres preferred by two different users: User A and User B. Each set contains the unique genre names:

    user_a_genres = {“action”, “drama”, “thriller”}
    user_b_genres = {“action”, “comedy”, “drama”, “romance”, “adventure”}

    Now, you want to check if User A’s preferred movie genres are a subset of User B’s preferred movie genres, meaning that all the genres liked by User A are also present in User B’s preferences.

                    user_a_genres = {"action", "drama", "thriller"}
    user_b_genres = {"action", "comedy", "drama", "romance", "adventure"}
    
    is_subset = user_a_genres.''(user_b_genres)
    
    print(is_subset)
  • Imagine you are developing a ticketing system for an event. You have two sets representing the attendees who purchased tickets for two different ticket categories: Category A and Category B. Each set contains the attendee names:

    category_a_attendees = {“John”, “Alice”, “Michael”, “Sarah”}
    category_b_attendees = {“Alice”, “Sarah”, “David”, “Emily”}

    Now, you want to identify the attendees who purchased tickets for Category A but not for Category B.

                    category_a_attendees = {"John", "Alice", "Michael", "Sarah"}
    category_b_attendees = {"Alice", "Sarah", "David", "Emily"}
    
    unique_attendees = category_a_attendees.''(category_b_attendees)
    
    print(unique_attendees)
  • Python Exercises for Sets

    • If you’ve faced difficulties with these exercises, take a look at Sets Module on my Python course.


    You can also share ‘Python Sets’ Exercises on your:
    X (Twitter)TelegramLinkedinFacebook – or on your favorite platform…




    Happy Coding!
    Behnam Khani