**Overview**

Python Set is one of the built-in Data Types in Python. Other Python data types are

**List**, **Tuple**, and **Dictionary**. Python Set is a collection of unordered and unique elements (no duplicate elements). A Set is denoted by the curly braces {} and is immutable, i.e., once the Set is created cannot be changed, however, you can add an element or remove but can’t replace an element.

**What’s Next!**

Let’s jump into the Python Set Beginner project, where we will walk you through a couple of beginner Python Set questions to get you a command on the Python Set concept. Starting from removing duplicate elements, we will move further to perform basic set operations like union, intersection, difference, etc.

#### Table of Contents

**Project 1: Unique Words Finder**

Create a program that takes a paragraph of text as input and finds all the unique words in the text. The program should use a set to store and display the unique words.

` ````
```text = 'I am a boy I am a Learner'
words = text.split()
unique_word = set()
for word in words:
unique_word.add(word)
print(unique_word)

**Explanation: **The program starts with a variable **text **that stores a sequence of characters. Next, the function split() is used to convert the string in the list of strings. An empty **set()** is declared to add each string from the variable **words **after the **for loop **and displayed using the **print()** function.

Now, let’s see the same question with **Python def**

` ````
```def unique_word_finder(text):
words = text.split()
unique_word = set()
for word in words:
unique_word.add(word)
return unique_word
text = 'I am a boy I am a Learner'
unique_word = unique_word_finder(text)
print(unique_word)

**Explanation: **First create a function **unique_word_finder** and pass the parameter **text**.

This function returns the set of unique strings. Since the input is passed to the **text **is a String paragraph, therefore; used the **split()** function to get a list of strings.

Next, declare a variable holding the empty **set()**. Now use the **for loop **to access each string and add it to the empty set(). Since the set() does not store duplicate items, therefore; it automatically eliminates the duplicate items and stores unique items.

Finally, declare the value to the parameter **text **and call the function to return a set of unique items.

**Project 2: Common Elements Finder**

Write a program that takes two lists of numbers as input and finds the common elements between the two lists. The program should use sets to determine and display the common elements.

` ````
```list1 = [1,2,1]
list2 = [1,3,2]
#common_number = set(list1) & set(list2)
common1 = set(list1) #[1,2]
common2 = set(list2) #[1,3,2]
common_number = common1.intersection(common2)
print(common_number)

**Explanation: **The program starts with two lists of numbers. Next, both lists are converted into the Python Set data type by using the set() function. To find the common numbers from both sets, use the **intersection()** function. Finally, the set of common numbers from lists is displayed using the **print()** function.

**Another way:**

Uncomment the line number 3 and comment the line numbers 4 and 5. In line number 3, the list is converted into the Python set. Rest, applied the intersection() function to get the common numbers from both sets.

Now, let’s see the same question with **Python def**

` ````
```def common_numbers(list1,list2):
common1 = set(list1)
common2 = set(list2)
common_num = common1.intersection(common2)
return common_num
list1 = [1,2,3,4,5]
list2 = [2,3,5,8,9]
common = common_numbers(list1,list2)
print("Common elements between the two lists are:", common)

**Explanation: **The program starts with creating a function **common_numbers** and passes the two lists as parameters.

Since the function returns the common numbers of both lists, therefore; inside the function, we first converted both lists to a Python Set data type by using the **set() function**, and next applied the intersection() function to find the common numbers.

Outside function, defined the two lists of numbers and passed it to the function as the parameter’s value and finally, the output is displayed using the **print()** function.

**Project 3: Set Operations Simulator**

Develop a program that performs basic set operations such as union, intersection, difference, and symmetric difference. The program should take two sets as input and display the results of these operations.

` ````
```set1 = {1,2,3,4,5}
set2 = {1,3,6,7,5}
union_num = set1.union(set2)
intersection_num = set1.intersection(set2)
difference_num = set1.difference(set2)
symmetric_difference_num = set1.symmetric_difference(set2)
print('Union:',union_num)
print('Intersection:',intersection_num)
print('Difference:',difference_num)
print('Symmetric:',symmetric_difference_num)

**Explanation:**

