beginner pathway
Python Fundamentals for Useful Small Tools
Use Python to read local text, transform it, and print useful results.
What you will learn
- Read and write simple scripts
- Use pathlib safely
- Test one small function
Download course manifest
Reference word counter
1. Your First Python Script
Prerequisites
Objectives
- Recognize variables
- Run a Python file
- Print a result
A Python script is a plain text file of instructions. Start with visible output and small inputs. A useful beginner script should be easy to read and easy to rerun.
Worked example
message = 'progress'; print(message.upper())
Exercise
Write a script that stores your learning goal in a variable and prints it.
Assessment
What function displays text in the terminal?
Reveal worked answer after your attempt
print sends a visible result to standard output.
Sources
- Python Tutorial — Python Software Foundation; Python Software Foundation License Version 2; examples additionally 0BSD; source review: VERIFIED
2. Build a Word Counter
Prerequisites
Objectives
- Read a local file
- Split text into words
- Handle a missing file
A word counter is small but real: it accepts a path, reads text, splits on whitespace, counts the words, and reports a clear error when the file is missing.
Worked example
from pathlib import Path
text = Path('notes.txt').read_text(encoding='utf-8')
print(len(text.split()))
Exercise
Explain how you would turn the example into a command that accepts a file path.
Assessment
Which method reads text from a pathlib Path?
Reveal worked answer after your attempt
Path.read_text reads a text file using the chosen encoding.
Sources
Prerequisites
Objectives
- Separate logic from printing
- Check expected output
- Keep tests small
Testing turns a script into a dependable tool. Put the count logic in a function, test that function with known text, then keep the command-line wrapper thin.
Worked example
def count_words(text: str) -> int:
return len(text.split())
assert count_words('open knowledge') == 2
Exercise
Write one test case for empty text and one for three words.
Assessment
Why test a function instead of only looking at printed output?
Reveal worked answer after your attempt
A function test is repeatable and can fail clearly when behavior changes.
Sources
- unittest: Unit testing framework — Python Software Foundation; Python Software Foundation License Version 2; examples additionally 0BSD; source review: VERIFIED
Final mini-project
Build and test a tiny word-count script for local text files.
Project rubric
- The script accepts or clearly identifies its input file
- Repeatable tests cover empty text and a known word count
- The evidence records the exact command and observed result
Use these criteria for self-review. Independent verification is still required before claiming mastery.