Python

 

Introduction

In Python, a set is an unordered collection of unique items. Sets are particularly useful when you need to store items without duplication and perform set operations like union, intersection, and difference. Unlike lists or tuples, sets do not allow duplicate elements, making them a great tool for managing unique data.

Objective

The objective of this tutorial is to help you understand how to use Python sets to store and manipulate unique data. You’ll learn basic set operations and how to implement them in your programs.

Code Example


# Python Set Example: Unique Data Handling

# Create a set with some duplicate elements
data_set = {1, 2, 3, 4, 5, 1, 3}

# Display the set (duplicates will be removed automatically)
print("Unique data in the set:", data_set)

# Adding new elements
data_set.add(6)
data_set.add(7)

# Removing an element
data_set.remove(4)

# Check if an element exists in the set
print("Is 3 in the set?", 3 in data_set)

# Perform set operations: Union, Intersection, Difference
set_b = {5, 6, 7, 8, 9}
print("Union of set_a and set_b:", data_set | set_b)
print("Intersection of set_a and set_b:", data_set & set_b)
print("Difference between set_a and set_b:", data_set - set_b)
        

Explanation of the Program Structure

The program starts by creating a set, data_set</>, with some duplicate elements. When printed, Python automatically removes the duplicates and displays only unique values.

We then use the add() method to add new elements to the set and the remove() method to remove an existing element.

The program checks if an element exists in the set using the in keyword. Then, it demonstrates common set operations:

  • Union: Combines the elements of both sets.
  • Intersection: Displays the common elements between two sets.
  • Difference: Displays elements present in the first set but not in the second set.

How to Run the Program

To run the Python program, follow these steps:

  1. Ensure you have Python installed on your system. If not, download it from python.org.
  2. Open a text editor or an IDE, such as VS Code, Sublime Text, or PyCharm.
  3. Copy the provided Python code into a new Python file (e.g., unique_data.py).
  4. Run the program through your terminal or command prompt by navigating to the folder containing the file and typing python unique_data.py.
  5. The output will be displayed in the terminal, showing the unique set values and the results of the set operations.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)