</> Code Editor { } Code Formatter

Python Sets Exercises


1/20

Python Sets Practice Questions

Correct
0%

Which of the following statements about Python sets is correct?


Understanding Python Sets

Sets are a built-in Python data type that represents an **unordered collection of unique elements**.

  • Option 1 – Incorrect: Sets cannot contain duplicates; any repeated elements are automatically removed.
  • Option 2 – Incorrect: Sets are unordered; the insertion order is not preserved.
  • Option 3 – Incorrect: Sets do not support indexing or slicing like lists.
  • Option 4 – Correct: Sets automatically remove duplicates and do not maintain element order.

Example:

my_set = {1, 2, 2, 3}
print(my_set)  # Output: {1, 2, 3} (duplicates removed, order not guaranteed)

Key takeaways:

  • Sets store only unique elements.
  • They are unordered, so indexing and slicing are not possible.
  • Useful for membership checks, removing duplicates, and set operations.

Quick Recap of Python Sets Concepts

If you are not clear on the concepts of Sets, you can quickly review them here before practicing the exercises. This recap highlights the essential points and logic to help you solve problems confidently.

What is a Python Set?

A Python set is an unordered collection of unique elements. Sets do not allow duplicates, and the elements cannot be accessed by index. Elements must be hashable (immutable). Sets are commonly used to store distinct items and perform mathematical set operations.

Creating Python Sets

You can create a set using curly braces or the set() constructor:

# Using curly braces
gems = {"onyx", "opal", "amethyst", "jade"}

# Using the set() constructor
numbers = set([11, 22, 33, 44])
letters = set("abracadabra")  # duplicates automatically removed

# Empty set
empty_set = set()

Accessing / Iterating Elements of Python Set

You can loop through a set and check membership:

exotic_fruits = {"durian", "rambutan", "salak", "longan"}
for fruit in exotic_fruits:
    print(fruit)

# Check membership
if "durian" in exotic_fruits:
    print("Durian is in the set")

Adding and Removing Elements in Python Set

You can add single or multiple elements and remove them safely:

planets = {"Mercury", "Venus", "Earth"}
planets.add("Mars")
planets.update(["Jupiter", "Saturn"])
planets.remove("Venus")
planets.discard("Pluto")  # safe even if not present
print(planets)

Set Operations: Union, Intersection, Difference, Symmetric Difference

Sets support mathematical operations to combine or compare elements:

A = {"lion", "tiger", "leopard"}
B = {"tiger", "cheetah", "jaguar"}

print(A.union(B))        # {'lion', 'tiger', 'leopard', 'cheetah', 'jaguar'}
print(A & B)             # {'tiger'} — intersection
print(A - B)             # {'lion', 'leopard'} — difference
print(A ^ B)             # {'lion', 'leopard', 'cheetah', 'jaguar'} — symmetric difference

Removing Duplicates from a List Using a Set

Sets can be used to quickly remove duplicates from a list:

animals_list = ["koala", "panda", "koala", "lemur", "panda"]
unique_animals = set(animals_list)
print(unique_animals)  # {'lemur', 'panda', 'koala'}

Converting Sets Back to Lists

You can convert a set to a list if you need indexing or sorting:

numbers_set = {5, 10, 15, 20}
numbers_list = list(numbers_set)
print(numbers_list)  # [5, 10, 15, 20]

Advantages of Python Sets

  • Automatically remove duplicates
  • Support fast membership testing
  • Support mathematical set operations (union, intersection, difference)
  • Can store heterogeneous, immutable elements
  • Useful for deduplication, comparisons, and group operations

Limitations of Sets

  • Unordered — cannot rely on element order
  • Elements must be hashable (immutable)
  • No indexing or slicing — convert to a list if needed

Summary: Key Takeaways

  • Sets are unordered collections of unique elements
  • Efficient for membership testing and set operations
  • Ideal for deduplication, comparisons, and fast lookups
  • Not suitable when order or indexing is required


About This Exercise: Python Sets Exercises

Welcome to Solviyo’s Python Sets exercises, designed to help you master one of Python’s most powerful and versatile data structures. Sets allow you to store unique items, perform fast membership tests, and execute operations like union, intersection, and difference efficiently. These exercises provide hands-on practice and interactive MCQs to make learning practical and engaging.

What You Will Learn

In this set of Python Sets exercises, you will explore:

  • Creating sets and understanding their uniqueness compared to lists and tuples.
  • Adding and removing elements, and working with mutable and immutable set items.
  • Performing essential set operations including union, intersection, difference, and symmetric difference.
  • Applying sets to solve real-world problems such as merging datasets, removing duplicates, and comparing collections.
  • Understanding common mistakes, such as indexing sets, and best practices for efficient set usage.
  • Reinforcing learning through interactive MCQs alongside practical exercises.
  • Developing confidence in applying Python sets in coding interviews, projects, and professional tasks.

Why Learning Python Sets Matters

Sets are an essential part of Python programming for handling collections of unique items efficiently. Mastering sets allows you to perform complex data operations quickly, write cleaner code, and tackle real-world programming challenges with confidence. Understanding set operations prepares you for advanced topics and coding interviews.

Start Practicing Python Sets Today

By completing these exercises, you will gain hands-on experience in manipulating Python sets and applying them in practical scenarios. Each exercise includes detailed explanations to help you understand the reasoning behind solutions. Start practicing Python Sets exercises now, and strengthen your Python programming skills while mastering this essential data structure.