Code of the Day
BeginnerYour First AI Conversation

Lab: Your first AI-assisted program

Use an AI to write a small Python program — then read it, test it, extend it, and understand every line.

Lab · optionalUsing AIBeginner25 min
By the end of this lesson you will be able to:
  • Write a context-rich prompt that produces a working Python function
  • Read and understand AI-generated code line by line
  • Test code with at least three inputs, including edge cases
  • Ask targeted follow-up questions to understand unfamiliar parts
  • Extend the code with an AI-assisted modification

This lab requires a Python environment and an AI chat interface. For Python: your computer's terminal (python3), an online runner like python.org/shell, or a Jupyter notebook all work. For AI: Claude, ChatGPT, or any LLM-based chat tool.

This is where the concepts become habits. You will go through the complete loop — define a task, write a prompt, receive code, read it, test it, ask about it, and extend it — in a single sitting. Do every step in order. Skipping steps defeats the purpose.

Choose a task

Pick one of these small programs, or propose your own variation of similar scope:

Option A: Word counter. A function that takes a string and returns the number of words in it (where a word is any sequence of non-whitespace characters).

Option B: List summariser. A function that takes a list of numbers and returns a dict with three keys: min, max, and average.

Option C: Title-caser. A function that takes a sentence string and returns it with every word capitalised, except common short words like "a", "an", "the", "and", "or", "but", "in", "on", "at" (unless the word is the first word).

All three are straightforward enough to read in a few minutes, and all three have interesting edge cases that AI might handle well or poorly.

Step 1: Write a context-rich prompt

Before you send anything to the AI, write your prompt on paper or in a text file. It should include:

  • The task, stated precisely
  • The language (Python)
  • The input type and output type
  • At least one constraint or edge case you care about
  • The format you want (just the function, with or without comments)

Here is an example for Option A:

Write a Python function called count_words that takes a single string argument and returns the number of words in it. A word is any sequence of non-whitespace characters. The function should handle edge cases including an empty string, a string with only whitespace, and a string with multiple consecutive spaces between words. Return an integer. Include a short comment for each logical step.

Notice what that prompt specifies: the function name, the input type, the return type, the definition of "word," three specific edge cases, and the comment style. The model does not have to guess any of those things.

Write your own prompt for the task you chose, then send it.

Step 2: Read the code before running it

When you receive the function, do not run it yet. Read it.

Work through it line by line, as if you were explaining it to someone who had never seen Python:

  • What does this line do?
  • What does this variable hold at this point?
  • If the input were an empty string, what happens at this line?
  • Are there any method names you do not recognise?

For any method or function you do not recognise, either look it up (docs.python.org is always authoritative) or ask the AI: "In the code you just wrote, what does str.split() do with no arguments? What does it return for an empty string?"

Before moving to Step 3, you should be able to describe what the function does, in plain English, without looking at it.

Step 3: Run it and test three inputs

Run the function in your Python environment. Create three test calls:

  1. The normal case. Something typical — a sentence with a few words.
  2. An empty input. "" or [], depending on your chosen task.
  3. A tricky edge case. For the word counter: " " (spaces only), or "one two three" (multiple spaces). For the summariser: a list with one element. For the title-caser: the first word being one of the small words.
# Example for Option A
def count_words(text):
    # ... the AI-generated function here ...

# Test calls
print(count_words("hello world"))        # Expect: 2
print(count_words(""))                   # Expect: 0
print(count_words("  hello   world  "))  # Expect: 2

Record the actual output. Does it match what you expected? If a case fails or returns something surprising, continue to Step 4 before fixing anything.

Step 4: Ask the AI to explain something

Find one part of the code that you are not completely confident about — even if it appears to work. Ask the AI to explain it:

  • "In your implementation, what exactly happens when split() is called with no arguments on a string with leading spaces?"
  • "Why did you use sum(numbers) / len(numbers) rather than a loop? Are there edge cases where that fails?"
  • "What does capitalize() do to a string that's already capitalised?"

The goal is not to find a bug — it is to build genuine understanding of the code. Code you cannot explain is code you cannot trust in production or debug when it fails.

If the AI's explanation does not quite add up, or if it says something that contradicts what you observed in Step 3, trust your observation. Verify the explanation against the Python documentation or by running a small test. The model's explanation of its own code can also be wrong.

Step 5: Extend it

Now ask the AI to modify the function to do one extra thing. Good options:

  • Option A extension: Return a dict with both the word count and the character count (excluding spaces).
  • Option B extension: Add a fourth key, count, with the number of items in the list. Handle the case where the list is empty by returning all None values rather than crashing.
  • Option C extension: Add an optional parameter small_words that lets the caller supply their own list of words to not capitalise, with the default being the list you already have.

Write the extension request as a follow-up in the same conversation. Include a reference to the existing function: "In the function you wrote above, add..."

When you receive the modified version:

  • Read it again — what changed?
  • Run your three original test cases plus at least one test of the new feature.
  • Confirm the original behaviour is unchanged.

Reflect

Before moving on, write down:

  1. One thing the AI got right on the first try that you would have spent time on if you had written it yourself.

  2. One thing you had to verify or fix — either something you caught while reading, something that failed in testing, or a part where the explanation didn't hold up.

  3. One thing you understand about Python now that you didn't understand (or understand as clearly) before this lab.

These three items are what the lab is for. The function itself is not the deliverable — the habit is.

Where to go next

You have completed the beginner tier's first two modules. You can now describe what AI is and how it works, structure prompts that get useful responses, and verify what comes back before you act on it.

The intermediate tier will build on this foundation: prompt engineering patterns that work across more complex tasks, using AI for code review and debugging, and starting to direct AI on multi-step coding work.

Finished reading? Mark it complete to track your progress.

On this page