In python, the set can be defined as the unordered collection of various items enclosed within the curly braces. The elements of the set cannot be duplicate. The elements of the python set must be immutable.

Unlike other collections in python, there is no index attached to the elements of the set, i.e., we cannot directly access any element of the set by the index. However, we can print them all together or we can get the list of elements by looping through the set.

The set can be created by enclosing the comma separated items with the curly braces.

`Set={value1, value2….}`

```
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
print(Days)
print(type(Days))
print("Looping through the set elements ... ")
for i in Days:
print(i)
```

```
{'Wednesday', 'Tuesday', 'Sunday', 'Friday', 'Thursday', 'Saturday', 'Monday'}
```
Looping through the set elements...
Wednesday
Tuesday
Sunday
Friday
Thursday
Saturday
Monday

In Python, we can perform various mathematical operations on python sets like union, intersection, difference, etc

Operator | Description |
---|---|

`|` |
Union Operator |

`&` |
Intersection Operator |

`-` |
Difference Operator: |

The union of two sets are calculated by using the or (|) operator. The union of the two sets contains the all the items that are present in both the sets.

```
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1 | Days2)
```

`{'Thr', 'Fri', 'Sun', 'Tue', 'Wed', 'Mon', 'Sat'}`

The & (intersection) operator is used to calculate the intersection of the two sets in python. The intersection of the two sets are given as the set of the elements that common in both sets.

```
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1 & Days2)
```

`{'Mon', 'Sat'}`

The difference of two sets can be calculated by using the subtraction (-) operator. The resulting set will be obtained by removing all the elements from set 1 that are present in set 2.

```
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1 - Days2)
```

`{'Tue', 'Wed'}`

Python contains the following methods to be used with the sets. Those are

- len(set)
- max(set)
- min(set)
- sum(set)
- sorted(set)
- set()
- add()
- update()
- discard()
- remove()
- pop()
- clear()
- union()
- intersection()
- difference()
- issubset()
- issuperset()

In Python len() is used to find the length of set,i.e it returns the number of items in the set.

`len(set)`

```
num={1,2,3,4,5,6}
print(“length of set :”,len(num))
```

`length of set : 6`

In Python max() is used to find maximum value in the set.

`max(set)`

```
num={1,2,3,4,5,6}
lang={'java','c','python','cpp'}
print("Max of set :",max(num))
print("Max of set :",max(lang))
```

```
Max of set : 6
Max of set : python
```

In Python min() is used to find minimum value in the set.

`min(set)`

```
num={1,2,3,4,5,6}
lang={'java','c','python','cpp'}
print("Min of set :",min(num))
print("Min of set :",min(lang))
```

```
Min of set: 1
Min of set : c
```

In python, sum(set) function returns sum of all values in the set. Set values must in number type.

`sum(set)`

```
num={1,2,3,4,5,6}
print(“sum of set items :”,sum(num))
```

`sum of set items: 21`

In python, sorted (set) function is used to sort all items of set in an ascending order. It also sorts the items into descending and ascending order. It takes an optional parameter 'reverse' which sorts the set into descending order.

`sorted (set[,reverse=True])`

```
num={1,3,2,4,6,5}
lang={'java','c','python','cpp'}
print(sorted(num))
print(sorted(lang))
print(sorted(num,reverse=True))
```

```
{1, 2, 3, 4, 5, 6}
{'c', 'cpp', 'java', 'python'}
{6, 5, 4, 3, 2, 1}
```

The set() method takes sequence types and converts them to sets. This is used to convert a given string or list or tuple into set.

`set(sequence)`

```
set1=set("PYTHON")
print(set1)
days=["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
set2 = set(days)
print(set2)
days=("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun")
set3 = set(days)
print(set3)
```

```
{'N', 'O', 'T', 'H', 'P', 'Y'}
{'Fri', 'Thur', 'Tue', 'Sun', 'Mon', 'Sat', 'Wed'}
{'Fri', 'Thur', 'Tue', 'Sun', 'Mon', 'Sat', 'Wed'}
```

In python, the add() method used to add some particular item to the set.

`set.add (item)`

```
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}
print("\n printing the original set ... ")
print(Days)
Days.add("Saturday");
Days.add("Sunday");
print("\n Printing the modified set...");
print(Days)
```

