Skip to content

Commit 99d9163

Browse files
Initial commit: Runnable beginner Python exercises with self-checks and solutions. Powered by pythonbeginner.help
Part of the pythonbeginner.help project (https://pythonbeginner.help).
0 parents  commit 99d9163

12 files changed

Lines changed: 231 additions & 0 deletions

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__/
2+
*.pyc
3+
.venv/
4+
.idea/
5+
.DS_Store

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Python Beginner Exercises 🏋️🐍
2+
3+
> Small, runnable practice exercises for new Python programmers — each with a built-in self-check and a worked solution.
4+
5+
These exercises mirror the learning track on **[pythonbeginner.help](https://pythonbeginner.help)**. Read a topic, then practice it here.
6+
7+
## How to use
8+
9+
1. Make sure you have [Python installed](https://pythonbeginner.help/learn/) (3.8+).
10+
2. Open an exercise in the [`exercises/`](exercises) folder and complete the `TODO`.
11+
3. Run it — each file checks your answer with `assert` and prints ✅ when it passes:
12+
13+
```bash
14+
python exercises/01_variables.py
15+
```
16+
17+
4. Stuck? Compare with the matching file in [`solutions/`](solutions).
18+
19+
## Exercises
20+
21+
| # | Exercise | Topic | Tutorial |
22+
|---|----------|-------|----------|
23+
| 01 | [Variables & types](exercises/01_variables.py) | Variables, `int`/`str` | [Learn →](https://pythonbeginner.help/learn/) |
24+
| 02 | [String methods](exercises/02_strings.py) | Strings, slicing | [Learn →](https://pythonbeginner.help/learn/) |
25+
| 03 | [Lists](exercises/03_lists.py) | Lists, indexing | [Learn →](https://pythonbeginner.help/learn/) |
26+
| 04 | [Loops](exercises/04_loops.py) | `for` / `range` | [Learn →](https://pythonbeginner.help/learn/) |
27+
| 05 | [Functions](exercises/05_functions.py) | `def`, return values | [Learn →](https://pythonbeginner.help/learn/) |
28+
29+
## Run all exercises at once
30+
31+
```bash
32+
for f in exercises/*.py; do echo "== $f =="; python "$f"; done
33+
```
34+
35+
## More
36+
37+
- 📚 Tutorials: **[pythonbeginner.help](https://pythonbeginner.help)**
38+
- 📋 [Python Cheatsheet](https://github.com/python-beginner-help/python-cheatsheet)
39+
- 🔧 [Python Error Fixes](https://github.com/python-beginner-help/python-error-fixes) — when an exercise throws an error you don't recognise.
40+
41+
## License
42+
43+
Released under [CC0 1.0](LICENSE) by **pythonbeginner.help** — copy, remix, and teach with these freely.

exercises/01_variables.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Exercise 01 — Variables & types.
2+
3+
Tutorial: https://pythonbeginner.help/learn/
4+
5+
TODO:
6+
1. Create a variable `name` holding your name as a string.
7+
2. Create a variable `age` holding an age as an integer.
8+
3. Create `age_text` that turns `age` into a string using str().
9+
10+
Run: python exercises/01_variables.py
11+
"""
12+
13+
# --- your code below -------------------------------------------------------
14+
name = ... # TODO: a string, e.g. "Ada"
15+
age = ... # TODO: an integer, e.g. 36
16+
age_text = ... # TODO: str(age)
17+
18+
# --- self-check (don't edit) ----------------------------------------------
19+
assert isinstance(name, str) and name != "", "name should be a non-empty string"
20+
assert isinstance(age, int), "age should be an integer"
21+
assert age_text == str(age), "age_text should be str(age)"
22+
print(f"✅ Passed! {name} is {age_text} years old.")

exercises/02_strings.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Exercise 02 — String methods & slicing.
2+
3+
Tutorial: https://pythonbeginner.help/learn/
4+
5+
TODO: given `phrase`, produce:
6+
1. `shout` -> the phrase in UPPERCASE
7+
2. `first5` -> the first 5 characters
8+
3. `words` -> a list of the words
9+
10+
Run: python exercises/02_strings.py
11+
"""
12+
13+
phrase = "python is fun"
14+
15+
# --- your code below -------------------------------------------------------
16+
shout = ... # TODO: phrase.upper()
17+
first5 = ... # TODO: phrase[0:5]
18+
words = ... # TODO: phrase.split()
19+
20+
# --- self-check (don't edit) ----------------------------------------------
21+
assert shout == "PYTHON IS FUN", "shout should be the phrase uppercased"
22+
assert first5 == "pytho", "first5 should be the first 5 characters"
23+
assert words == ["python", "is", "fun"], "words should be the split words"
24+
print("✅ Passed!", shout, "|", first5, "|", words)

exercises/03_lists.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Exercise 03 — Lists.
2+
3+
Tutorial: https://pythonbeginner.help/learn/
4+
5+
TODO: starting from `nums`:
6+
1. Append the number 5.
7+
2. Store the first item in `first` and the last in `last`.
8+
3. Store a NEW sorted list in `ordered` (don't change `nums`).
9+
10+
Run: python exercises/03_lists.py
11+
"""
12+
13+
nums = [3, 1, 2]
14+
15+
# --- your code below -------------------------------------------------------
16+
# TODO: nums.append(5)
17+
first = ... # TODO: nums[0]
18+
last = ... # TODO: nums[-1]
19+
ordered = ... # TODO: sorted(nums)
20+
21+
# --- self-check (don't edit) ----------------------------------------------
22+
assert nums == [3, 1, 2, 5], "did you append 5 to nums?"
23+
assert first == 3, "first should be nums[0]"
24+
assert last == 5, "last should be nums[-1]"
25+
assert ordered == [1, 2, 3, 5], "ordered should be a sorted copy of nums"
26+
print("✅ Passed!", nums, "first:", first, "last:", last, "ordered:", ordered)

exercises/04_loops.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Exercise 04 — Loops.
2+
3+
Tutorial: https://pythonbeginner.help/learn/
4+
5+
TODO: using a loop,
6+
1. Build `squares` = [0, 1, 4, 9, 16] (squares of 0..4).
7+
2. Set `total` = the sum of the numbers 1..10 inclusive.
8+
9+
Run: python exercises/04_loops.py
10+
"""
11+
12+
# --- your code below -------------------------------------------------------
13+
squares = []
14+
# TODO: loop over range(5) and append i * i to squares
15+
16+
total = 0
17+
# TODO: loop over range(1, 11) and add each number to total
18+
19+
# --- self-check (don't edit) ----------------------------------------------
20+
assert squares == [0, 1, 4, 9, 16], "squares should be the squares of 0..4"
21+
assert total == 55, "total should be 1+2+...+10"
22+
print("✅ Passed!", "squares:", squares, "total:", total)

exercises/05_functions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Exercise 05 — Functions.
2+
3+
Tutorial: https://pythonbeginner.help/learn/
4+
5+
TODO:
6+
1. Write `greet(name)` that returns "Hello, <name>!".
7+
2. Write `is_even(n)` that returns True if n is even, else False.
8+
9+
Run: python exercises/05_functions.py
10+
"""
11+
12+
# --- your code below -------------------------------------------------------
13+
def greet(name):
14+
... # TODO: return f"Hello, {name}!"
15+
16+
17+
def is_even(n):
18+
... # TODO: return n % 2 == 0
19+
20+
21+
# --- self-check (don't edit) ----------------------------------------------
22+
assert greet("Ada") == "Hello, Ada!", "greet should return 'Hello, Ada!'"
23+
assert is_even(4) is True, "is_even(4) should be True"
24+
assert is_even(7) is False, "is_even(7) should be False"
25+
print("✅ Passed!", greet("Ada"), "| is_even(4):", is_even(4))

solutions/01_variables.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Solution 01 — Variables & types. https://pythonbeginner.help/learn/"""
2+
3+
name = "Ada"
4+
age = 36
5+
age_text = str(age)
6+
7+
assert isinstance(name, str) and name != ""
8+
assert isinstance(age, int)
9+
assert age_text == str(age)
10+
print(f"✅ Passed! {name} is {age_text} years old.")

solutions/02_strings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Solution 02 — String methods & slicing. https://pythonbeginner.help/learn/"""
2+
3+
phrase = "python is fun"
4+
5+
shout = phrase.upper()
6+
first5 = phrase[0:5]
7+
words = phrase.split()
8+
9+
assert shout == "PYTHON IS FUN"
10+
assert first5 == "pytho"
11+
assert words == ["python", "is", "fun"]
12+
print("✅ Passed!", shout, "|", first5, "|", words)

solutions/03_lists.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Solution 03 — Lists. https://pythonbeginner.help/learn/"""
2+
3+
nums = [3, 1, 2]
4+
5+
nums.append(5)
6+
first = nums[0]
7+
last = nums[-1]
8+
ordered = sorted(nums)
9+
10+
assert nums == [3, 1, 2, 5]
11+
assert first == 3
12+
assert last == 5
13+
assert ordered == [1, 2, 3, 5]
14+
print("✅ Passed!", nums, "first:", first, "last:", last, "ordered:", ordered)

0 commit comments

Comments
 (0)