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
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.
ⓘ 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.
🎓 Practice ladder
1 graded rung · ~5 minYour 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…
Where next?
Go up a level