```
printing the original set ...
{'Wednesday', 'Friday', 'Thursday', 'Tuesday', 'Monday'}
Printing the modified set...
{'Wednesday', 'Sunday', 'Friday', 'Thursday', 'Tuesday', 'Saturday', 'Monday'}
```

Python provides the update () method to add more than one item in the set.

`set.update ([item1, item2…])`

```
Months={"Jan","Feb","Mar","Apr"}
print("\n Printing the original set ... ")
print(Months)
Months.update (["May","Jun","Jul"])
print("\n Printing the modified set...");
print(Months)
```

```
Printing the original set ...
{'Mar', 'Apr', 'Jan', 'Feb'}
Printing the modified set...
{'Mar', 'Apr', 'Jan', 'Jun', 'May', 'Jul', 'Feb'}
```

Python provides discard () method which can be used to remove the items from the set. If item doesn't exist in the set, the python will not give the error. The program maintains its control flow.

`set.discard (item)`

```
Months={"Jan","Feb","Mar","Apr"}
print("\n printing the original set ... ")
print(Months)
Months.discard("Apr")
print("\n Printing the modified set...");
print(Months)
Months.discard("May") #doesn’t give error
print("\n Printing the modified set...");
print(Months)
```

```
printing the original set ...
{'Jan', 'Apr', 'Mar', 'Feb'}
Printing the modified set...
{'Jan', 'Mar', 'Feb'}
Printing the modified set...
{'Jan', 'Mar', 'Feb'}
```

Python provides remove () method which can be used to remove the items from the set. If item doesn't exist in the set, the python will give the error.

`set.remove (item)`

```
Months={"Jan","Feb","Mar","Apr"}
print("\n printing the original set ... ")
print(Months)
Months.remove("Apr")
print("\n Printing the modified set...");
print(Months)
Months.remove("May") #it give error
print("\n Printing the modified set...");
print(Months)
```

```
printing the original set ...
{'Feb', 'Jan', 'Apr', 'Mar'}
Printing the modified set...
{'Feb', 'Jan', 'Mar'}
KeyError: 'May' doesn’t exist.
```

In Python, pop () method is used to remove the item. However, this method will always remove the last item.

`set.pop ()`

```
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}
print("\n printing the original set ... ")
print(Days)
Days.pop()
print("\n Printing the modified set...");
print(Days)
```

```
Printing the original set...
{'Monday', 'Wednesday', 'Friday', 'Tuesday', 'Thursday'}
Printing the modified set...
{'Wednesday', 'Friday', 'Tuesday', 'Thursday'}
```

In Python, clear () method is used to remove the all items in set.

`set.clear ()`

```
Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}
print("\n printing the original set ... ")
print(Days)
Days.clear()
print("\n Printing the modified set...");
print(Days)
```

```
printing the original set ...
{'Monday', 'Wednesday', 'Friday', 'Tuesday', 'Thursday'}
Printing the modified set...
set()
```

In Python, the union () method is used to perform union of two sets. The union of the two sets contains the all the items that are present in both the sets.

`set1.union (set2)`

```
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1.union(Days2))
```

`{'Thr', 'Fri', 'Sun', 'Tue', 'Wed', 'Mon', 'Sat'}`

In Python, the intersection () is used to calculate the intersection of the two sets in python. The intersection of the two sets is given as the set of the elements that common in both sets.

`set1.intersection (set2)`

```
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1.intersection(Days2))
```

`{'Mon', 'Sat'}`

The difference of two sets can be calculated by using the difference () method. The resulting set will be obtained by removing all the elements from set 1 that are present in set 2.

`set1.difference (set2)`

```
Days1={"Mon","Tue","Wed","Sat"}
Days2={"Thr","Fri","Sat","Sun","Mon"}
print(Days1.difference(Days2))
```

`{'Tue', 'Wed'}`

The issubset() method returns True if all elements of a set are present in another set (passed as an argument). If not, it returns False.

`set1.issubset (set2)`

```
set1={1,2,3,4}
set2={1,2,3,4,5,6,7,8,9}
print(set1.issubset(set2))
print(set2.issubset(set1))
```

```
True
False
```

The issuperset () method returns True if a set has every elements of another set (passed as an argument). If not, it returns False.

`set1.issuperset (set2)`

```
set1={1,2,3,4}
set2={1,2,3,4,5,6,7,8,9}
print(set1.issuperset(set2))
print(set2.issuperset(set1))
```

```
False
True
```

Next Topic :