Skip to content

Commit d10f8f4

Browse files
Add 5 new exercises with solutions, runner, and CI; fix tutorial links
- Replace generic /learn/ links in all existing exercises and solutions with the matching specific tutorial URL. - Add exercises 06-10: dictionaries, conditionals, files, errors / try-except, and classes — each with a self-check and a matching solution. - Add run_all.py: a small runner that executes every exercise and reports pass / fail / skip (skips exercises that still contain TODO placeholders). - Add .github/workflows/exercises.yml: runs every solutions/*.py file on Python 3.11 for every push and pull request. - Update the README to list all 10 exercises and document the runner and CI.
1 parent a8b2a54 commit d10f8f4

23 files changed

Lines changed: 365 additions & 20 deletions

.github/workflows/exercises.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: exercises
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
run-exercises:
11+
name: Run solution exercises
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Set up Python
16+
uses: actions/setup-python@v5
17+
with:
18+
python-version: "3.11"
19+
- name: Run solutions
20+
run: |
21+
for f in solutions/*.py; do
22+
echo "== $f =="
23+
python "$f"
24+
done

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,57 @@ These exercises mirror the learning track on **[pythonbeginner.help](https://pyt
1212

1313
## How to use
1414

15-
1. Make sure you have [Python installed](https://pythonbeginner.help/learn/) (3.8+).
16-
2. Open an exercise in the [`exercises/`](exercises) folder and complete the `TODO`.
15+
1. Make sure you have [Python installed](https://pythonbeginner.help/learn/how-to-install-python-on-windows-macos-and-linux/) (3.8+).
16+
2. Open an exercise in the [`exercises/`](exercises) folder and complete the `TODO`s.
1717
3. Run it — each file checks your answer with `assert` and prints ✅ when it passes:
1818

1919
```bash
2020
python exercises/01_variables.py
2121
```
2222

2323
4. Stuck? Compare with the matching file in [`solutions/`](solutions).
24+
5. Run every exercise at once with the bundled runner:
25+
26+
```bash
27+
python run_all.py # stop on first failure
28+
python run_all.py --keep # run them all, report each result
29+
```
30+
31+
The runner also reports any unfinished exercise (one that still contains `...` TODO placeholders) as `SKIP` so you can see at a glance what's left to do.
2432

2533
## Exercises
2634

27-
| # | Exercise | Topic | Tutorial |
28-
|---|----------|-------|----------|
29-
| 01 | [Variables & types](exercises/01_variables.py) | Variables, `int`/`str` | [Learn →](https://pythonbeginner.help/learn/) |
30-
| 02 | [String methods](exercises/02_strings.py) | Strings, slicing | [Learn →](https://pythonbeginner.help/learn/) |
31-
| 03 | [Lists](exercises/03_lists.py) | Lists, indexing | [Learn →](https://pythonbeginner.help/learn/) |
32-
| 04 | [Loops](exercises/04_loops.py) | `for` / `range` | [Learn →](https://pythonbeginner.help/learn/) |
33-
| 05 | [Functions](exercises/05_functions.py) | `def`, return values | [Learn →](https://pythonbeginner.help/learn/) |
35+
| # | Exercise | Topic | Tutorial |
36+
|----|-----------------------------------|--------------------------------------|-----------------------------------------------------------------------------------------------------------------|
37+
| 01 | [Variables & types](exercises/01_variables.py) | Variables, `int` / `str` | [Learn →](https://pythonbeginner.help/learn/python-variables-explained-for-beginners/) |
38+
| 02 | [String methods](exercises/02_strings.py) | Strings, slicing | [Learn →](https://pythonbeginner.help/learn/python-strings-explained-basics-and-examples/) |
39+
| 03 | [Lists](exercises/03_lists.py) | Lists, indexing, `sorted()` | [Learn →](https://pythonbeginner.help/learn/python-lists-explained-beginner-guide/) |
40+
| 04 | [Loops](exercises/04_loops.py) | `for` and `range` | [Learn →](https://pythonbeginner.help/learn/python-for-loops-explained/) |
41+
| 05 | [Functions](exercises/05_functions.py) | `def`, return values | [Learn →](https://pythonbeginner.help/learn/python-functions-explained/) |
42+
| 06 | [Dictionaries](exercises/06_dictionaries.py) | `dict`, `.get()`, keys | [Learn →](https://pythonbeginner.help/learn/python-dictionaries-explained/) |
43+
| 07 | [Conditionals](exercises/07_conditionals.py) | `if` / `elif` / `else`, ternary | [Learn →](https://pythonbeginner.help/learn/python-if-else-and-elif-explained/) |
44+
| 08 | [Files](exercises/08_files.py) | `open`, `read`, `write` | [Learn →](https://pythonbeginner.help/learn/python-file-handling-basics-read-and-write/) |
45+
| 09 | [Errors & try/except](exercises/09_errors.py) | `try`, `except ValueError` | [Learn →](https://pythonbeginner.help/learn/using-try-except-else-and-finally-in-python/) |
46+
| 10 | [Classes](exercises/10_classes.py) | `class`, `__init__`, methods | [Learn →](https://pythonbeginner.help/learn/python-classes-and-objects-explained/) |
47+
48+
## CI
49+
50+
Every push and pull request runs all of the `solutions/*.py` files through GitHub Actions on Python 3.11, so the answers are always verified to work. See [`.github/workflows/exercises.yml`](.github/workflows/exercises.yml).
3451

3552
## Run all exercises at once
3653

3754
```bash
3855
for f in exercises/*.py; do echo "== $f =="; python "$f"; done
3956
```
4057

41-
## More
58+
…or just use the runner above.
59+
60+
## Companion repos
4261

4362
- 📚 Tutorials: **[pythonbeginner.help](https://pythonbeginner.help)**
4463
- 📋 [Python Cheatsheet](https://github.com/python-beginner-help/python-cheatsheet)
4564
- 🔧 [Python Error Fixes](https://github.com/python-beginner-help/python-error-fixes) — when an exercise throws an error you don't recognise.
65+
-[Awesome Python for Beginners](https://github.com/python-beginner-help/awesome-python-for-beginners) — the curated resource list.
4666

4767
## License
4868

exercises/01_variables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Exercise 01 — Variables & types.
22
3-
Tutorial: https://pythonbeginner.help/learn/
3+
Tutorial: https://pythonbeginner.help/learn/python-variables-explained-for-beginners/
44
55
TODO:
66
1. Create a variable `name` holding your name as a string.

exercises/02_strings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Exercise 02 — String methods & slicing.
22
3-
Tutorial: https://pythonbeginner.help/learn/
3+
Tutorial: https://pythonbeginner.help/learn/python-strings-explained-basics-and-examples/
44
55
TODO: given `phrase`, produce:
66
1. `shout` -> the phrase in UPPERCASE

exercises/03_lists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Exercise 03 — Lists.
22
3-
Tutorial: https://pythonbeginner.help/learn/
3+
Tutorial: https://pythonbeginner.help/learn/python-lists-explained-beginner-guide/
44
55
TODO: starting from `nums`:
66
1. Append the number 5.

exercises/04_loops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Exercise 04 — Loops.
22
3-
Tutorial: https://pythonbeginner.help/learn/
3+
Tutorial: https://pythonbeginner.help/learn/python-for-loops-explained/
44
55
TODO: using a loop,
66
1. Build `squares` = [0, 1, 4, 9, 16] (squares of 0..4).

exercises/05_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Exercise 05 — Functions.
22
3-
Tutorial: https://pythonbeginner.help/learn/
3+
Tutorial: https://pythonbeginner.help/learn/python-functions-explained/
44
55
TODO:
66
1. Write `greet(name)` that returns "Hello, <name>!".

exercises/06_dictionaries.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Exercise 06 — Dictionaries.
2+
3+
Tutorial: https://pythonbeginner.help/learn/python-dictionaries-explained/
4+
5+
TODO: starting from `prices`:
6+
1. Add a new key "pear" with the value 3.
7+
2. Look up the price of "apple" safely into `apple_price`
8+
(use `.get()` so missing keys don't crash — return None if absent).
9+
3. Build `items` = a list of the keys in `prices`.
10+
11+
Run: python exercises/06_dictionaries.py
12+
"""
13+
14+
prices = {"apple": 2, "banana": 1, "orange": 4}
15+
16+
# --- your code below -------------------------------------------------------
17+
# TODO: prices["pear"] = 3
18+
apple_price = ... # TODO: prices.get("apple")
19+
items = ... # TODO: list(prices.keys())
20+
21+
# --- self-check (don't edit) ----------------------------------------------
22+
assert prices["pear"] == 3, "did you add 'pear': 3 to prices?"
23+
assert apple_price == 2, "apple_price should be 2"
24+
assert sorted(items) == ["apple", "banana", "orange", "pear"], "items should list all keys"
25+
print("✅ Passed!", "prices:", prices, "| apple_price:", apple_price)

exercises/07_conditionals.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Exercise 07 — Conditionals.
2+
3+
Tutorial: https://pythonbeginner.help/learn/python-if-else-and-elif-explained/
4+
5+
TODO: classify a number:
6+
1. Set `label` to "positive", "zero", or "negative" based on `n`.
7+
2. Set `parity` to "even" or "odd" using a ternary expression.
8+
9+
Run: python exercises/07_conditionals.py
10+
"""
11+
12+
n = -7
13+
14+
# --- your code below -------------------------------------------------------
15+
if n > 0:
16+
label = ... # TODO: "positive"
17+
elif n < 0:
18+
label = ... # TODO: "negative"
19+
else:
20+
label = ... # TODO: "zero"
21+
22+
parity = ... # TODO: "even" if n % 2 == 0 else "odd"
23+
24+
# --- self-check (don't edit) ----------------------------------------------
25+
assert label == "negative", "for n = -7, label should be 'negative'"
26+
assert parity == "odd", "for n = -7, parity should be 'odd'"
27+
print("✅ Passed!", "label:", label, "| parity:", parity)

exercises/08_files.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Exercise 08 — Files.
2+
3+
Tutorial: https://pythonbeginner.help/learn/python-file-handling-basics-read-and-write/
4+
5+
TODO:
6+
1. Write the lines in `lines` to a temp file called `greeting.txt`.
7+
2. Read the same file back into `contents` (as a single string).
8+
9+
The file should live in the current working directory and is cleaned up
10+
automatically after the self-check runs.
11+
12+
Run: python exercises/08_files.py
13+
"""
14+
15+
import os
16+
17+
lines = ["Hello, world!", "Welcome to Python.", "Goodbye."]
18+
19+
# --- your code below -------------------------------------------------------
20+
path = "greeting.txt"
21+
22+
# TODO: open(path, "w") and write each line in `lines` (remember "\n")
23+
24+
contents = ... # TODO: open(path) and read the whole file
25+
26+
# --- self-check (don't edit) ----------------------------------------------
27+
assert contents == "Hello, world!\nWelcome to Python.\nGoodbye.\n", \
28+
f"contents was {contents!r}"
29+
print("✅ Passed!", "file contents:", repr(contents))
30+
31+
os.remove(path)

0 commit comments

Comments
 (0)