Lesson · Python for Beginners
Mini project: a study-session tracker
A small complete program is a chance to connect ideas rather than learn another isolated syntax feature.
What you will learn
- Design a small data record
- Write and call a helper function
- Persist a result and verify its output
Define a small goal
This project records one topic and a positive number of minutes. It intentionally avoids accounts, network access, and a large interface: the goal is to practise the Python ideas from earlier lessons. Write down the expected input and output first, then separate the calculation from the formatting.
Keep the calculation reusable
A function can validate a duration and return a record that another part of the program stores. Use a list when you later want to keep several records in memory, and a dictionary when each record has named fields. Simple data structures make the first version easier to inspect.
Persist only what you need
The example uses JSON to write a small record into a temporary directory. JSON represents common data such as strings, numbers, lists, and dictionaries. A temporary file makes the example safe to run repeatedly; a real tracker would need a deliberate location and a plan for preserving earlier sessions.
Test normal and invalid cases
Run the program with a positive duration, then try zero or text. A useful program states its rule and does not save an invalid record as if it were valid. Change one thing at a time, read the result, and keep the example small enough that you can explain each statement.
import json
from pathlib import Path
from tempfile import TemporaryDirectory
def make_session(topic, minutes):
if minutes <= 0:
raise ValueError('minutes must be positive')
return {'topic': topic, 'minutes': minutes}
with TemporaryDirectory() as folder:
path = Path(folder) / 'session.json'
session = make_session('Python', 25)
path.write_text(json.dumps(session), encoding='utf-8')
saved = json.loads(path.read_text(encoding='utf-8'))
print(f"{saved['topic']}: {saved['minutes']} minutes")
Expected output
Python: 25 minutes
Practice exercise
Extend the project to keep two session records in a list and print their total minutes.
Show a hint
Sum the minutes values from each dictionary.
Show one possible solution
Create a list of two make_session results and calculate sum(session['minutes'] for session in sessions) before printing it.
Quick check
Zero is a valid number in Python; the check enforces a rule chosen for this application.