Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
250 changes: 250 additions & 0 deletions docs/python/python-set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
---
id: python-set
title: Set in Python
sidebar_label: Set in Python #displays in sidebar
sidebar_position: 10
tags:
[
Python,
List in Python,
Introduction of python,
Comment thread
codewithdhruba01 marked this conversation as resolved.
Python Syntax,
Variables,
Operators,
Type Casting,
String,
Tuple in Python,
Set in Python
]

---

# Set in Python

A **Set** in Python is a built-in data structure used to store multiple **unique** elements in a single variable. Sets are **unordered** and **mutable**, making them an efficient tool for membership tests, eliminating duplicates, and performing mathematical set operations.


## Why Use Sets?

Sets are commonly used when:
- You need to **store unique values only**
- The order of elements **does not matter**
- You want to perform **fast membership testing**
- You need efficient **union, intersection, and difference operations**


## Key Characteristics

- **Unordered:** Elements have no defined order.
- **Unique:** No duplicates allowed.
- **Mutable:** You can add or remove items.
- **Iterable:** You can loop through a set.


## Creating a Set

There are two main ways to create a set:

### Using Curly Braces `{}`

```python
fruits = {"apple", "banana", "cherry"}
print(fruits) # Output: {'banana', 'apple', 'cherry'}
````

### Using the `set()` Constructor

```python
numbers = set([1, 2, 3, 2])
print(numbers) # Output: {1, 2, 3}
```

> **Important:** Empty curly braces `{}` create an **empty dictionary**, not a set. Use `set()` instead:

>
> ```python
> empty_set = set()
> ```


## Adding Elements to a Set

You can add elements using the `add()` method:

```python
colors = {"red", "green"}
colors.add("blue")
print(colors) # Output: {'red', 'green', 'blue'}
```

## Removing Elements from a Set

Python provides several methods to remove elements:

### `remove()`

Removes the specified item. Raises an error if the item does not exist.

```python
colors = {"red", "green"}
colors.remove("red")
print(colors) # Output: {'green'}
```

> If you try to remove an item not present:
>
> ```python
> colors.remove("yellow") # KeyError
> ```


### `discard()`

Removes the specified item without raising an error if the item is not found.

```python
colors = {"green"}
colors.discard("yellow") # No error
```

### `pop()`

Removes and returns an arbitrary element.

```python
colors = {"red", "blue"}
removed = colors.pop()
print("Removed:", removed)
print("Remaining:", colors)
```


### `clear()`

Removes all elements.

```python
colors.clear()
print(colors) # Output: set()
```


## Updating a Set

To add multiple items at once, use `update()`:

```python
a = {1, 2}
a.update([3, 4], {5, 6})
print(a) # Output: {1, 2, 3, 4, 5, 6}
```


## Membership Testing

Check if an item is in a set using `in` or `not in`:

```python
nums = {1, 2, 3}
print(2 in nums) # Output: True
print(5 not in nums) # Output: True
```


## Common Set Operations

Python sets support powerful operations:

| Operation | Syntax | Description | |
| -------------------- | -------------------------------------- | ------------------------------------ | ----------------------------------- |
| Union | \`a | b`or`a.union(b)\` | Combine all elements from both sets |
| Intersection | `a & b` or `a.intersection(b)` | Elements common to both sets | |
| Difference | `a - b` or `a.difference(b)` | Elements in `a` but not in `b` | |
| Symmetric Difference | `a ^ b` or `a.symmetric_difference(b)` | Elements in either set, but not both | |

---

### Example

```python
a = {1, 2, 3}
b = {3, 4, 5}

# Union
print(a | b) # {1, 2, 3, 4, 5}

# Intersection
print(a & b) # {3}

# Difference
print(a - b) # {1, 2}

# Symmetric Difference
print(a ^ b) # {1, 2, 4, 5}
```

---

## Iterating Over a Set

Loop through elements with a `for` loop:

```python
animals = {"cat", "dog", "rabbit"}
for animal in animals:
print(animal)
```

> Since sets are unordered, the output order may differ every time.


## Frozenset

A **frozenset** is an immutable version of a set. Once created, you cannot modify it.

```python
fs = frozenset([1, 2, 3])
print(fs) # frozenset({1, 2, 3})

# fs.add(4) # AttributeError
```

Use `frozenset` when you need a **hashable set**, e.g., as dictionary keys.


## When to Use Sets?

* Removing duplicates from lists
* Fast membership tests
* Performing set algebra (union, intersection)
* Representing unique collections


## Best Practices

* Use `set()` to create empty sets.
* Use `discard()` if you are not sure whether an element exists.
* Prefer sets over lists when you need uniqueness or fast lookups.

---

## Practice Exercises

1. **Create a set of your favorite fruits. Add one more fruit and remove another.**
2. **Find the union and intersection of `{1, 2, 3}` and `{3, 4, 5}`.**
3. **Write a program that removes duplicates from a list using a set.**
4. **Create a frozenset and show that it cannot be modified.**

---

## Summary Table

| Feature | Description |
| --------------------- | -------------------------------------------- |
| **Mutable** | Yes (can add/remove elements) |
| **Ordered** | No |
| **Duplicates** | Not allowed |
| **Empty Declaration** | `set()` |
| **Immutable Variant** | `frozenset` |
| **Typical Use Cases** | Membership tests, uniqueness, set operations |

2 changes: 1 addition & 1 deletion docs/python/setup-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: setup-environment
title: Setting up your development environment
sidebar_label: Setting up environment
sidebar_position: 10
sidebar_position: 11
tags:
[
html,
Expand Down