|
| 1 | +# Python Sets Tutorial |
| 2 | + |
| 3 | +A comprehensive guide to understanding and working with sets in Python. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | +- [What are Sets?](#what-are-sets) |
| 7 | +- [Set Characteristics](#set-characteristics) |
| 8 | +- [Creating Sets](#creating-sets) |
| 9 | +- [Basic Set Operations](#basic-set-operations) |
| 10 | +- [Accessing Set Elements](#accessing-set-elements) |
| 11 | +- [Modifying Sets](#modifying-sets) |
| 12 | +- [Set Operations (Mathematical)](#set-operations-mathematical) |
| 13 | +- [Set Methods Reference](#set-methods-reference) |
| 14 | + |
| 15 | +## What are Sets? |
| 16 | + |
| 17 | +Sets are Python's built-in data structure for storing **multiple unique items** in a single variable. They are the **complete opposite of lists** in terms of ordering and indexing. |
| 18 | + |
| 19 | +### Basic Template |
| 20 | +```python |
| 21 | +my_set = {"apple", "banana", "cherry"} |
| 22 | +``` |
| 23 | + |
| 24 | +## Set Characteristics |
| 25 | + |
| 26 | +| Feature | Set | List | Tuple | |
| 27 | +|---------|-----|------|-------| |
| 28 | +| **Ordered** | ❌ No (Unordered) | ✅ Yes | ✅ Yes | |
| 29 | +| **Indexed** | ❌ No | ✅ Yes | ✅ Yes | |
| 30 | +| **Mutable** | 🔄 Partially* | ✅ Yes | ❌ No | |
| 31 | +| **Duplicates** | ❌ No | ✅ Yes | ✅ Yes | |
| 32 | +| **Syntax** | `{}` Curly braces | `[]` Square brackets | `()` Round brackets | |
| 33 | + |
| 34 | +*Items are unchangeable, but you can add/remove items |
| 35 | + |
| 36 | +## Creating Sets |
| 37 | + |
| 38 | +### 1. Basic Set Creation |
| 39 | +```python |
| 40 | +my_set = {"apple", "banana", "cherry"} |
| 41 | +print(f"Set => {my_set}") |
| 42 | +# Output: {'banana', 'cherry', 'apple'} (order may vary) |
| 43 | +``` |
| 44 | + |
| 45 | +### 2. Using Set Constructor |
| 46 | +```python |
| 47 | +set2 = set(("A", "B", "C")) |
| 48 | +print(set2) # {'A', 'B', 'C'} |
| 49 | +``` |
| 50 | + |
| 51 | +### 3. Mixed Data Types |
| 52 | +```python |
| 53 | +set1 = {"abc", 34, True, 40, "male"} |
| 54 | +``` |
| 55 | + |
| 56 | +## Basic Set Operations |
| 57 | + |
| 58 | +### 1. No Duplicates Allowed |
| 59 | +```python |
| 60 | +my_set = {"apple", "banana", "cherry", "apple"} |
| 61 | +print(f"After adding duplicate: {my_set}") |
| 62 | +# Output: {'apple', 'banana', 'cherry'} (duplicate removed) |
| 63 | +``` |
| 64 | + |
| 65 | +### 2. Special Case: Boolean and Numeric Values |
| 66 | +```python |
| 67 | +my_set = {"apple", "banana", "cherry", 1, True, 0, False} |
| 68 | +print(f"With True/False and 0/1: {my_set}") |
| 69 | +# Output: {0, 1, 'cherry', 'banana', 'apple'} |
| 70 | +# Note: True=1 and False=0, so duplicates are removed |
| 71 | +``` |
| 72 | + |
| 73 | +### 3. Length of Set |
| 74 | +```python |
| 75 | +print(f"Length of set: {len(my_set)}") |
| 76 | +``` |
| 77 | + |
| 78 | +## Accessing Set Elements |
| 79 | + |
| 80 | +**Important**: Sets are unordered and unindexed, so you **cannot access items by index**. |
| 81 | + |
| 82 | +### 1. Iterate Through Set |
| 83 | +```python |
| 84 | +set2 = {"A", "B", "C"} |
| 85 | + |
| 86 | +for element in set2: |
| 87 | + print(f"Element in set: {element}") |
| 88 | +``` |
| 89 | + |
| 90 | +### 2. Check Membership |
| 91 | +```python |
| 92 | +print("A" in set2) # True |
| 93 | +print("G" not in set2) # True |
| 94 | +``` |
| 95 | + |
| 96 | +## Modifying Sets |
| 97 | + |
| 98 | +### Adding Items |
| 99 | + |
| 100 | +#### 1. Add Single Item |
| 101 | +```python |
| 102 | +set2 = {"A", "B", "C"} |
| 103 | +set2.add("D") |
| 104 | +print(f"After adding D: {set2}") |
| 105 | +# Output: {'A', 'D', 'C', 'B'} |
| 106 | +``` |
| 107 | + |
| 108 | +#### 2. Add Multiple Items (update) |
| 109 | +```python |
| 110 | +this_set = {"apple", "banana", "cherry"} |
| 111 | +tropical = {"pineapple", "mango", "papaya"} |
| 112 | + |
| 113 | +this_set.update(tropical) |
| 114 | +print(f"After adding tropical set: {this_set}") |
| 115 | +# Output: {'apple', 'papaya', 'cherry', 'pineapple', 'banana', 'mango'} |
| 116 | +``` |
| 117 | + |
| 118 | +#### 3. Add Any Iterable |
| 119 | +```python |
| 120 | +this_set = {"apple", "banana", "cherry"} |
| 121 | +mylist = ["kiwi", "orange"] |
| 122 | + |
| 123 | +this_set.update(mylist) |
| 124 | +print(f"After adding list: {this_set}") |
| 125 | +# Output: {'banana', 'cherry', 'orange', 'kiwi', 'apple'} |
| 126 | +``` |
| 127 | + |
| 128 | +### Removing Items |
| 129 | + |
| 130 | +#### 1. `remove()` - Raises Error if Not Found |
| 131 | +```python |
| 132 | +this_set.remove("apple") |
| 133 | +print(f"After removing apple: {this_set}") |
| 134 | +# Raises KeyError if "apple" doesn't exist |
| 135 | +``` |
| 136 | + |
| 137 | +#### 2. `discard()` - Safe Removal (No Error) |
| 138 | +```python |
| 139 | +this_set.discard("kiwi") |
| 140 | +print(f"After discarding kiwi: {this_set}") |
| 141 | +# No error even if "kiwi" doesn't exist |
| 142 | +``` |
| 143 | + |
| 144 | +#### 3. `pop()` - Remove Random Item |
| 145 | +```python |
| 146 | +removed_item = this_set.pop() |
| 147 | +print(f"Removed random item: {removed_item}") |
| 148 | +print(f"Set after pop: {this_set}") |
| 149 | +# Note: You can't predict which item will be removed |
| 150 | +``` |
| 151 | + |
| 152 | +#### 4. `del` - Delete Entire Set |
| 153 | +```python |
| 154 | +del this_set # Completely deletes the set |
| 155 | +``` |
| 156 | + |
| 157 | +## Set Operations (Mathematical) |
| 158 | + |
| 159 | +### 1. Union (Combine All Items) |
| 160 | +```python |
| 161 | +set_a = {"apple", "banana", "cherry"} |
| 162 | +set_b = {"pineapple", "apple", "papaya"} |
| 163 | + |
| 164 | +# Method 1: union() |
| 165 | +union_result = set_a.union(set_b) |
| 166 | +print(f"Union: {union_result}") |
| 167 | + |
| 168 | +# Method 2: | operator |
| 169 | +union_result = set_a | set_b |
| 170 | +print(f"Union with |: {union_result}") |
| 171 | + |
| 172 | +# Method 3: update() (modifies original) |
| 173 | +set_a.update(set_b) |
| 174 | +print(f"Union with update: {set_a}") |
| 175 | +``` |
| 176 | + |
| 177 | +### 2. Intersection (Common Items Only) |
| 178 | +```python |
| 179 | +set_a = {"apple", "banana", "cherry"} |
| 180 | +set_b = {"pineapple", "apple", "papaya"} |
| 181 | + |
| 182 | +# Method 1: intersection() |
| 183 | +common = set_a.intersection(set_b) |
| 184 | +print(f"Intersection: {common}") # {'apple'} |
| 185 | + |
| 186 | +# Method 2: & operator |
| 187 | +common = set_a & set_b |
| 188 | +print(f"Intersection with &: {common}") # {'apple'} |
| 189 | +``` |
| 190 | + |
| 191 | +### 3. Difference (Items in First Set Only) |
| 192 | +```python |
| 193 | +set_a = {"apple", "banana", "cherry"} |
| 194 | +set_b = {"pineapple", "apple", "papaya"} |
| 195 | + |
| 196 | +# Method 1: difference() |
| 197 | +diff = set_a.difference(set_b) |
| 198 | +print(f"Difference: {diff}") # {'cherry', 'banana'} |
| 199 | + |
| 200 | +# Method 2: - operator |
| 201 | +diff = set_a - set_b |
| 202 | +print(f"Difference with -: {diff}") # {'cherry', 'banana'} |
| 203 | + |
| 204 | +# Method 3: difference_update() (modifies original) |
| 205 | +set_a.difference_update(set_b) |
| 206 | +print(f"After difference_update: {set_a}") # {'cherry', 'banana'} |
| 207 | +``` |
| 208 | + |
| 209 | +### 4. Symmetric Difference (Items NOT in Both) |
| 210 | +```python |
| 211 | +set_a = {"apple", "banana", "cherry"} |
| 212 | +set_b = {"pineapple", "apple", "papaya"} |
| 213 | + |
| 214 | +# Method 1: symmetric_difference() |
| 215 | +sym_diff = set_a.symmetric_difference(set_b) |
| 216 | +print(f"Symmetric Difference: {sym_diff}") |
| 217 | +# Output: {'papaya', 'cherry', 'banana', 'pineapple'} |
| 218 | + |
| 219 | +# Method 2: ^ operator |
| 220 | +sym_diff = set_a ^ set_b |
| 221 | +print(f"Symmetric Difference with ^: {sym_diff}") |
| 222 | +``` |
| 223 | + |
| 224 | +## Set Methods Reference |
| 225 | + |
| 226 | +| Method | Description | Operator Alternative | |
| 227 | +|--------|-------------|---------------------| |
| 228 | +| `add(item)` | Add single item | - | |
| 229 | +| `update(iterable)` | Add multiple items | - | |
| 230 | +| `remove(item)` | Remove item (raises error if not found) | - | |
| 231 | +| `discard(item)` | Remove item (no error if not found) | - | |
| 232 | +| `pop()` | Remove random item | - | |
| 233 | +| `clear()` | Remove all items | - | |
| 234 | +| `union(other)` | Combine all items | `\|` | |
| 235 | +| `intersection(other)` | Common items only | `&` | |
| 236 | +| `difference(other)` | Items in first set only | `-` | |
| 237 | +| `symmetric_difference(other)` | Items not in both | `^` | |
| 238 | + |
| 239 | +## Visual Set Operations |
| 240 | + |
| 241 | +``` |
| 242 | +Set A: {1, 2, 3, 4} |
| 243 | +Set B: {3, 4, 5, 6} |
| 244 | +
|
| 245 | +Union (A ∪ B): {1, 2, 3, 4, 5, 6} |
| 246 | +Intersection (A ∩ B): {3, 4} |
| 247 | +Difference (A - B): {1, 2} |
| 248 | +Symmetric Diff (A △ B): {1, 2, 5, 6} |
| 249 | +``` |
| 250 | + |
| 251 | +## When to Use Sets |
| 252 | + |
| 253 | +1. **Remove duplicates** from a list |
| 254 | +2. **Membership testing** (very fast) |
| 255 | +3. **Mathematical operations** (union, intersection, etc.) |
| 256 | +4. **Unique collections** where order doesn't matter |
| 257 | + |
| 258 | +## Common Use Cases |
| 259 | + |
| 260 | +### Remove Duplicates from List |
| 261 | +```python |
| 262 | +numbers = [1, 2, 2, 3, 4, 4, 5] |
| 263 | +unique_numbers = list(set(numbers)) |
| 264 | +print(unique_numbers) # [1, 2, 3, 4, 5] |
| 265 | +``` |
| 266 | + |
| 267 | +### Fast Membership Testing |
| 268 | +```python |
| 269 | +valid_users = {"alice", "bob", "charlie"} |
| 270 | +user = "alice" |
| 271 | + |
| 272 | +if user in valid_users: # Very fast operation |
| 273 | + print("Access granted") |
| 274 | +``` |
| 275 | + |
| 276 | +### Find Common Elements |
| 277 | +```python |
| 278 | +team_a_skills = {"python", "sql", "excel"} |
| 279 | +team_b_skills = {"python", "java", "sql"} |
| 280 | + |
| 281 | +common_skills = team_a_skills & team_b_skills |
| 282 | +print(f"Common skills: {common_skills}") # {'python', 'sql'} |
| 283 | +``` |
| 284 | + |
| 285 | +## Key Takeaways |
| 286 | + |
| 287 | +1. **Sets eliminate duplicates automatically** |
| 288 | +2. **No indexing** - use iteration or membership testing |
| 289 | +3. **Unordered** - items may appear in different order each time |
| 290 | +4. **Fast membership testing** - much faster than lists for large collections |
| 291 | +5. **Mathematical operations** - perfect for set theory operations |
| 292 | +6. **Use `discard()` over `remove()`** for safer item removal |
| 293 | +7. **Multiple operator shortcuts** available for set operations |
| 294 | + |
| 295 | +## Important Notes |
| 296 | + |
| 297 | +- **True = 1** and **False = 0** in sets (duplicates removed) |
| 298 | +- **Cannot access by index** - sets are unordered |
| 299 | +- **`pop()` removes random item** - unpredictable in sets |
| 300 | +- **Perfect for unique collections** and mathematical set operations |
| 301 | + |
| 302 | +--- |
| 303 | + |
| 304 | +**Remember**: Sets are ideal when you need **unique items** and **fast membership testing**, but not when you need **ordered** or **indexed** access to elements! |
0 commit comments