Add lesson 6: Lists and dictionaries

Mira Kovac 1 year ago
parent c6e0c87b2e commit d9b09d46be251c2fa6c92e04dff9b6cce6244ef0
3 changed files with 37 additions and 0 deletions
+19-0lessons/06-lists-and-dictionaries/README.md ADDED
@@ -0,0 +1,19 @@
1+# Lesson 6: Lists and dictionaries
2+
3+Lists and dictionaries comes up in almost every real Python project. We start with the why, then write code together.
4+
5+Full lesson: [https://glibcode.net/courses/python-basics/lessons/6](https://glibcode.net/courses/python-basics/lessons/6)
6+
7+## Files
8+
9+- `collections_demo.py`: lists and dictionaries side by side
10+
11+## Run it
12+
13+```sh
14+python3 collections_demo.py
15+```
16+
17+## Exercises
18+
19+See [exercises.md](exercises.md). Ask in the forum once you've tried.
+13-0lessons/06-lists-and-dictionaries/collections_demo.py ADDED
@@ -0,0 +1,13 @@
1+fruits = ["apple", "banana", "cherry"]
2+fruits.append("date")
3+print(fruits[0], fruits[-1], len(fruits))
4+
5+stock = {"apple": 12, "banana": 0, "cherry": 40}
6+stock["date"] = 7
7+
8+for fruit, count in stock.items():
9+ status = "sold out" if count == 0 else f"{count} left"
10+ print(f"{fruit:<8} {status}")
11+
12+in_stock = [f for f, n in stock.items() if n > 0]
13+print("In stock:", ", ".join(in_stock))
+5-0lessons/06-lists-and-dictionaries/exercises.md ADDED
@@ -0,0 +1,5 @@
1+# Exercises: lesson 6
2+
3+1. Explain the example to someone who hasn't taken the course. Which part was hardest to explain?
4+2. Make the example ask for its input instead of using a fixed value.
5+3. Find one thing in `collections_demo.py` you could name better and rename it everywhere.