Guided pathThis is part of Understand Transformers & BERTBack to the path

Writing your own tiny function

P41.using-functions.02 · Audience: guest, language-pro, it-ml · Prerequisites: Calling a function — a tool with an input and an output

Real LLM grading for this pageLLM grading (this page):

Some tidying you do again and again: capitalise a heading, trim a stray space, tag a term with its length. Doing it by hand each time is where mistakes creep in. When a small task keeps coming back, you can wrap it up once, give it a name, and reuse it — the same way you called len and sorted, except now the tool is yours. This module writes your first two-line function.

Step 1 / 3 — def names a new tool
ⓘ Concept: def is how you make a function
def — short for define — starts a new function.def greet(name): reads as "define a tool called greet that takes one input called name." The input in the brackets is a stand-in: whatever value you pass in later arrives under that name inside the function.

Why it matters — Writing your own function is the moment the notebook stops being a calculator and becomes yours: you name a task once and reuse it forever.

Ask the mentor about this module

Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.

Images, PDF or text. Kept on this device only.
Keeping your files on this device

Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.

Ctrl/Cmd + Enter to send

🎓 Practice ladder

1 graded rung · ~5 min

Your turn: write a two-line function that takes a word and hands back a small fact about it — its length. Define it with def, return the answer, then call it. Press Run.

Explorer rung — write a two-line function

Loading exercise…

My notes on this module

Loading your notes...

Writing your own tiny function — TransformerLab