# Python. Sets. Basic concepts. Creating a set. Operation in. The set operations to create a new set

## Sets. Basic concepts. Creating a set. Operation in. The set operations to create a new set

### Contents

Search other websites:

##### 1. The concept of a set

The Python programming language has tools for working with sets. In Python, a set is an unordered collection of elements with the following features:

• all elements of the set are unique. In other words, the set cannot have two identical elements;
• the elements of the set are immutable objects;
• elements of the set can be of different types.

In Python, a set is represented by a separate data type. Sets support the operations that exist in set theory from a course in mathematics.

The set can be empty (not contain elements).

Examples of sets as literals.

```# a set of 3 elements: 'a', 5, 'textbook'
{'а', 5, "textbook"}

# a set containing numbers from 0 to 5
{0,1,2,3,4,5}

# empty set
{ }```

### ⇑

##### 2. Creating an object of type set. Function set(). Examples

To create an object of type “set” you can use one of ways:

• by assigning to this object a set of elements in curly braces { };
• by assigning to the object some result that was returned by the set() function. The set() function allows you to convert objects of other types into a set;
• by assigning to the object the result of the frozenset() function. The frozenset() function is the constructor of the frozenset class. This function allows you to get an immutable set.
• by assigning to the object the result of some operation on sets (for example, the operation & intersection of sets);
• by assigning to the object the result of a function that returns some set.

Example.

```# Sets
# Creating an object of type 'set'

# 1. Using curly braces {}
x = { 0, 0, 1, 2, 3 } # x = {0, 1, 2, 3} - elements in the set are not repeated
y = { 'a', "textbook", 5 } # y = {'a', 'textbook', 5}
z = { True, False, 0.25, 'Hello' } # z = {False, True, 'Hello', 0.25}

# 2. Using the set() function
x = set(['a', 5, "textbook", 5]) # x = {5, 'textbook', 'a'}
y = set('Hello!') # y = {'l', 'o', 'e', '!', 'H'}
z = set([0,1,2,3,4,5]) # z = {0, 1, 2, 3, 4, 5}

# 3. Using some operation
x = { 1, 2, 3, 4 } & { 3, 2} # x = {2, 3}
y = { 1, 2, 3, 4 } - { 1, 4} # y = {2, 3}
z = { 'a', 'c', 5 } | { True } # z = {'a', 5, 'c', True}

# 4. Using the frozenset() function
x = frozenset([1, 2, 3, 4]) # x - immutable set

# 5. Using the function
x = { 1, 2, 3, 4 }
y = { 3, 5, 7 }
z = x.union(y) # z = { 1, 2, 3, 4, 5, 7 }```

### ⇑

##### 3. Basic operations on sets. List

Python supports the following set operations:

• in – check the element for entry into the set;
• (minus) is the difference of the sets;
• | – union of sets;
• & – intersection of sets;
• ^ – symmetric difference.

### ⇑

##### 4. Operations in, not in. Checking an item for entry. Example

The in operation is used in cases where it is necessary to determine whether an element is included in a set. The general form of the operation is as follows:

`Item in Set`

where

• Item – some object (element) that needs to be checked for occurrence in the set Set. If the Item object is in the Set, then the result of the operation will be True. Otherwise, the result of the operation will be False;
• Set – some set.

Typically, the in operation is used in condition checking statements.

In programs, the in operator can be combined with other logical operators, for example, the not operator. In this case, the general form of the not in statement is as follows

`Item not in Set`

The result of the not in operation is always the opposite of the result of the in operation.

Example 1.

```# Sets
# Operation in. Checking the entry of an element in a set

# 1. The in operation
x = { 3, 8, 'а', 'Hello' } # the set of elements
a = 'Hello' # element

if a in x:
print('The element is in set')
else:
print('The element is not in set')```

Example 2. Combining the in operator with the logical operator not.

```# Using the 'not in' statement
day = { 'Sun', 'Mon', 'Tue', 'Wed', 'Thi', 'Fri', 'Sat'}

# Enter the day using the keyboard
inputDay = str(input('Input day of week: '))

# Checking
if inputDay not in day:
print('Wrong day')
else:
print('OK')```

The result of the program

```Input day of week: sunday
Wrong day```

### ⇑

##### 5. Operation –. The difference of sets. Example

In Python, an operation of sets difference is indicated by the (minus) symbol. In addition, the difference() function is implemented in Python to determine the difference of sets.

The general form of the operation is as follows:

