5.4. Data Structures in Python: Sets

Página 18

In the Python programming language, data structures are a fundamental part of organizing and manipulating information. One such data structure is the set, also known as a 'set'. Sets are an unordered collection of unique objects. The main characteristic of sets is that they do not allow duplicates, which makes them ideal for operations such as union, intersection and difference.

To create a set in Python, we use the curly braces {} or the set() function. For example, we can create a set of numbers as follows:

numbers = {1, 2, 3, 4, 5}
or
numbers = set([1, 2, 3, 4, 5])

It is important to note that sets do not preserve the order of elements. Therefore, if you try to print the above set of numbers, the order of the elements may vary.

As mentioned earlier, one of the main uses of sets is performing set operations such as union, intersection, and difference. The union of two sets is a new set that contains all the elements of both sets. In Python, union can be performed using the | or the union() method. For example:

a = {1, 2, 3}
b = {3, 4, 5}
c = a | B
or
c = a.union(b)

The result will be the set {1, 2, 3, 4, 5}, which contains all elements of a and b.

The intersection of two sets is a new set that contains only those elements that are in both sets. In Python, intersection can be performed using the & operator or the intersection() method. For example:

a = {1, 2, 3}
b = {3, 4, 5}
c = a & b
or
c = a.intersection(b)

The result will be the set {3}, which contains only the elements that are in a and b.

The difference between two sets is a new set that contains the elements of the first set that are not in the second set. In Python, the difference can be performed using the - operator or the difference() method. For example:

a = {1, 2, 3}
b = {3, 4, 5}
c = a - b
or
c = a.difference(b)

The result will be the set {1, 2}, which contains the elements of a that are not in b.

In addition to these operations, sets in Python also support other operations such as adding and removing elements, checking whether an element belongs to the set, and so on. To add an element to a set, we use the add() method. To remove an element, we use the remove() method. If we want to check if an element belongs to a set, we can use the in operator.

In short, sets in Python are a powerful data structure that allow us to manipulate collections of elements efficiently. They are particularly useful when we need to perform set operations or when we want to ensure that a collection of elements does not contain duplicates.

Sets, like lists, dictionaries, and tuples, are an essential part of the Python language. Mastering these data structures is critical to becoming an effective and efficient Python programmer. Therefore, we hope that this chapter of our complete Python and Django System Building Course has given you a clear and concise understanding of Python assemblies.

Now answer the exercise about the content:

Which of the following statements about sets in Python is true?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

195.5. Data Structures in Python: Stacks

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text