**1. Union:** Returns the set of unique numbers from both sets.**2. Intersection:** Returns the set of common numbers from both sets.**3. Difference:** Returns the set of only the numbers present in set1 but not in set2.**4. Symmetric Difference:** It is the opposite of the intersection() function, i.e., Returns the set of only the uncommon numbers from both sets.

The program starts with 2 sets of numbers. To get the union, intersection, difference, and symmetric difference, we applied the function and finally, displayed the output with the **print()** function.

Now, let’s see the same question with **Python def**

` ````
```def set_operations(set1, set2):
union_num = set1.union(set2)
intersection_num = set1.intersection(set2)
difference_num = set1.difference(set2)
symmetric_difference_num = set1.symmetric_difference(set2)
return union_num, intersection_num, difference_num, symmetric_difference_num
set1 = {2,4,6,8,10}
set2 = {1,2,4,6,8}
union_num, intersection_num, difference_num, symmetric_difference_num = set_operations(set1, set2)
print('Union:',union_num)
print('Intersection:',intersection_num)
print('Difference:',difference_num)
print('Symmetric:',symmetric_difference_num)

**Explanation: **The program starts with a function **set_operations **with two parameters set1 and set2. Since the function returns the union, intersection, difference, and symmetric difference, we applied the function and returned the output.

Outside function, defined the two sets of numbers and passed as the parameter’s value. Finally, call the function and display the output with the **print()** function.

**Project 4: Duplicate Remover**

Create a program that removes duplicate elements from a list. The program should convert the list into a set to eliminate duplicates and then convert it back to a list for display.

` ````
```list1 = ['a','b','c','a','b','d']
convert_to_set = set(list1)
convert_back_to_list = list(convert_to_set)
print('List of unique strings:',convert_back_to_list)

**Explanation:** The program starts with a list of strings. Next, the list is converted into the Python Set to remove the duplicate strings. Finally, the set is converted back to a list and displayed using the **print()** function.

Now, let’s see the same question with **Python def**

` ````
```def delete_duplicate_element(list1):
convert_to_set = set(list1)
convert_to_list = list(convert_to_set)
return convert_to_list
list1 = [1,2,3,1,2,5,6]
List = delete_duplicate_element(list1)
print('List with unique elements:',List)

**Explanation: **The program starts with a function **delete_duplicate_element **with a parameter i.e., list1. Next, the list is converted into the Python Set to remove the duplicate strings. Finally, the set is converted back to a list. Used the **return** keyword to get the output from the function.

Outside the function, a list of numbers is defined and passed as a parameter’s value. Finally, displayed using the **print()** function.

**Project 5: Venn Diagram Data Analyzer**

Write a program that simulates a simple Venn diagram by taking two sets of data and calculating the elements that belong to only one set, both sets, or neither. The program should display the results in a format that resembles the sections of a Venn diagram.

` ````
```set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
universal_set = {1,2,3,4,5,6,7,8,9,10}
# Unique elements in both sets
union_elements = set1.union(set2)
# Elements only in set1
difference_element_set1 = set1.difference(set2)
# Elements only in set2
difference_element_set2 = set2.difference(set1)
# Elements in both sets
intersection_elements = set1.intersection(set2)
neither_element = universal_set.difference(union_elements)
print('Union Set:',union_elements)
print('Difference Set1:',difference_element_set1)
print('Difference Set2:',difference_element_set2)
print('Both Sets:',intersection_elements)
print('Neither Set:',neither_element)

**Explanation:**

- Both Sets: Find the common elements in set1 and set2 using the
**intersection() function**. - Only in Set 1: Find elements in set1 that are not in set2 using the
**difference() function**. - Only in Set 2: Find elements in set2 that are not in set1 using the
**difference() function**. - Neither Set: Find elements in the universal_set that are not in either set1 or set2 using the
**difference()**function with the union of set1 and set2.

The program starts with three sets of numbers. Now apply the union, intersection, and difference function to return the elements that belong to only one set, both sets, or neither.

To get the elements that are neither present in set1 nor in set2, we defined an additional set i.e., **universal_set**, containing the possible elements from both sets. Next, we first take the union of both sets and compare it with the difference of **universal_set**.

Finally, the different operations of the set are displayed using the **print()** function.

This output visually simulates a Venn diagram by categorizing elements into four possible sections: elements unique to each set, elements common to both sets and elements that belong to neither set.