⚡0 XP🔥0

Zero-Shot and Few-Shot Prompting

Lesson 6 of 19 ⏱ 4 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs Python

Get consistent results by showing examples - zero-shot vs one-shot vs few-shot prompting, choosing good examples, formatting them, and when examples help most.

Zero-shot prompting#

Zero-shot = you give instructions only, with no examples. Modern models handle many tasks this way:

Classify the sentiment of this review as positive, negative or neutral:
"The trainer explained recursion really well, but the AC was not working."

Zero-shot works well when the task is common and the expected output is obvious.

Few-shot prompting#

Few-shot = you include a few examples (usually 2-5) of input → output. The model picks up the pattern: style, format, length, labels, level of detail.

Convert each course feedback into a category and a one-line action.

Feedback: "The lab computers keep freezing during practicals."
Category: Infrastructure
Action: Ask IT to check lab PCs before Monday batch.

Feedback: "Please share recordings, I missed two classes due to exams."
Category: Content access
Action: Upload class recordings to the student portal.

Feedback: "Sir goes too fast in the OOP chapter."
Category: Teaching pace
Action: Add one revision session for OOP with extra practice.

Feedback: "Fee receipt not received for the second instalment."
Category:

The model continues the pattern and produces a category and an action in exactly your format.

हिंदी में समझें

Zero-shot = AI को सिर्फ निर्देश देना, कोई उदाहरण नहीं। Few-shot = निर्देश के साथ 2-5 उदाहरण देना - "ऐसे input पर ऐसा output चाहिए"।

इसे ऐसे समझें: नए employee को सिर्फ बोलकर समझाने से अच्छा है कि आप उसे पहले से भरे हुए 2-3 form दिखा दें। फिर वह उसी तरह बाकी form भर देगा। AI भी उदाहरण देखकर आपका format, भाषा, लंबाई और style तुरंत पकड़ लेता है।

जब आपका format खास हो, categories आपकी अपनी हों, या style बताना मुश्किल हो - तब few-shot सबसे ज़्यादा काम आता है।

Zero, one or few?#

Technique Examples Use when
Zero-shot 0 Common tasks with obvious output (summarise, translate, explain)
One-shot 1 You mainly need to show the format
Few-shot 2-5+ Custom categories, specific style, tricky formats, consistent tone

How to choose good examples#

  1. Cover different cases - include easy and tricky inputs, and every category at least once.
  2. Vary the surface - different lengths, topics and wording, so the model doesn't copy irrelevant details.
  3. Be 100% correct and consistent - the model will copy mistakes too.
  4. Use the exact format you want back - same labels, same capitalisation, same separators.
  5. Keep them realistic - examples like your real data work best.
Common Mistake

If all your examples are about mobile phones, the model may bring phones into answers about laptops. If all examples are short, answers will be short. Examples teach everything they contain, not just what you intended.

Wrapping examples in tags#

For longer examples, tags keep things clear:

Write a product description in our brand style. Here are examples:

<example>
Product: Steel water bottle 1L
Description: Cold for 24 hours, hot for 12. Leak-proof lid, fits every
bag side pocket. Your daily hydration partner - from college to office.
</example>

<example>
Product: Cotton laptop sleeve 15"
Description: Soft cotton outside, padded inside. Slim enough for any
backpack, tough enough for daily commutes. Your laptop's best friend.
</example>

Now write one for:
Product: Wireless mouse with silent clicks

Few-shot for classification - a common business use#

▶ Try it YourselfPython
Ctrl + Enter to run

Examples + instructions = best of both#

Examples show the pattern; instructions explain the rules. Use both:

Rewrite each sentence in simple Hindi for Class 6 students.
Rules: use common words, keep English technical terms in brackets.

English: The CPU processes instructions.
Hindi: CPU (सीपीयू) निर्देशों (instructions) को पूरा करता है।

English: RAM stores data temporarily.
Hindi:

Summary#

  • Zero-shot: instructions only - fine for common tasks.
  • Few-shot: 2-5 input→output examples teach format, style and custom labels.
  • Choose examples that are correct, varied, realistic and in your exact output format.
  • Wrap long examples in <example> tags; combine examples with clear rules.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Build a few-shot prompt

Write few_shot(instruction, examples, new_input) where examples is a list of (input, output) tuples. Return the prompt in this exact format (blank line between blocks):

INSTRUCTION

Input: in1
Output: out1

Input: in2
Output: out2

Input: NEW_INPUT
Output:
2

Parse labelled answers

An AI returned labels like "1. positive\n2. negative\n3. neutral". Write parse_labels(text) that returns the labels as a list: ["positive", "negative", "neutral"]. Ignore empty lines and extra spaces.

Quick Quiz

Q1.

What is few-shot prompting?

Q2.

Which task benefits MOST from few-shot examples?

Q3.

What is a risk of using examples that are all very similar?

Q4.

Zero-shot prompting means...