Python – Sets

Python – Sets

The set is one of the built-in data types in Python. Read this article to learn more about the Python Sets.

However, in mathematics, a set is the collection of data types including a list or tuple. A set object consists of a collection of one or more immutable objects that will be enclosed by the curly brackets{}.

What is Set in Python?

The Python set will allow you to store multiple items in a single variable. Thus, the set is among the 4 built-in data types in Python that will store the collection of data.

Furthermore, the Python set will indicate an unordered, unchangeable, and unindexed collection. It won’t allow any duplicate values. In simple terms, unordered will refer to the items with no defined order in the Python sets. So, it will show in a different order each time you need to use them and won’t be referred by the index or key. The set items are unchangeable.

What are the examples of Python sets?

Let us look into examples of Python sets provided below:

# Creating two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# Output: Elements of set1
print("Set 1:", set1)

# Output: Elements of set2
print("Set 2:", set2)

# Adding an element to set1
set1.add(6)
# Output: Updated set1
print("After adding 6 to set 1:", set1)

# Removing an element from set2
set2.remove(8)
# Output: Updated set2
print("After removing 8 from set 2:", set2)

# Union of set1 and set2
union_set = set1.union(set2)
# Output: Union of set1 and set2
print("Union of set1 and set2:", union_set)

# Intersection of set1 and set2
intersection_set = set1.intersection(set2)
# Output: Intersection of set1 and set2
print("Intersection of set1 and set2:", intersection_set)

# Difference between set1 and set2
difference_set = set1.difference(set2)
# Output: Difference between set1 and set2
print("Difference of set1 and set2:", difference_set)

# Checking if set1 is a subset of set2
is_subset = set1.issubset(set2)
# Output: Whether set1 is a subset of set2
print("Is set1 a subset of set2:", is_subset)

Output

Set 1: {1, 2, 3, 4, 5}
Set 2: {4, 5, 6, 7, 8}
After adding 6 to set 1: {1, 2, 3, 4, 5, 6}
After removing 8 from set 2: {4, 5, 6, 7}
Union of set1 and set2: {1, 2, 3, 4, 5, 6, 7}
Intersection of set1 and set2: {4, 5, 6}
Difference of set1 and set2: {1, 2, 3}
Is set1 a subset of set2: False

Example 2 – to illustrate that the duplicates are not allowed in Python sets

# Creating a set with duplicate elements
my_set = {1, 2, 3, 3, 4, 4, 5}

# Output: The set with duplicates
print("Set with duplicates:", my_set)

Output

Set with duplicates: {1, 2, 3, 4, 5}

Example 3- to determine the length of Python sets

Check out the example below to determine the length of Python sets

# Creating a set
my_set = {1, 2, 3, 4, 5}

# Getting the length of the set
set_length = len(my_set)

# Output: Length of the set
print("Length of the set:", set_length)

Output

Length of the set: 5

Example 4- to create the Python set with items of different data types

# Creating a set with items of different data types
mixed_set = {1, 2.5, 'apple', (3, 4)}

# Output: The set with different data types
print("Set with different data types:", mixed_set)

Output

Set with different data types: {1, 2.5, 'apple', (3, 4)}

What is the Set() Constructor?

The Set() constructor will help you to create the Python sets.

What is the example for a set constructor?

# Creating a set using the set constructor
my_set = set([1, 2, 3, 4, 5])

# Output: The created set
print("Created set using set constructor:", my_set)

Output

Created set using set constructor: {1, 2, 3, 4, 5}

Conclusion

In Python, sets are a powerful data structure that stores unique elements. They’re like lists, but they don’t allow duplicates. Sets are defined using curly braces {}, and you can create a set using either curly braces directly or the set() constructor.

Hence, a set will allow you to perform various operations with sets like adding elements, removing elements, finding intersections, unions, and differences, and checking for subsets.

Python – Sets – FAQs

Q1.What is set and subset in Python?

Ans. A subset indicates the collection of elements that will belong to another set. In contrast, the set is the collection of data types.

Q2.Are Python sets unique?

Ans. Yes, set elements are unique

Q3.Is set a class in Python?

Ans. Yes, in Python, a set is a built-in data type and is an unordered collection of unique elements. It is implemented as a class in Python.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment