|
| 1 | +# Day 20 – Dictionary in Python |
| 2 | + |
| 3 | +## 🎯 Topics Covered |
| 4 | + |
| 5 | +- Dictionary |
| 6 | + - `dict()` |
| 7 | + - `get()` |
| 8 | + - `update()` |
| 9 | + - `pop()` |
| 10 | + - `popitem()` |
| 11 | + - `clear()` |
| 12 | + - `items()` |
| 13 | + - `keys()` |
| 14 | + - `values()` |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +# What is a Dictionary? |
| 19 | + |
| 20 | +- A dictionary in Python is an unordered, mutable, and indexed collection of key–value pairs. |
| 21 | +It is used to store data values like a real-world dictionary — where each key maps to a specific value. |
| 22 | + |
| 23 | +# Example: |
| 24 | +```python |
| 25 | +student = {"name": "Vinaal", "age": 21, "course": "Python"} |
| 26 | +print(student) |
| 27 | +``` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Creating a Dictionary |
| 32 | + |
| 33 | +You can create a dictionary using curly braces {} or the dict() constructor. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## Using curly braces |
| 38 | +```python |
| 39 | +info = {"brand": "Tesla", "model": "Model X", "year": 2025} |
| 40 | +``` |
| 41 | + |
| 42 | +## Using dict() constructor |
| 43 | +```python |
| 44 | +data = dict(name="Dark AI", skill="Full Stack Developer") |
| 45 | +``` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## Accessing Dictionary Elements |
| 50 | + |
| 51 | +### Using Key |
| 52 | +```python |
| 53 | +person = {"name": "Alice", "age": 25} |
| 54 | +print(person["name"]) # Output: Alice |
| 55 | +``` |
| 56 | + |
| 57 | +### Using get() |
| 58 | + |
| 59 | +- Safely returns the value of a key. If the key doesn’t exist, it returns None instead of an error. |
| 60 | +```python |
| 61 | +person = {"name": "Alice", "age": 25} |
| 62 | +print(person.get("age")) # Output: 25 |
| 63 | +print(person.get("city")) # Output: None |
| 64 | +``` |
| 65 | + |
| 66 | +### Modifying Dictionary Elements |
| 67 | + |
| 68 | +- You can update an existing key or add a new one. |
| 69 | +```python |
| 70 | +car = {"brand": "BMW", "year": 2020} |
| 71 | +car["year"] = 2025 # Update value |
| 72 | +car["color"] = "Black" # Add new key-value pair |
| 73 | +print(car) |
| 74 | +``` |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Dictionary Methods |
| 79 | + |
| 80 | +### keys() |
| 81 | + |
| 82 | +- Returns all keys from the dictionary. |
| 83 | +```python |
| 84 | +person = {"name": "Vinaal", "age": 21, "course": "Python"} |
| 85 | +print(person.keys()) |
| 86 | +``` |
| 87 | + |
| 88 | +### Output |
| 89 | +```python |
| 90 | +dict_keys(['name', 'age', 'course']) |
| 91 | +``` |
| 92 | + |
| 93 | +### values() |
| 94 | + |
| 95 | +- Returns all values from the dictionary. |
| 96 | +```python |
| 97 | +person = {"name": "Vinaal", "age": 21} |
| 98 | +print(person.values()) |
| 99 | +``` |
| 100 | + |
| 101 | +### Output |
| 102 | +```python |
| 103 | +dict_values(['Vinaal', 21]) |
| 104 | +``` |
| 105 | + |
| 106 | +### items() |
| 107 | + |
| 108 | +- Returns all key–value pairs as tuples. |
| 109 | +```python |
| 110 | +person = {"name": "Vinaal", "age": 21} |
| 111 | +print(person.items()) |
| 112 | +``` |
| 113 | + |
| 114 | +### Output |
| 115 | +```python |
| 116 | +dict_items([('name', 'Vinaal'), ('age', 21)]) |
| 117 | +``` |
| 118 | + |
| 119 | +### update() |
| 120 | + |
| 121 | +- Merges another dictionary or adds new key–value pairs. |
| 122 | +```python |
| 123 | +student = {"name": "Dark", "age": 20} |
| 124 | +student.update({"age": 21, "course": "Python"}) |
| 125 | +print(student) |
| 126 | +``` |
| 127 | + |
| 128 | +### Output |
| 129 | +```python |
| 130 | +{'name': 'Dark', 'age': 21, 'course': 'Python'} |
| 131 | +``` |
| 132 | + |
| 133 | +### pop() |
| 134 | + |
| 135 | +- Removes a specific key and returns its value. |
| 136 | +```python |
| 137 | +data = {"x": 10, "y": 20, "z": 30} |
| 138 | +data.pop("y") |
| 139 | +print(data) |
| 140 | +``` |
| 141 | + |
| 142 | +### Output |
| 143 | +```python |
| 144 | +{'x': 10, 'z': 30} |
| 145 | +``` |
| 146 | + |
| 147 | +### popitem() |
| 148 | + |
| 149 | +- Removes the last inserted key–value pair. |
| 150 | +```python |
| 151 | +info = {"a": 1, "b": 2, "c": 3} |
| 152 | +info.popitem() |
| 153 | +print(info) |
| 154 | +``` |
| 155 | + |
| 156 | +### Output |
| 157 | +```python |
| 158 | +{'a': 1, 'b': 2} |
| 159 | +``` |
| 160 | + |
| 161 | +### clear() |
| 162 | + |
| 163 | +- Removes all items from the dictionary. |
| 164 | +```python |
| 165 | +info = {"x": 100, "y": 200} |
| 166 | +info.clear() |
| 167 | +print(info) |
| 168 | +``` |
| 169 | + |
| 170 | +### Output |
| 171 | +```python |
| 172 | +{} |
| 173 | +``` |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## Looping Through a Dictionary |
| 178 | +```python |
| 179 | +# Looping through keys: |
| 180 | +car = {"brand": "Tesla", "model": "S", "year": 2025} |
| 181 | +for key in car: |
| 182 | + print(key) |
| 183 | + |
| 184 | +Looping through values: |
| 185 | +for value in car.values(): |
| 186 | + print(value) |
| 187 | + |
| 188 | +Looping through both keys and values: |
| 189 | +for key, value in car.items(): |
| 190 | + print(key, ":", value) |
| 191 | +``` |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +## Copying a Dictionary |
| 196 | + |
| 197 | +- You can use copy() to make a duplicate dictionary. |
| 198 | +```python |
| 199 | +original = {"name": "Dark", "lang": "Python"} |
| 200 | +duplicate = original.copy() |
| 201 | +print(duplicate) |
| 202 | +``` |
| 203 | + |
| 204 | +--- |
| 205 | + |
| 206 | +## Nested Dictionary |
| 207 | + |
| 208 | +- A dictionary inside another dictionary. |
| 209 | +```python |
| 210 | +students = { |
| 211 | + "student1": {"name": "Alice", "age": 20}, |
| 212 | + "student2": {"name": "Bob", "age": 22} |
| 213 | +} |
| 214 | + |
| 215 | +print(students["student1"]["name"]) |
| 216 | +# Output: Alice |
| 217 | +``` |
| 218 | + |
| 219 | +--- |
| 220 | + |
| 221 | +## Built-in Functions for Dictionary |
| 222 | +```python |
| 223 | +len() # Returns total number of key-value pairs |
| 224 | +str() # Converts dictionary into a string |
| 225 | +type() # Returns the type of the object |
| 226 | +``` |
| 227 | + |
| 228 | +--- |
| 229 | + |
| 230 | +## 📘 *Next step (Day 20):* |
| 231 | + |
| 232 | +I’ll explore **Function**. |
0 commit comments