 Earlier we learned about the List, today we will learn about the set. In Python, a Set is a collection of unordered data or we can say the set is a collection of heterogeneous data. Inserted data are stored in curly brackets. Basically SET are used for performing the mathematical operation like UNION, INTERSECTION, DIFFERENCES, COMPLEMENT.

## Creating a dataset in Python

if you want to create the empty set below is the syntax.

```mset=set() # we are creating an empty set with name mset.
```

But if you want to give value to the set than you have to use the curly brackets.

```mset={1,2,3,4,5,6,7} # we can store multiple values, each value is seperated by comma.
```

• In List, we have indexed for data but in SET data do not have an index.
• SET elements cannot access by the index.
• SET does not support slicing.
• SET cannot contain Duplicate value.

#### How to create the set

```mset={1,2,3,4,5,6,7,8,9}
```

## How to add the value to set

To add the value add() is the method, here we have set now we will add 12 in the set. But the element will be added at the last.

```mset={1,2,3,4,5,6,7,8,9}
print(mset)
```

Output

```mset={1,2,3,4,5,6,7,8,9,12}
```

## How to access the set value

As we know set does not have index value so we cannot access the particular value directly. We can access them by looping the SET.

```mset={1,2,3,4,5,6,7,8,9}
for x in mset:
print(x)
```

Output

```1
2
3
4
5
6
7
8
9
```

## How to remove the value from SET

We have three methods to remove the element from SET.
a.) remove()
c.) pop()

Important: There is a difference between remove and discard method. If we are removing an element from the Set and the element is not found in the set then remove() method will give Error, but the discard() method will not return any error. We also have a pop() method to remove an element from SET, the POP() method will remove the first element from the SET.

```mset={1,2,3,4,5,6,7,8,9}
mset.remove(2)
print(mset)

#Output
mset={1,3,4,5,6,7,8,9}

mset={1,2,3,4,5,6,7,8,9}
print(mset)

#Output
mset={1,3,4,5,6,7,9}

mset={1,2,3,4,5,6,7,8,9}
mset.POP()
print(mset)

#Output
mset={2,3,4,5,6,7,8,9}
```

## How to Clear the SET

To Clear the set we can use the clear() method. It will clear the element and return the empty SET.

```mset={1,2,3,4,5,6,7,8,9}
mset.clear()
print(mset)

#Output
set()
```

## How to perform the UNION

To combine the two set we can use the union() method, but union() method will remove the duplicate data. Here Suppose we have two SET.

```mset={1,2,3,4,5,6,7,8,9}
cset={2,4,3,6,7,8,2,87,22}
print(mset.union(cset))

#Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 22, 87}

# Here we are performing union on mset Union cset.same we can perform for cset union mset.

mset={1,2,3,4,5,6,7,8,9}
cset={2,4,3,6,7,8,2,87,22}
print(cset.union(mset))

#Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 22, 87}
```

## Intersection in Python

If we are performing Intersection on two SET. It will give the common value of the SET.

```mset={1,2,3,4,5,6,7,8,9}
cset={2,4,3,6,7,8,2,87,22}
print(cset.intersection (mset))
```

## Difference  in Python

The difference will return the value which is not common in Both, But it will give the common value from the first SET.

```mset={1,2,3,4,5,6,7,8,9}
cset={2,4,3,6,7,8,2,87,22}
print(cset.difference  (mset))
#Output:
{22, 87}
```

## Other Methods in SET

• min()
• max()
• sum()

Here you can use the above method to find the minimum values, the max value you can also perform the sum, for using above we have followed one Rule that is set have the number value only.

Important: If you want to perform the math operation than you have to include the Math Library.

import math

example: math.sign(45)

```math.sign(cset)
```