In this tutorial, we will learn how to implement a Set in Python. A Set is an unordered collection of items that is not indexed and does not accept duplicate values. It is mutable, and its elements can be altered after the Set has been created.

Sets are useful for membership testing, removing duplicates from a sequence, and performing mathematical set operations like union, intersection, difference, and symmetric difference.

### Step 1: Create a Set

Creating a Set is easy and can be done in two ways: using curly braces or the **set()** function. Below are examples of both approaches:

1 2 3 4 5 |
set_1 = {1, 2, 3, 4, 5} print(set_1) set_2 = set([1, 2, 2, 3, 4]) print(set_2) |

**Output:**

{1, 2, 3, 4, 5} {1, 2, 3, 4}

Observe how the duplicate value **2** was automatically removed from **set_2**.

### Step 2: Add and Remove Items

Since a Set is mutable, you can add and remove items using the **add()** and **remove()** functions, respectively.

1 2 3 4 5 |
set_1.add(6) print(set_1) set_1.remove(3) print(set_1) |

**Output:**

{1, 2, 3, 4, 5, 6} {1, 2, 4, 5, 6}

The **discard()** function can also remove an item from a Set, but unlike **remove()**, it does not raise an error if the item is not found in the Set:

1 2 3 |
set_1.discard(4) set_1.discard(10) print(set_1) |

**Output:**

{1, 2, 5, 6}

### Step 3: Iterate Over a Set

Although a Set is unordered and not indexed, you can still iterate over its elements using a simple **for** loop:

1 2 |
for item in set_1: print(item) |

**Output:**

1 2 5 6

### Step 4: Set Operations

Python Sets support various mathematical set operations, such as union, intersection, difference, and symmetric difference. Here is an example for each, using two Sets:

1 2 3 4 5 6 7 8 9 10 11 12 |
A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8} union = A | B intersection = A & B difference = A - B symmetric_difference = A ^ B print("Union :", union) print("Intersection :", intersection) print("Difference :", difference) print("Symmetric difference :", symmetric_difference) |

**Output:**

Union : {1, 2, 3, 4, 5, 6, 7, 8} Intersection : {4, 5} Difference : {1, 2, 3} Symmetric difference : {1, 2, 3, 6, 7, 8}

Alternatively, you can use the corresponding set methods to achieve the same results:

1 2 3 4 5 6 7 8 9 |
union = A.union(B) intersection = A.intersection(B) difference = A.difference(B) symmetric_difference = A.symmetric_difference(B) print("Union :", union) print("Intersection :", intersection) print("Difference :", difference) print("Symmetric difference :", symmetric_difference) |

## Full Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
set_1 = {1, 2, 3, 4, 5} print(set_1) set_2 = set([1, 2, 2, 3, 4]) print(set_2) set_1.add(6) print(set_1) set_1.remove(3) print(set_1) set_1.discard(4) set_1.discard(10) print(set_1) for item in set_1: print(item) A = {1, 2, 3, 4, 5} B = {4, 5, 6, 7, 8} union = A | B intersection = A & B difference = A - B symmetric_difference = A ^ B print("Union :", union) print("Intersection :", intersection) print("Difference :", difference) print("Symmetric difference :", symmetric_difference) |

## Conclusion

In this tutorial, you have learned how to implement a Set in Python, add and remove items, iterate over a Set, and perform various mathematical set operations. With Sets, you can efficiently perform a wide range of tasks, such as membership testing and computing the union or intersection of two collections.