coding180 icon
Coding180
Learn how to work with sets in Python, including creating them, adding and removing elements, and pe

Learn how to work with sets in Python, including creating them, adding and removing elements, and pe

Home > Tutorials > Python for beginners > Data structure


If you're new to coding in Python, you might be wondering what sets are and how they work. In this article, we'll explore the basics of sets, including how to create them, add and remove elements, and perform set operations like union, intersection, and difference.

What is a Set?

In Python, a set is an unordered collection of unique elements. This means that each element in a set is distinct from all others. Sets are an important data structure in Python because they allow you to perform operations like union, intersection, and difference on your data.

Creating Sets

To create a set in Python, you can use the set() function or the curly braces {}. If you're using the curly braces, make sure not to confuse them with dictionaries which also use curly braces but have key-value pairs inside.

# Using set() function
my_set = set()
print(my_set)

# Using curly braces
my_set = {1, 2, 3}
print(my_set)

In this example, we created an empty set using the set() function and a set with three elements using the curly braces {}. Note that we separated the elements with commas.

Adding and Removing Elements

To add an element to a set, you can use the .add() method. To remove an element, you can use the .remove() method.

my_set = {1, 2, 3}

# Adding an element
my_set.add(4)
print(my_set)

# Removing an element
my_set.remove(2)
print(my_set)

In this example, we added the element 4 to our set using the .add() method and removed the element 2 using the .remove() method.

Set Operations

Now that we know how to create and modify sets, let's explore some set operations. Here are some common set operations:

  • Union: combines all the elements from two sets.
  • Intersection: finds the common elements between two sets.
  • Difference: finds the elements in one set that are not in the other set.
  • Subset: checks if one set is a subset of another set.
set_a = {1, 2, 3}
set_b = {2, 3, 4}

# Union
union = set_a.union(set_b)
print(union)

# Intersection
intersection = set_a.intersection(set_b)
print(intersection)

# Difference
difference = set_a.difference(set_b)
print(difference)

# Subset
subset = set_a.issubset(set_b)
print(subset)

In this example, we performed each set operation on the sets set_a and set_b. We printed out the result of each operation using print().

Examples and Exercises

Now that you know how to work with sets, let's try some examples and exercises to solidify your understanding.

Example 1

You have two sets:

set_a = {"apple", "banana", "orange"}
set_b = {"banana", "kiwi", "pineapple"}

What are the common elements between the two sets?

common_elements = set_a.intersection(set_b)
print(common_elements)

Output: {'banana'}

Exercise 1

Create a set containing the numbers 1 through 10. Remove 5 from the set and add 11. Print out the resulting set.

Exercise 2

You have two sets:

set_a = {1, 2, 3}
set_b = {2, 3, 4}

What are the elements that are in set_a but not in set_b?

Conclusion

Congratulations! You now have a better understanding of sets and how to perform operations on them. Keep practicing with more examples and exercises to master this data structure. The ability to work with sets will be invaluable as you


user

Robort Gabriel

Lagos, Nigeria

Freelance Web Developer, Native Android Developer, and Coding Tutor.

Skills
  • UI / UX
  • JavaScript
  • Python
  • PHP
  • Kotlin
  • Java
  • Bootrap
  • Android
  • Laravel