|
| 1 | +--- |
| 2 | +id: complex-data-types |
| 3 | +title: Complex data types |
| 4 | +sidebar_label: 10. Complex data types |
| 5 | +sidebar_position: 11 |
| 6 | +--- |
| 7 | +# Python Data Structures: Beyond the Basics |
| 8 | + |
| 9 | +Welcome to your next step in Python! While primitive data types (like integers, floats, and strings) hold a single value, Python has built-in complex data types that allow you to store, organize, and manipulate collections of data efficiently. |
| 10 | + |
| 11 | +Let's explore the four main complex data types: Lists, Tuples, Dictionaries, and Sets. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 1. Lists |
| 16 | +A list is an ordered, mutable (changeable) collection of items. Lists can contain items of different data types and allow duplicate values. They are the most common and versatile data structure in Python. |
| 17 | + |
| 18 | +**Key Characteristics:** |
| 19 | +* **Ordered:** Maintains the exact order in which you add items. |
| 20 | +* **Mutable:** You can add, remove, or change items after creation. |
| 21 | +* **Syntax:** Created using square brackets `[]`. |
| 22 | + |
| 23 | +**Example:** |
| 24 | +```python |
| 25 | +# Creating a list of fruits |
| 26 | +fruits = ["apple", "banana", "cherry"] |
| 27 | + |
| 28 | +# Modifying an element (lists are 0-indexed) |
| 29 | +fruits[0] = "blueberry" |
| 30 | + |
| 31 | +# Adding a new element to the end |
| 32 | +fruits.append("orange") |
| 33 | + |
| 34 | +print(fruits) |
| 35 | +# Output: ['blueberry', 'banana', 'cherry', 'orange'] |
| 36 | +``` |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## 2. Tuples |
| 41 | +A tuple is very similar to a list, but it is **immutable**. Once a tuple is created, you cannot change, add, or remove items. They are perfect for data that should never change (like fixed coordinates or configurations). |
| 42 | + |
| 43 | +**Key Characteristics:** |
| 44 | +* **Ordered:** Maintains insertion order. |
| 45 | +* **Immutable:** Unchangeable after creation. |
| 46 | +* **Syntax:** Created using parentheses `()`. |
| 47 | + |
| 48 | +**Example:** |
| 49 | +```python |
| 50 | +# Creating a tuple of coordinates |
| 51 | +coordinates = (10.5, 20.0, 5.5) |
| 52 | + |
| 53 | +# Accessing an element |
| 54 | +print(coordinates[0]) # Output: 10.5 |
| 55 | + |
| 56 | +# coordinates[0] = 15.0 <-- This would cause an error! |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## 3. Dictionaries (Dicts) |
| 62 | +A dictionary is a collection of key-value pairs. Instead of using numbers to access elements (like finding the 1st item in a list), you use unique keys (like looking up a word in a real dictionary to find its definition). |
| 63 | + |
| 64 | +**Key Characteristics:** |
| 65 | +* **Key-Value Pairs:** Stores data as `key: value`. |
| 66 | +* **Mutable:** You can add, remove, or change pairs. |
| 67 | +* **Syntax:** Created using curly braces `{}` with colons separating keys and values. |
| 68 | + |
| 69 | +**Example:** |
| 70 | +```python |
| 71 | +# Creating a dictionary for a student |
| 72 | +student = { |
| 73 | + "name": "Alice", |
| 74 | + "age": 22, |
| 75 | + "major": "Computer Science" |
| 76 | +} |
| 77 | + |
| 78 | +# Accessing a value using its key |
| 79 | +print(student["name"]) # Output: Alice |
| 80 | + |
| 81 | +# Adding a new key-value pair |
| 82 | +student["graduation_year"] = 2026 |
| 83 | +``` |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## 4. Sets |
| 88 | +A set is an unordered collection of unique items. Sets are highly efficient for checking if an item exists and for ensuring there are absolutely no duplicate values in your data. |
| 89 | + |
| 90 | +**Key Characteristics:** |
| 91 | +* **Unordered:** No guaranteed order and no indexing (you can't ask for the "first" item). |
| 92 | +* **Unique:** No duplicate values allowed. |
| 93 | +* **Syntax:** Created using curly braces `{}` or the `set()` function. |
| 94 | + |
| 95 | +**Example:** |
| 96 | +```python |
| 97 | +# Creating a set of numbers with duplicates |
| 98 | +unique_numbers = {1, 2, 2, 3, 4, 4, 5} |
| 99 | + |
| 100 | +# Duplicates are automatically removed |
| 101 | +print(unique_numbers) # Output: {1, 2, 3, 4, 5} |
| 102 | + |
| 103 | +# Checking if a value exists in the set (very fast) |
| 104 | +print(3 in unique_numbers) # Output: True |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## Lesson Review Test |
| 110 | + |
| 111 | +Test your understanding of the concepts covered above! |
| 112 | + |
| 113 | +1. Which data type is **immutable** (cannot be changed after creation)? |
| 114 | + * A) List |
| 115 | + * B) Tuple |
| 116 | + * C) Dictionary |
| 117 | + * D) Set |
| 118 | +2. Which data type uses `key: value` pairs? |
| 119 | + * A) List |
| 120 | + * B) Tuple |
| 121 | + * C) Dictionary |
| 122 | + * D) Set |
| 123 | +3. What will be the output of the following code? |
| 124 | + ```python |
| 125 | + my_set = {10, 10, 20, 30} |
| 126 | + print(len(my_set)) |
| 127 | + ``` |
| 128 | + * A) 2 |
| 129 | + * B) 3 |
| 130 | + * C) 4 |
| 131 | + * D) Error |
| 132 | +4. **True or False:** A list is created using square brackets `[]`. |
| 133 | + |
| 134 | +*(Answers: 1. B, 2. C, 3. B (duplicates are removed, so the set is {10, 20, 30} which has 3 items), 4. True)* |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +<details> |
| 139 | + <summary>📚 Deep Dive</summary> |
| 140 | + |
| 141 | +Ready to learn more? Check out these excellent resources: |
| 142 | + |
| 143 | +* **Python Official Documentation:** [Data Structures](https://docs.python.org/3/tutorial/datastructures.html) |
| 144 | +* **Real Python:** [Lists and Tuples](https://realpython.com/python-lists-tuples/) |
| 145 | +* **Real Python:** [Dictionaries](https://realpython.com/python-dicts/) |
| 146 | +* **W3Schools:** [Python Sets](https://www.w3schools.com/python/python_sets.asp) |
| 147 | + |
| 148 | +</details> |
| 149 | + |
| 150 | + |
0 commit comments