Table of Contents

## Introduction

In this article we will introduce one more built-in ** Data Structure in Python-Sets**. Previously we read about other built-in

**Tuples, Lists and Dictionaries. We will learn about**

*Data Structures in Python-***,**

*Data Structure in Python-Sets***functions used, iterations and operators.**Lets start with the definition.

** Few quick words:** I will suggest you to download the latest version of Python which will help you to practice coding python programs. In case you do not have Python installed in your system you can use

**this link**to online run your Python codes.

I also suggest you to visit the article **here **to learn the basic concepts of Blockchain.

## Data Structures in Python-Sets-Definition

- Sets are collection of items which are
*iterable, mutable, unordered and have no repeated items or elements.* - Set is a collection which is both
and*unordered*. They are used to store multiple items in a single variable.**unindexed** - Unlike
**Lists, Sets**are unordered. The primary reason for using Sets, as opposed to Lists, is the more optimized method of finding whether an item is contained in the Set or not. **Syntax: {key1,key2,key3,…….}**

*set( )*

- You can create a Set using the
function or enclosing elements in curly brackets ({ }).*set( )* - Like
**Dictionaries**, Sets also use { }. The difference is that Dictionaries haveand*keys*attached to them, whereas Sets only have*values**keys.*

**Output:**

**We can create a Set from a string, tuple, lists and dictionaries. It will remove any duplicate values.**

**Output:**

Now lets try and make a ** set **from a

*.*

**list****Output:**

*len( ): *to get the size of a set

- The
function is used to determine the size of a set.*len( )* - For the set,
*set_test,*defined in the above example, let us find the length using the.*len( )*

**Output:**

*add( ):* to add an item to a set

- We can add any new item in a set using the
function.*add( )*

**Output:**

{1,2,3}

{1,2,3,4}

*remove( ):*to remove an item from a list

- We can delete any item from a set by using the
*remove( )*

**Output:**

{1,2,3,4}

{1,2,3}

## Iterate with *for *and *in*

- Like in a tuple, list or a dictionary, we can iterate over the elements of a set using
and*for**in.*

**Output:**

### Test a value with *in*

- Let us define a dictionary with the surnames as the
and first names as the corresponding*keys*.*values*

**Output:**

- Let’s say we want to check all the surnames with the first names Nitin.

**Output:**

- Similarly if we want to list down all the surnames with first names Barry.

**Output:**

- When we want to list down all the surnames with the first-names Nitin but except Riley (since may be Riley a UK surname does not suit with the Indian first name Nitin)

**Output:**

### Operators (a&b, a|b, a-b, a^b, a<=b, a<b, a>=b)

- Let us again take the previous example.

- If we want to list all the surnames with either ‘Nitin’ or ‘Joe’ as the first names, then we will do something like the below.

**Output:**

We could have used the ampersand ‘** &’ **operator also.

Here the ** & **operator takes the intersection of the two sets compared, i.e.,

*names (set of all the first names) and {‘Nitin,’Joe’}.*Similarly when we want to find all the surnames with ‘Nitin’ as the first names but not ‘Joe’ nor ‘Barry’.

**Output:**

**&**(ampersand) is one of the operators used in Sets. Other operators are**I ,-.****&**is used for intersection, same as the function**intersection( )****I**is used for Union (members of either Set) of two Sets.**union( )**does the same**.****–**is used for the difference (member of first set but not the second) of two Sets.- Let us take two sets
**a**and**b**

of the two sets*The intersection*

**Output:**

or we can use the **intersection( ) function**

**Output:**

of the two sets by using*The union***|**operator or the**union( ) function**

**Output:**

of the two sets by using the*The difference***–**operator or the**difference( )**function.

**Output:**

- Other
operators in Sets are*less frequently used***^,<=, <, >=** - The
(items in one set or the other, but not in both) uses*exclusive***^ operator.**You can use the**symmetric_difference( )**function also

to check whether one set is a subset of the other set. You can also use the*Use <=***issubset( )**function.

**Output:**

**Use >=**to check whether one set is a superset of another set. In that case, all the elements of the second set are also the elements of the first set. You can use thefunction also.*issuperset( )*

**Output:**