|
| 1 | +# Day 22: Tuples & Sets 🗂️ |
| 2 | + |
| 3 | +Welcome to Week 4! We're starting with two more versatile data structures: **Tuples** and **Sets**. While lists and dictionaries are great all-purpose tools, Tuples and Sets solve specific problems more elegantly and efficiently. |
| 4 | + |
| 5 | +### Key Concepts |
| 6 | + |
| 7 | +#### Tuples |
| 8 | +Think of a **tuple** as an immutable list—a collection of items that, once created, **cannot be changed**. They are **ordered**, meaning elements have a defined position. |
| 9 | + |
| 10 | +* **Key Feature:** Immutability. This makes them useful for data that should be constant, like a point on a graph or the dimensions of an image. It also makes them faster to process than lists. |
| 11 | +* **Syntax:** Defined using parentheses `()`. |
| 12 | + |
| 13 | +#### Sets |
| 14 | +A **set** is a collection of items that are **unordered** and **do not allow duplicate elements**. |
| 15 | + |
| 16 | +[Image of a Venn Diagram with Union, Intersection, and Difference] |
| 17 | + This makes them ideal for tasks involving uniqueness and set logic. |
| 18 | + |
| 19 | +* **Key Feature:** Uniqueness. If you convert a list to a set, all duplicate elements are automatically removed. They also provide extremely fast membership testing (`if item in my_set`). |
| 20 | +* **Syntax:** Defined using curly braces `{}`. |
| 21 | + |
| 22 | +### 📝 Practice Exercises |
| 23 | + |
| 24 | +1. **Find the Unique Vowels:** Given a string, use a set to find all the unique vowels in it. |
| 25 | +2. **Combine Data:** You have two lists of students who attended two different events. Use set operations to find: |
| 26 | + * Students who attended **both** events. |
| 27 | + * Students who attended **only one** of the events. |
| 28 | +3. **Tuple to List:** Create a tuple of your favorite movies. Convert it to a list, add a new movie, and then convert it back to a tuple. |
| 29 | + |
| 30 | +### ✨ Best Practices & Professional Notes |
| 31 | + |
| 32 | +* **Use Tuples for Fixed Data:** If you have data that shouldn't change, like a database record or configuration values, a tuple is a safer and more memory-efficient choice than a list. |
| 33 | +* **Use Sets for Uniqueness:** Sets are the fastest way to check for membership and eliminate duplicates. Don't use them for data that needs to be in a specific order. |
| 34 | + |
| 35 | +### 🏃 How to Run This Code |
| 36 | + |
| 37 | +1. Open your terminal or command prompt. |
| 38 | +2. Navigate to the `day_22_Tuples_&_Sets` directory. |
| 39 | + ```bash |
| 40 | + cd path/to/your/fluffy-python/day_22_Tuples_&_Sets |
| 41 | + ``` |
| 42 | +3. Run the script using: |
| 43 | + ```bash |
| 44 | + python tuples_sets_intro.py |
| 45 | + ``` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +### ➡️ What's Next? |
| 50 | + |
| 51 | +Tomorrow, we will explore advanced string manipulation and a powerful tool called **Regular Expressions** to find and match text patterns. |
| 52 | + |
| 53 | +[⬅️ Back to Main Repository](./README.md) |
0 commit comments