What I Learned
Core competencies and skills developed throughout this module
Python Basics & Running Scripts
Set up Python 3 and learned how scripts are written, saved as .py files and executed from the command line. Python uses indentation instead of braces, so clean formatting is part of the syntax itself.
- Running scripts with python script.py from the command line
- Testing ideas quickly in the interactive interpreter
- Understanding indentation as the way code blocks are defined
- Writing comments and docstrings to explain a script
Variables & Data Structures
Worked with Python's dynamic typing and its built-in data structures, choosing the right one depending on whether data is ordered, changeable or needs fast lookups by key.
- Using int, float, str and bool and converting between them
- Lists with indexing, slicing and list comprehensions
- Dictionaries for key-value data such as configurations
- Tuples for fixed values and sets for unique entries
Control Flow: Decisions & Loops
Controlled the flow of a script with conditions and loops, the building blocks that let a script react to its input and process many items automatically.
- Making decisions with if, elif and else
- Iterating over lists, strings and files with for loops
- Repeating work with while loops until a condition is met
- Steering loops with break, continue and the loop else block
Functions & Modules
Structured scripts into reusable functions and used the standard library through import instead of reinventing common functionality.
- Defining functions with parameters, default values and return values
- Avoiding duplicated code by extracting logic into functions
- Importing modules such as os, shutil, time and random
- Using if __name__ == "__main__" to make scripts importable
Automating Files & Directories
Read, wrote and organised files and folders from a script, the core of most everyday automation tasks like processing logs, exports or backups.
- Opening files with the right mode (r, w, a, x)
- Using with open(...) so files are always closed again
- Reading line by line and writing results to new files
- Listing, moving and archiving files with os and shutil
Error Handling & Planning with Flowcharts
Made scripts robust against invalid input and missing files, and planned their logic beforehand with program flowcharts (PAP).
- Catching exceptions with try, except, else and finally
- Handling typical errors like ValueError and FileNotFoundError
- Validating user input read with input()
- Drawing program flowcharts before writing the script
Code Examples
Practical examples from the module
""" Reads a log file and counts the entries per level. Demonstrates dictionaries, loops, file handling and error handling. """ from collections import Counter def summarize(path): counts = Counter() with open(path, "r", encoding="utf-8") as f: for line in f: # Expected format: "2026-03-14 09:12:01 ERROR Disk full" parts = line.split() if len(parts) >= 3: counts[parts[2]] += 1 return counts try: result = summarize("server.log") for level, amount in result.most_common(): print(f"{level:<8} {amount}") except FileNotFoundError: print("The log file does not exist.")
import os import shutil import time SOURCE = "downloads" ARCHIVE = "archive" MAX_AGE_DAYS = 30 def cleanup(source, archive, max_age_days): """Moves files older than max_age_days into the archive folder.""" os.makedirs(archive, exist_ok=True) limit = time.time() - max_age_days * 24 * 60 * 60 moved = 0 for name in os.listdir(source): path = os.path.join(source, name) if os.path.isfile(path) and os.path.getmtime(path) < limit: shutil.move(path, os.path.join(archive, name)) moved += 1 return moved if __name__ == "__main__": count = cleanup(SOURCE, ARCHIVE, MAX_AGE_DAYS) print(f"{count} file(s) archived.")
Skills Acquired
Technologies and concepts mastered in this module
Key Takeaways
- Automate the boring parts: Any task you repeat more than twice is a candidate for a script.
- Readability is built in: Python's indentation forces a clean structure from the start.
- Use the standard library: Modules like os and shutil already solve most file tasks.
- Expect failure: A good script handles missing files and bad input instead of crashing.
- Plan first: A quick flowchart makes the script's logic obvious before the first line of code.