Spaces:
Running
Running
File size: 6,084 Bytes
ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 c214ac4 ce9e994 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# ]
# ///
import marimo
__generated_with = "0.10.19"
app = marimo.App()
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
# 📚 Python Dictionaries
Welcome to the world of Python dictionaries — where data gets organized with keys!
## Creating Dictionaries
Dictionaries are collections of key-value pairs. Here's how to create them:
```python
simple_dict = {"name": "Alice", "age": 25} # direct creation
empty_dict = dict() # empty dictionary
from_pairs = dict([("a", 1), ("b", 2)]) # from key-value pairs
```
Below is a sample dictionary we'll use to explore operations.
"""
)
return
@app.cell
def _():
sample_dict = {
"name": "Python",
"type": "programming language",
"year": 1991,
"creator": "Guido van Rossum",
"is_awesome": True,
}
return (sample_dict,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Basic Dictionary Operations
Let's explore how to work with dictionaries.
Try modifying the `sample_dict` above and watch how the results change!
"""
)
return
@app.cell
def _(sample_dict):
# Accessing values of the dictionary
def access_dict():
print(f"Name: {sample_dict['name']}")
print(f"Year: {sample_dict['year']}")
access_dict()
return (access_dict,)
@app.cell
def _(sample_dict):
# Safe access with get()
def safe_access():
print(f"Version: {sample_dict.get('version', 'Not specified')}")
print(f"Type: {sample_dict.get('type', 'Unknown')}")
safe_access()
return (safe_access,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Dictionary Methods
Python dictionaries come with powerful built-in methods:
"""
)
return
@app.cell
def _(sample_dict):
# Viewing dictionary components
def view_components():
print("Keys:", list(sample_dict.keys()))
print("Values:", list(sample_dict.values()))
print("Items:", list(sample_dict.items()))
view_components()
return (view_components,)
@app.cell
def _():
# Modifying dictionaries
def demonstrate_modification():
_dict = {"a": 1, "b": 2}
print("Original:", _dict)
# Adding/updating
_dict.update({"c": 3, "b": 22})
print("After update:", _dict)
# Removing
_removed = _dict.pop("b")
print(f"Removed {_removed}, Now:", _dict)
demonstrate_modification()
return (demonstrate_modification,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Dictionary Comprehension
Create dictionaries efficiently with dictionary comprehensions:
"""
)
return
@app.cell
def _():
# Dictionary comprehension examples
def demonstrate_comprehension():
# Squares dictionary
_squares = {x: x**2 for x in range(5)}
print("Squares:", _squares)
# Filtered dictionary
_even_squares = {x: x**2 for x in range(5) if x % 2 == 0}
print("Even squares:", _even_squares)
demonstrate_comprehension()
return (demonstrate_comprehension,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Nested Dictionaries
Dictionaries can contain other dictionaries, creating complex data structures:
"""
)
return
@app.cell
def _():
nested_data = {
"users": {
"alice": {
"age": 25,
"email": "[email protected]",
"interests": ["python", "data science"],
},
"bob": {
"age": 30,
"email": "[email protected]",
"interests": ["web dev", "gaming"],
},
}
}
return (nested_data,)
@app.cell
def _(nested_data):
# Accessing nested data
def access_nested():
print("Alice's age:", nested_data["users"]["alice"]["age"])
print("Bob's interests:", nested_data["users"]["bob"]["interests"])
# Safe nested access
def _get_nested(data, *keys, default=None):
_current = data
for _key in keys:
if isinstance(_current, dict):
_current = _current.get(_key, default)
else:
return default
return _current
print("\nSafe access example:")
print(
"Charlie's age:",
_get_nested(
nested_data, "users", "charlie", "age", default="Not found"
),
)
access_nested()
return (access_nested,)
@app.cell(hide_code=True)
def _(mo):
mo.md(
"""
## Common Dictionary Patterns
Here are some useful patterns when working with dictionaries:
```python
# Pattern 1: Counting items
counter = {}
for item in items:
counter[item] = counter.get(item, 0) + 1
# Pattern 2: Grouping data
groups = {}
for item in _items:
key = get_group_key(item)
groups.setdefault(key, []).append(item)
# Pattern 3: Caching/Memoization
cache = {}
def expensive_function(arg):
if arg not in cache:
cache[arg] = compute_result(arg)
return cache[arg]
```
"""
)
return
@app.cell(hide_code=True)
def _(mo):
callout_text = mo.md("""
## Master the Dictionary!
Next Steps:
- Practice different dictionary methods
- Try creating nested data structures
- Experiment with dictionary comprehensions
- Build something using common patterns
Keep organizing your data! 🗂️✨
""")
mo.callout(callout_text, kind="success")
return (callout_text,)
@app.cell
def _():
import marimo as mo
return (mo,)
if __name__ == "__main__":
app.run()
|