Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create a set in Python

Getting Started with Sets in Python

Python, one of the most user-friendly programming languages, provides a variety of data structures to make data manipulation easier. Today, we will be focusing on an integral part of Python's data structures: the 'Set'.

Think of a set as a bag full of distinct items. It's like having a collection of unique trading cards. No matter how many times you add the same card, it will only count as one. This is the main feature of a set - it doesn't allow duplicates.

Understanding the Syntax

Creating a set in Python is quite straightforward. We use curly brackets {} to define a set. Let's take a look at an example:

my_set = {1, 2, 3, 4, 5}
print(my_set)

When you execute this code, Python will print the set:

{1, 2, 3, 4, 5}

This is a simple Python set containing distinct integers. Note that sets are unordered; the items will appear in no specific order.

Adding Elements to a Set

To add an element to a set, we use the add() method. It's like adding another unique trading card to your collection. Here’s how it's done:

my_set.add(6)
print(my_set)

Executing this code will give:

{1, 2, 3, 4, 5, 6}

See how the number 6 was added to our set? That's the add() method at work.

Removing Elements from a Set

Just as we can add elements to a set, we can also remove them using the remove() method. It's like taking a card out of your trading card collection. Here's an example:

my_set.remove(6)
print(my_set)

When you run this code, Python will print:

{1, 2, 3, 4, 5}

The number 6, which we added earlier, is now removed from our set.

Checking if an Item Exists

If you want to check if a certain card is in your collection, you can do so using the in keyword. Let's say we want to check if the number 3 is in our set:

print(3 in my_set)

Python will output True if the element is in the set, and False otherwise.

Union of Sets

Sometimes, we may want to combine two sets of trading cards. In Python, we can do this using the union() method or the | operator. For instance:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1.union(set2)
print(set3)

Executing this code will give:

{1, 2, 3, 4, 5}

Despite the number 3 being in both sets, in the resultant set it appears only once because sets only hold unique elements.

Intersection of Sets

If you want to find the common items between two sets (like finding common cards in two collections), you can use the intersection() method or the & operator. Here's how:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = set1.intersection(set2)
print(set3)

Running this code gives:

{3}

The number 3 is the only common element in both sets.

Conclusion

And that's it! You've now learned how to create and manipulate sets in Python. They are quite similar to collections of trading cards, aren't they?

Remember that the beauty of programming is not just in understanding the concepts but also in applying them creatively to solve problems. So, the next time you're dealing with a collection of unique items in Python, you know a set can be your go-to data structure.

Python sets are like a magic trick up your sleeve, ready to impress with their unique abilities when the situation calls for it. After all, every magician needs their bag of tricks, and in the world of Python, sets are one trick you can't afford to miss!