Set is an unordered collection of items. So, Every element is unique (no duplicates) and must be immutable.

then, The set itself is mutable (we can add or remove items).

So that, Use to perform mathematical operations like union, intersection, symmetric difference.

#### Syntax :

Firstly I have to open pycharm.

```
{ } # set use curly braces
set1 = {1,2,3}
set2 = {1,”helo”,3,11.45} //mixed data
try this
set3 = {1,3,5,’h’,1,’h’} ????
my_set = {1, 2, [3, 4]} ????
```

moreover, set cannot have mutable items.

But we can make set from a list,

```
>>> set([1,2,3,2])
{1, 2, 3}
```

## Empty set in Python.

at the last like the list we can not create the empty set like.

```
a = { }
type(a) //out put : <class 'dict'>
But for this we have to use set() function Like :
b=set()
type(b) // output : <class 'set'>
```

## Changing the set in Python :

indexing has no meaning. We cannot access or change an element of the set using indexing or slicing.

So, We can add single elements using the method add().

Then, Multiple elements can be added using the update() method.

So, The update() method can take tuples, lists, strings, or other sets as its argument.

moreover, In all cases, duplicates are avoided.

A particular item can be removed from the set using methods like discard() and remove().

While using discard() if the item does not exist in the set, it remains unchanged. But remove() will raise an error in such a condition.

We can remove and return an item using the **pop()** method.

Set being unordered, there is no way of determining which item will be popped. It is completely arbitrary.

We can also remove all items from a set using **clear()**.

## Set Operation in Python:

Set operations like **union, intersection, difference, and symmetric difference**.

then, We can do this with operators or methods.**Union:** Union is performed using** | **operator. So, The same can accomplish using the method **union().**

```
a = {1,2,3}
b = {2,5,7}
a | b // output : set([1, 2, 3, 5, 7])
a.union(b) // output : set([1, 2, 3, 5, 7])
b.union(a)
c = a|b
print c //output : set([1, 2, 3, 5, 7])
```

## Set operation

**Intersection:**

**Elements that are common in both sets. So, The intersection** performed using & operator.

so, The same can accomplish using the method **intersection().**

```
a & b
set([2])
>>> a.intersection(b)
set([2])
>>> b.intersection(a)
set([2])
```

## Set difference

**(A – B)** is a set of elements that are only in **A** but not in **B**. Similarly, **B – A **is a set of element

in B but not in **A**.

Difference performed using – operator. The same can accomplish using the method **difference()**.

```
a-b
set([1, 3])
>>> b-a
set([5, 7])
a.difference(b)
set([1, 3])
```

## Symmetric Difference :

**A and B** is a set of elements in both **A and B **except those common in both.

The symmetric difference performed using the** ^** operator. The same can accomplish using the

method **symmetric_difference().**

```
a^b
set([1, 3, 5, 7])
>>> b^a
set([1, 3, 5, 7])
>>> a.symmetric_difference(b)
set([1, 3, 5, 7])
```

## Frozen set

Its elements cannot change once assigned.

Frozen sets are immutable sets. Sets that are mutably un-hashable so, can’t use as dictionary keys.

On the other hand, frozen sets are hashable and can use as keys to a dictionary.

Created using the function **frozenset().**

This datatype supports methods like **copy(), difference(), intersection(), isdisjoint(), issub****set(), issuperset(), symmetric_difference() and union().**

Being immutable it does not have a method that adds or removes elements.

```
set1 = frozenset([1,2,3,4])
set2 = frozenset(['a',1,2,4,'b'])
set1|set2
Output : frozenset(['a', 1, 2, 3, 4, 'b'])
set1.isdisjoint(set2)
Output : False
set1.difference(set2)
Output : frozenset([3])
Try this : set1.add('j') ???
```

## Develop below program for set

Write a program for a set that shows the membership operator’s use.

Iterate the set using for loop as well as while loop.

Program to Illustrate Different Set Operations like **union, intersection, difference, symmetric difference.**

Write a program for set which show the use of **copy(),issubset(), issuperset(), all() and any()**

for more studies visit our website:-https://learningpoints.in/