Writing your own tiny function
P41.using-functions.02 · Audience: Guest, Language professional, IT/ML learner · Prerequisites: Calling a function — a tool with an input and an output
Where next?
This module unlocks
Go up a level
P41.using-functions.02 · Audience: Guest, Language professional, IT/ML learner · Prerequisites: Calling a function — a tool with an input and an output
This module unlocks
Go up a level
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.
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 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.
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.
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.
Loading exercise…
Loading your notes...