Sets - Learn Python 3 - Snakify

# Lesson 10. Sets

## 1.What is a set

Set in Python is a data structure equivalent to sets in mathematics. It may consist of various elements; the order of elements in a set is undefined. You can add and delete elements of a set, you can iterate the elements of the set, you can perform standard operations on sets (union, intersection, difference). Besides that, you can check if an element belongs to a set.

Unlike arrays, where the elements are stored as ordered list, the order of elements in a set is undefined (moreover, the set elements are usually not stored in order of appearance in the set; this allows checking if an element belongs to a set faster than just going through all the elements of the set).

Any immutable data type can be an element of a set: a number, a string, a tuple. Mutable (changeable) data types cannot be elements of the set. In particular, list cannot be an element of a set (but tuple can), and another set cannot be an element of a set. The requirement of immutability follows from the way how do computers represent sets in memory.

## 2.How to define a set

You can define a set as simple as by naming all of its elements in brackets. The only exception is empty set, which can be created using the function set(). If set(..) has a list, a string or a tuple as a parameter, it will return a set composed of its elements. For example,

A = {1, 2, 3}
A = set('qwerty')
print(A)

will print {'e', 'q', 'r', 't', 'w', 'y'} as the output.

The order of elements is unimportant. For example, the program

A = {1, 2, 3}
B = {3, 2, 3, 1}
print(A == B)

will print True, because A and B are equal sets.

Each element may enter the set only once. set('Hello') returns the set of four elements: {'H', 'e', 'l', 'o'}.

## 3.Operations with elements

You can get the number of elements in the set using the function len.

You can also iterate over all the elements of the set (in an undefined order!) using the loop for:

primes = {2, 3, 5, 7, 11}
for num in primes:
print(num)

You can check whether an element belongs to a set using the keyword in: expressions like a in A return a value of type bool. Similarly there's the opposite operation not in. To add an element to the set there is the method add:

A = {1, 2, 3}
print(1 in A, 4 not in A)