`set1 – set2`

where

• set1 – the set from which the elements located in the set2 set are subtracted (deleted). When subtracting, only those elements are considered that are common (identical) to two sets. As a result, a new set is created containing the elements of set1, which are not in set2;
• set2 – a set defining the elements to be subtracted (deleted) from the set1.

The set difference operation may form chains. In this case, the general form of the operation is as follows:

`set1 - set2 - set3 - ... - setN`

Example.

```# Sets
# Operation -. The difference of sets

# Elements of the same type
x = { 1, 2, 3, 5 }
y = { 2, 8 }
z = x - y # z = {1, 3, 5}
v = y - x # v = {8}

# Elements of different types.
x = { 'a', True, 'Text' }
y = { 2, 8, 16.23, 'a' }
z = x - y # z = {True, 'Text'}```

### ⇑

##### 6. Operation |. The union of sets. Example

The union of the sets a and b is the formation of such a set c whose elements are in the set a or in the set b. In Python, you can use the | operation to implement union of sets or union() function.

In Python, you can use the | operation to implement union of sets or union() function.

Operation of union of sets | has the following general form

`set1 | set2`

where set1, set2 – some sets are combined. The resulting set contains all the elements of set1 and all elements of set2.

Operation of union of sets | can form chains as shown below

`set1 | set2 | set3 | ... | setN`

Example.

```# Sets
# Operation |. Union of sets
x = { 'a', 5, True }
y = { 2, 3.85, False }
z = x | y # z =   {3.85, True, False, 2, 5, 'a'}
v = x | y | { 170 } # v = {3.85, True, False, 2, 5, 'a', 170}```

### ⇑

##### 7. Operation &. Intersection of sets. Example

The intersection of two sets a, b is a set c that contains common elements of the sets a, b. Each element of c is located in a as well as in b.

Python uses the & operation or the intersection() function to implement set intersection. The intersection operation of sets & has the following general form

`set1 & set2`

here set1, set2 – sets for which you need to implement the so-called “intersection”. The result is a new set that contains elements that are included in both sets set1 and set2. Elements that are included in only one of the sets set1, set2 are truncated.

The intersection operation of sets & can form chains

`set1 & set2 & set3 & ... & setN`

Example.

```# Sets
# Operation &. Intersection of sets

# Result - empty set
x = { 'а', 5, True }
y = { 2, 3.85, False }
z = x & y # z =   {} - empty set

# The result is a nonempty set
x = { 3, 2.25, True, 'abc' }
y = { 2.25, 'abcd', 3, 7, True}
z = x & y # z = {True, 2.25, 3}```

### ⇑

##### 8. Operation ^. Symmetric difference. Example

To get the symmetric difference of sets in Python, you can use the operation ^ or the function symmetric_difference().

The operation of a symmetric difference of sets is the inverse to the operation of intersecting sets &. In this case, elements that are not common are included in the result set.

The general form of the operation is as follows

`set1 ^ set2`

where set1, set2 – sets for which a symmetric difference is realized. The result is a new set containing elements that are not common to the sets set1, set2.

Operation ^ can form chains

`set1 ^ set2 ^ set3 ^ ... ^ setN`

Example.

```# Sets
# Operation ^. Symmetric difference of sets

# The result is an empty set, no common elements
x = { 1, 3, 5 }
y = { 3, 5, 1 }
z = x ^ y # z = {} - empty set

# The value 3 in sets is common, therefore it is not included in the result
x = { 1, 3, 5 }
y = { 2, 3, 8 }
z = x ^ y # z = {1, 2, 5, 8} - value 3 is missed

# The values 'b', 'c' are common, so they are thrown out of the result
x = { 'a', 'b', 'c' }
y = { 'x', 'b', 'c', 'd', 'e' }
z = x ^ y # z = {'e', 'a', 'd', 'x'}```

### ⇑

##### 9. An example of using sets to filter lists

The example defines the numbers in the list. A set property is used that indicates that an element cannot be repeated several times in a set.

```# Using sets to filter lists
# 1. Create list of numbers from 1 to 10
List = [5, 3, 2, 3, 4, 2, 3, 7, 9, 5, 7]

# 2. Determine which numbers from 1 to 10 are on the List
numbers = set(List) # numbers = [2, 3, 4, 5, 7, 9]

# 3. Display the result
print('numbers = ', numbers)```

The result of the program

`numbers = [2, 3, 4, 5, 7, 9]`