pexels-tara-winstead-8386440
by 
19 Oct/25

Generate Python Functions with AI — A Guided Lab


Welcome to the Lab 🚀

Today, we’re going to use AI as your coding partner. You’ll see how a Large Language Model can turn your problem description into working Python code — instantly. By the end, you’ll be able to generate functions confidently, tweak them, and learn from AI suggestions.


Step 0: Set the Scene

Imagine: You’re building a small math toolkit for students. One core function: factorial.

  • Input: integer n
  • Output: factorial of n (n!)
  • Special case: if n is negative, raise an error.

📝 Mini-challenge: Write down, in plain English, what your function should do. Be as explicit as possible — this is the blueprint for your AI prompt.


Step 1: Crafting Your Prompt

There are many ways to ask the AI. Here are two:

Option A — Zero-shot prompt:

Write a Python function factorial(n) that returns the factorial of n. 
Raise a ValueError for negative inputs.

Option B — Few-shot prompt (with examples):

Example:
5 → 120
0 → 1

Now write a Python function factorial(n) that behaves like this.

💡 Pro Tip: Few-shot prompts give the model context and usually produce cleaner, more reliable code.


Step 2: See the AI Work

AI might generate:

def factorial(n):
    if n < 0:
        raise ValueError("Input must be non-negative")
    result = 1
    for i in range(2, n+1):
        result *= i
    return result

✅ Already looks good! But let’s test it.


Step 3: Test Your Function

print(factorial(5))  # 120
print(factorial(0))  # 1
print(factorial(-1)) # ValueError
  • Works perfectly!
  • If it didn’t, think: “What could I clarify in the prompt?”
  • Maybe add type hints, docstrings, or specify loop vs recursion.

Step 4: Iterative Refinement — Make It Better

Refine prompt to include style or efficiency:

Write a Python function factorial(n: int) -> int using recursion. 
Include a docstring and handle negative inputs with ValueError.

AI Output Example:

def factorial(n: int) -> int:
    """Return the factorial of n. Raise ValueError if n is negative."""
    if n < 0:
        raise ValueError("Input must be non-negative")
    if n == 0:
        return 1
    return n * factorial(n-1)

🔍 Notice the difference? The AI adapted to recursive style and added type hints + docstring.


Step 5: Mini Lab Challenges

Try generating these using AI:

  1. is_prime(n) — check if n is prime, raise errors for invalid inputs.
  2. reverse_string(s) — reverse a string safely; handle empty/invalid input.
  3. Compare zero-shot vs few-shot prompt outputs: which one is cleaner? Why?
  4. Try adding efficiency constraints (O(n) vs O(n^2)) to the prompt — see how the AI adapts.

Step 6: Pro Tips for AI Code Generation

  • Always specify input/output clearly.
  • Few-shot examples = better code.
  • Iteration is normal: AI might not get it right first time.
  • Use AI-generated code to learn coding patterns, not just copy-paste.
  • Include constraints, styles, or documentation in prompts for production-ready code.

Lab Summary 🏁

  • AI is your coding assistant, not a replacement for thinking.
  • Prompt clarity + examples + iteration = key to reliable code.
  • Experiment with style, recursion vs loops, error handling, and documentation.
  • Each small prompt tweak teaches you how the LLM “thinks” and how to guide it effectively.

Leave A Comment

Cart (0 items)
Proactive is a Digital Agency WordPress Theme for any agency, marketing agency, video, technology, creative agency.
380 St Kilda Road,
Melbourne, Australia
Call Us: (210) 123-451
(Sat - Thursday)
Monday - Friday
(10am - 05 pm)