Session 2 of 5 · 60 minutes
Writing Good Prompts
Learn the parts of a clear prompt, then build prompt templates with Python.
Goals
By the end of this session you can:
- Write a prompt with a role, a task, context and a format
- Use examples in a prompt to show the model what you want
- Improve a weak prompt step by step
- Build a reusable prompt template with a Python f-string
Words to know
- prompt
- The text you give a model to tell it what to do.
- role
- Who you want the model to act as, such as a patient science teacher.
- context
- Background facts the model needs, like the reader's age.
- few-shot prompt
- A prompt that includes a few examples of the input and the answer you want.
- template
- A prompt with blanks that your program fills in each time.
The lesson
Step 1: A prompt is a clear message
A model can only work with what you give it. A vague prompt gets a vague answer.
Compare:
- Weak:
Volcanoes - Better:
Explain how volcanoes erupt. - Best:
You are a friendly science teacher. Explain how volcanoes erupt to a 9-year-old in 3 short sentences.
The best prompt tells the model who it is, what to do, for whom, and in what shape.
Step 2: Four parts of a strong prompt
| Part | What it does | Example |
|---|---|---|
| Role | Sets the voice and level | You are a friendly science teacher |
| Task | Says exactly what to do | Explain how volcanoes erupt |
| Context | Gives the background | The reader is a 9-year-old in Tanzania |
| Format | Controls the shape | In 3 short sentences |
You don’t always need all four. But when an answer disappoints you, ask yourself: which of the four did I forget?
Step 3: Show an example
Sometimes the easiest way to explain what you want is to show it. Adding a few examples is called few-shot prompting.
Turn each word into a simple definition for a child.
Word: planet
Definition: A huge ball of rock or gas that travels around a star.
Word: river
Definition: A long stream of fresh water that flows to the sea.
Word: volcano
Definition:The model sees the pattern (word, then a one-line child-friendly definition) and continues it. Examples are especially good for style and format.
Step 4: Templates in Python
In an app you don’t type a new prompt every time. You write a template with blanks, and the program fills them in. Python’s f-strings are perfect for this.
def explain_prompt(topic, age):
return (
f"You are a friendly teacher. Explain {topic} "
f"to a {age}-year-old in 3 short sentences."
)
print(explain_prompt("rainbows", 8))
print(explain_prompt("electricity", 12))You are a friendly teacher. Explain rainbows to a 8-year-old in 3 short sentences.
You are a friendly teacher. Explain electricity to a 12-year-old in 3 short sentences.Good enough, but notice “a 8-year-old” should be “an 8-year-old”. Small details like this are why you test prompts and read the results.
You can keep many templates in a dictionary and pick one by name:
recipes = {
"explain": "Explain {topic} to a {age}-year-old in 3 short sentences.",
"quiz": "Write 3 quiz questions about {topic} for a {age}-year-old.",
}
def build_prompt(name, topic, age):
return recipes[name].format(topic=topic, age=age)
print(build_prompt("quiz", "volcanoes", 10))Write 3 quiz questions about volcanoes for a 10-year-old.Step 5: Improve, test, check
Good prompts come from a loop:
- Write a first version.
- Test it with several different inputs, not just your favorite one.
- Read the answers critically. Are they correct? Clear? Safe?
- Improve the prompt and test again.
Safety rule: Never type personal information (full names, addresses, phone numbers, passwords) into an online AI tool. Treat anything you type as something that could be seen by others.
Exercise
Fix the weak prompt
- Start with this weak prompt:
Tell me about volcanoes. - Rewrite it three times, adding one thing each time: first a role and reader, then a format, then an example of the style you want.
- Try each version in any AI chat tool, or swap with a partner and predict how the answers would differ. Write down what changed.
- Which ingredient made the biggest difference?
Need a hint?
Try: You are a friendly science teacher. Explain volcanoes to a 9-year-old in 3 short sentences.
Small project
Prompt recipe cards
Make a small set of prompt templates in Python that other people can reuse.
- Create a dictionary called
recipeswith at least three templates, such asexplain,quizandsummarize. Each uses{topic}and{age}placeholders. - Write a function
build_prompt(recipe_name, topic, age)that fills in the template and returns the finished prompt. - Print three finished prompts and read them aloud. Would a stranger understand what you want?
- Test two of your prompts with an AI chat tool. Improve one template based on the results.
Stretch it: Add a format placeholder so the user can choose a list or a paragraph.
Quiz
Check your understanding
Pick one answer for each question. Your score appears right in the page; nothing is sent anywhere.
Feedback on this lesson
Teachers and students: tell us what worked and what didn't. We read this to improve the curriculum.
Received
Thank you for the feedback.
The Education team reviews lesson feedback to improve the curriculum.
Preview mode: this form is not connected to a destination yet, so nothing was sent. Set its web address in src/data/config.json.