🎓 AIIQM-WELL University · Builder · Module B1 of 5

Python That Does Real Work
Functions, Files, and Logic

60–75 min
📋 Prerequisites: Foundation complete, can run a .py file
📊 Builder Level
Lesson Outcome: You can write functions, read a text file, use if/else logic, and produce structured output.
AIMY: In Foundation you wrote Python that printed things. That was correct for F4. In Builder, Python needs to actually do work — read a file, make a decision, write a result. The difference between print("hello") and a function that reads your notes and summarizes them is three concepts: functions, file I/O, and conditional logic. This module covers all three.
ANALYZE

Three Concepts That Power Real Python

1

Functions encapsulate logic

def my_function(input): ... return output. You define the work once. You call it with different inputs whenever you need it.
2

File I/O reads and writes files

with open("file.txt", "r") as f: content = f.read() reads a file. with open("output.txt", "w") as f: f.write(result) writes one.
3

Conditional logic makes decisions

if condition: do_this else: do_that. AI tools use this constantly to route between options.
4

Putting it together

A Python function that reads a text file, checks if it's longer than 100 words, and prints "Long document" or "Short document" uses all three concepts at once.
INTEGRATE

Write a Document Checker Function

  1. Create a new file in aiiqm-workspace: touch document_checker.py
  2. Write a function:
    def check_length(filename): with open(filename, "r") as f: words = f.read().split() if len(words) > 100: return "Long document" else: return "Short document"
  3. Create a test file: touch test_doc.txt — add some sample text (paste any paragraph you have)
  4. At the bottom of document_checker.py, add:
    result = check_length("test_doc.txt") print(result)
  5. Run: python document_checker.py
  6. Modify test_doc.txt to be longer or shorter — run again — observe how the output changes.
PTR (PROOF THAT IT RUNS)
  • Your function runs without errors.
  • Changing the length of test_doc.txt changes the output.
  • You can explain what each line in the function does.

Common Mistakes

IndentationError
Python uses 4-space indentation inside functions. Every line inside def and if must be consistently indented.
FileNotFoundError
Make sure you're running the script from the same folder as the .txt file. Use pwd to verify your location.
CHECKPOINT
  • You defined and called a function.
  • You read a file with open().
  • You used if/else logic.
  • You changed the input and observed the output change.
AIM COMMITMENT

Analyze: You understood functions, file I/O, and conditional logic as the three building blocks of real Python work.

Integrate: You built a working document checker that reads a file and makes a decision.

Manage: You now have a reusable pattern — read input, apply logic, produce output — that works for any text-based AI tool.