---
title: "Explain about the Python Sets."  
description: "Explain about the Python Sets."  
author: "ICSM Computer"  
published: 2025-09-22  
updated: 2025-09-24  
canonical: https://www.mindstick.com/forum/161923/explain-about-the-python-sets  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 3 minutes  

---

# Explain about the Python Sets.

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) about the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) Sets. with example [line by line](https://www.mindstick.com/interview/34101/how-do-you-read-a-file-line-by-line-using-a-generator-like-approach-e-g-yield-return).**

## Replies

### Reply by Anubhav Sharma

## 1. What is a Set in Python?

> A **set** is a built-in Python [**data type**](https://www.mindstick.com/forum/161367/what-are-python-s-data-types) used to store **unordered, unique elements**.

- **Unordered** → Elements have no fixed order.
- **Unique** → No duplicate values are allowed.
- **Mutable** → You can add or remove elements, but **elements themselves must be immutable** (like numbers, strings, tuples — but not lists or dictionaries).

## 2. Creating a Set

```python
# Empty set
my_set = set()

# Set with values
fruits = {"apple", "banana", "cherry"}
print(fruits)   # {'apple', 'banana', 'cherry'}

# Duplicates are ignored
nums = {1, 2, 2, 3, 4}
print(nums)     # {1, 2, 3, 4}
```

- `{}` creates an **empty dictionary**, not a set. Always use `set()` for an empty set.

## 3. Accessing Elements

Since sets are unordered, they don’t support indexing (`set[0]`).\
You loop through them:

```python
for fruit in fruits:
    print(fruit)
```

## 4. Adding & Removing Elements

```python
fruits.add("orange")        # Add single element
print(fruits)

fruits.update(["grape", "mango"])  # Add multiple elements
print(fruits)

fruits.remove("banana")     # Removes element (raises error if not found)
fruits.discard("pear")      # Removes element (NO error if not found)

fruits.pop()                # Removes a random element
fruits.clear()              # Removes all elements
```

## 5. Set Operations

Sets are powerful for **mathematical operations**:

```python
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}

print(A | B)   # Union → {1, 2, 3, 4, 5, 6}
print(A & B)   # Intersection → {3, 4}
print(A - B)   # Difference → {1, 2}
print(A ^ B)   # Symmetric Difference → {1, 2, 5, 6}
```

## 6. Set Methods

```python
print(A.issubset(B))      # False
print(A.issuperset(B))    # False
print(A.isdisjoint(B))    # False (they share 3,4)
```

## 7. Frozen Sets (Immutable Sets)

If you need an **immutable set** (cannot be changed):

```python
frozen = frozenset([1, 2, 3])
print(frozen)
# frozen.add(4) ❌ → Error
```

## 8. Example Use Cases

**Removing duplicates** from a list:

```python
nums = [1, 2, 2, 3, 4, 4, 5]
unique_nums = set(nums)
print(unique_nums)   # {1, 2, 3, 4, 5}
```

**Membership testing** (faster than lists):

```python
print(3 in A)   # True
print(7 in A)   # False
```

**Summary**:

- **Set** = unordered, unique, mutable collection.
- Supports mathematical set operations (union, intersection, difference).
- Great for removing duplicates & fast membership checks.
- Use **frozenset** if you need immutability.


---

Original Source: https://www.mindstick.com/forum/161923/explain-about-the-python-sets

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
