Write, run and debug small Python programs. Learn values, decisions, loops, collections, functions, file handling, classes and tests, then build a stock report from fictional CSV data.
Data & PythonTool stack
Python
Technology marks for Python Fundamentals, sourced from the CC0-licensed Simple Icons project.
Use conditions, loops, collections and functions to process records.
Validate input and read CSV data without hiding errors.
Write and run tests for normal and failure cases.
Build a small stock-report program with a clear handover.
Course outline and revision prompts
Module 1
First programs
Run a Python file and inspect the result; Separate text from numeric values; Make a decision with if and elif.
Lesson
Key terms
Revision question
Run a Python file and inspect the result
File, Interpreter, Output
You changed the code but see the old output. What should you check first?
Separate text from numeric values
String, Integer, Formatting
What does '12' + '3' produce?
Make a decision with if and elif
Condition, Branch, Boundary
With quantity = 5 and reorder_level = 5, which comparison includes the boundary?
Instructional figureProcess flow
Run a Python file and inspect the result: the working sequence
Trace the three stages of run a python file and inspect the result and compare them with the example.
1File
stock.py contains instructions saved as plain text.
then
2Interpreter
Python executes the expressions and assigns the resulting values.
then
3Output
print() displays the remaining count for comparison with a hand calculation.
Takeaway: A file must be saved before its changes can be run. Source code and displayed output are different things. Check a small calculation before trusting a larger one. The sequence describes this example; use the code and output to check each stage.
Module 2
Collections and reusable code
Process a collection with a loop; Organise records with lists and dictionaries; Write a function that returns a useful value.
Lesson
Key terms
Revision question
Process a collection with a loop
Iterable, Loop, Accumulator
What is total when quantities is [] in this example?
Organise records with lists and dictionaries
List, Dictionary, Record
What happens when a dictionary literal repeats the same key?
Write a function that returns a useful value
Argument, Function, Return value
A function reaches its end without executing return. What does its caller receive?
Instructional figureProcess flow
Process a collection with a loop: the working sequence
Trace the three stages of process a collection with a loop and compare them with the example.
1Iterable
The list provides three quantities in a defined order.
then
2Loop
The loop adds each quantity to the existing total.
then
3Accumulator
The accumulator finishes at 9; an empty list would leave it at zero.
Takeaway: Initialise an accumulator before the loop. One iteration should correspond to one intended item. An empty collection is an important test case. The sequence describes this example; use the code and output to check each stage.
Module 3
Reliable input and files
Validate a whole-number input in Python; Read CSV records without splitting them by hand; Load JSON and check its structure.
Lesson
Key terms
Revision question
Validate a whole-number input in Python
validation, Python, data
Why does '-2' need a check after int() succeeds?
Read CSV records without splitting them by hand
CSV, Header, Conversion
Why should you avoid line.split(',') for this fixture?
Load JSON and check its structure
JSON, Shape, Validation
What does successful json.loads() establish?
Instructional figureProcess flow
Text to a valid quantity
Text to a valid quantity: a step-by-step view of the code example.
1Parse text
Convert with int(); invalid integer text raises ValueError.
then
2Check range
Accept only positive values; zero and negative integers fail this rule.
then
3Return result
Return the accepted quantity, or None for an invalid entry.
Takeaway: Successful parsing does not establish business validity. Follow each step and compare it with the runnable example below.
Module 4
Build and test a small application
Keep instance data separate in a small class; Test a rule at its boundary; Build a stock report from validated records.
Lesson
Key terms
Revision question
Keep instance data separate in a small class
Class, Instance, Method
Which change should update only the books instance?
Test a rule at its boundary
Requirement, Test case, Regression
Which input exposes changing <= to < in the reorder rule?
Build a stock report from validated records
Input contract, Selection, Handover
An invalid row is found while building a stock report. What is the clearest policy for this example?
Instructional figureProcess flow
Keep instance data separate in a small class: the working sequence
Trace the three stages of keep instance data separate in a small class and compare them with the example.
1Class
StockItem defines how one item's data and behaviour are organised.
then
2Instance
pens and books receive different quantities in separate instances.
then
3Method
The same method evaluates the stored quantity for the object it is called on.
Takeaway: An instance stores data for one object. Methods use the instance supplied as self. Choose a class when grouping data and behaviour helps. The sequence describes this example; use the code and output to check each stage.
Course practical outcome
Build a stock report from a fictional CSV file.
Expected output: stock_report.py, a sample CSV, tests and a short README with example output.
Tools: Python 3, Text editor, Terminal, Fictional CSV file
Production steps
Define CSV columns for item name, quantity and reorder level.
Parse quantities as integers and reject missing names or negative values with a useful message.
Write a function that finds items at or below their reorder level.
Test empty data, a boundary quantity, invalid numeric text and a normal record.
Print a readable report and document how to run the script and tests.
Success criteria
The same input produces the same ordered report.
Boundary and invalid inputs have documented behaviour.
Empty files and bad headers do not produce a misleading success message.
Tests run with one documented command.
Self-review
Can someone run this from my instructions?
Are malformed records reported clearly?
Do the tests check a real failure and a boundary?
Next step: Ask another learner to run your program with one new CSV fixture, then improve the first confusing error message.
Glossary
variable
A name bound to a value in a program.
function
A named, reusable operation that can receive arguments and return a value.
list
An ordered, mutable collection of values.
dictionary
A mapping from unique keys to values.
exception
A runtime error or other exceptional condition that interrupts normal execution.
class
A definition used to create objects with associated data and behaviour.
assertion
A check that raises AssertionError when its condition is false; useful in tests, not a replacement for input validation.
CSV
Comma-separated values: a text format for tabular records that requires proper handling of quoting and delimiters.
Input and output - Python Software Foundation (accessed 2026-10-07)
This guide is generated from DigiLearn course material. Product versions, regulations and professional standards can change; consult the linked authoritative source before applying version-sensitive guidance.