-
What is the output of this code?
my_set = {1, 2, 3, 3, 4, 5, 5} print(my_set)
-
Suppose you are building an e-commerce platform, and you want to keep track of unique product categories. Initially, you have a set of existing categories. Let’s say:
product_categories = {“Electronics”, “Clothing”, “Home Decor”, “Books”}Now, a new product category, “Sports & Fitness,” is introduced to your platform. Add it to the set of existing categories.
product_categories = {"Electronics", "Clothing", "Home Decor", "Books"} product_categories.''("Sports & Fitness")
-
Let’s consider a task management application where you have a set of assigned tasks for a project. Initially, the set contains several tasks. For instance:
assigned_tasks = {“Task 1”, “Task 2”, “Task 3”, “Task 4”, “Task 5”}Now, let’s say that “Task 3” has been completed and you need to remove it from the set.
assigned_tasks = {"Task 1", "Task 2", "Task 3", "Task 4", "Task 5"} assigned_tasks.''("Task 3") print(assigned_tasks)
-
Let’s imagine you are building a quiz application, and you have a set of questions for a particular category. Initially, the set contains multiple questions. For instance:
questions = {“Question 1”, “Question 2”, “Question 3”, “Question 4”, “Question 5”}Now, when a user starts the quiz, you want to randomly retrieve and remove a question from the set.
questions = {"Question 1", "Question 2", "Question 3", "Question 4", "Question 5"} random_question = questions.''() print("Random Question:", random_question) print("Remained questions: ", questions)
-
Suppose you are developing a bookmarking application where users can save their favorite websites. You are maintaining a set of URLs representing the user’s bookmarks. Initially, the set contains several bookmarked URLs:
bookmarks = {“https://example1.com”, “https://example2.com”, “https://example3.com”, “https://example4.com”, “https://example5.com”}Now, let’s say a user wants to clear their bookmarks, either because they want to start fresh or they no longer need their saved websites.
bookmarks = {"https://example1.com", "https://example2.com", "https://example3.com", "https://example4.com", "https://example5.com"} bookmarks.''() print(bookmarks)
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) – Telegram – Linkedin – Facebook – or on your favorite platform…