Hello everyone, welcome back to programminginpython.com. Here in this post, I am going to discuss another data type in Python called `SET`

, which is an unordered collection of data that is iterable and has no duplicate elements. The set in Python represents the mathematical notation of a set.

A set can be simply initialized as `example = set()`

, here example is the name of the set, and can also put some elements in it like `example = set(['one', 2, 'three']) `

and here in `set`

datatype, the order of the elements is not preserved, which means lists and tuples where the order of the elements is checked for indexing.

Now I will perform some of the operations on sets. So let’s get started.

### July Promo - Limited Time Sale

**You can also watch the video on YouTube ****here**

#### Add elements to set

One can add elements to set directly as `set(['one',1,'two',2])`

or there is a method called `add`

as `example.add('one')`

where example here is a set `example = set()`

Here in this example, I will add even, odd, prime, and composite numbers between 1 to 10 as different set elements.

### July Promo - Limited Time Sale

Initialize the sets,

# set of all numbers from 1 to 10 numbers = set() # set of all even numbers between 1 to 10 even_numbers = set() # set of all odd numbers between 1 to 10 odd_numbers = set() # set of all prime numbers between 1 to 10 prime_numbers = set() # set of all composite numbers between 1 to 10 composite_numbers = set()

Now I will add elements to these sets, here I can manually add elements to the set as explained before `numbers = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])`

but I will do it programmatically here, in the previous tutorials I said how to find even, odd, prime, and composite numbers, so I will use that logic to add elements to the respected set here.

functions for `even_odd_sets`

and `prime_composite_sets`

#### Length of a set

Python has an inbuilt function `len(set)`

for finding the length of a set. So I will find the length of the above 5 sets which have different types of numbers in them.

The above lines produce the following output,

#### Intersection of sets

Intersection means the common elements in any given 2 sets, it is generally denoted as ? in mathematics, assume A and B are 2 sets, then consider the following diagram where `A ? B`

are the common elements in both sets.

So we can implement that intersection operation in Python by using the intersection method on one set with the other set. Ex: `A.intersection(B)`

==> A ? B

I will apply this to the numbers example I am using for this tutorial.

#### Union of sets

Union of any 2 sets returns all the elements in both the sets, it is generally denoted as ? in mathematics, assume A and B are 2 sets, then consider the following diagram where `A ? B`

are all the elements in both the sets.

So I will implement this union operation in python by using a function called union(), Ex: `A.union(B)`

Now I will apply this to our numbers example.

#### Difference between sets

The difference between the two sets is all the elements in the first set are not in the second.

A – B is the Set of all elements in A but not in B

B – A is the Set of all elements in B but not in A

See the below Venn diagrams,

A-B Venn diagram

B-A Venn diagram

This is implemented in Python by using a function called difference(), Ex: `A.difference(B)`

is the notation for A-B

Now I will implement our numbers example.

If you observe the above output, the difference between `numbers`

and `prime_numbers`

gave all the elements in `numbers`

set which are not in `prime_numbers`

set and the difference `prime_numbers`

and `numbers`

returned an empty set because there are no elements in `prime_numbers`

which are not in numbers.

#### Remove an element from a set

We can also remove a single element. There are two functions for doing it `remove()`

and `discard()`

Ex: `set.remove('element')`

Ex: `set.discard('element')`

The difference between these two functions remove and discard is that, if the element we are deleting is not present, then the remove function throws an error whereas the discard function, just removes if the element is present or else does nothing.

#### Clear all the elements

And finally, we can clear all the elements in a set by using an inbuilt function in Python for sets called `clear()`

.Simply call this method on any set and all the elements in it will be cleared and will become empty.

That’s it for this post guys, also feel free to check the programs on patterns here or find some programs on algorithms here.