Loading lesson…
A function is a reusable chunk of code with a name. You'll write three, add type hints, and let AI suggest better names and docstrings.
A function packages logic so you can call it by name anywhere. Name it clearly and your code reads like a story. Name it 'do_stuff' and your future self will curse your past self.
def greet(name: str, enthusiasm: int = 1) -> str:
"""Return a friendly greeting.
Args:
name: the person to greet
enthusiasm: number of exclamation marks (default 1)
"""
bangs = "!" * enthusiasm
return f"Hello, {name}{bangs}"
print(greet("Maya"))
print(greet("Maya", enthusiasm=5))Type hints (: str, -> str) are optional but massively improve both human and AI understanding.def add_broken(a, b):
print(a + b) # shows the answer — but returns None
def add_good(a, b):
return a + b # gives the answer back to the caller
result = add_broken(2, 3) # prints 5, but result is None
total = add_good(2, 3) # total is 5 — usable
print(result * 2) # TypeError: NoneType
print(total * 2) # 10If a function needs to be usable in another function, it must return, not print.| Loose function | Tight function |
|---|---|
| def do(x) | def fetch_user(user_id: int) -> dict |
| No docstring | Docstring with Args and Returns |
| Prints results | Returns results |
| Hard to test | Easy to test |
Big idea: a function is a small contract. Its name says what it does, its signature says how to use it, its docstring says why. AI pair-programming gets dramatically better when you tighten those three things up first.
15 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-prog-python-functions-builders
What is the core idea behind "Python Functions — Writing Your Own Tools"?
Which term best describes a foundational idea in "Python Functions — Writing Your Own Tools"?
A learner studying Python Functions — Writing Your Own Tools would need to understand which concept?
Which of these is directly relevant to Python Functions — Writing Your Own Tools?
Which of the following is a key point about Python Functions — Writing Your Own Tools?
Which of these does NOT belong in a discussion of Python Functions — Writing Your Own Tools?
Which statement is accurate regarding Python Functions — Writing Your Own Tools?
Which of these does NOT belong in a discussion of Python Functions — Writing Your Own Tools?
What is the key insight about "Docstrings make AI smarter" in the context of Python Functions — Writing Your Own Tools?
What is the recommended tip about "Review before you run" in the context of Python Functions — Writing Your Own Tools?
Which statement accurately describes an aspect of Python Functions — Writing Your Own Tools?
What does working with Python Functions — Writing Your Own Tools typically involve?
Which best describes the scope of "Python Functions — Writing Your Own Tools"?
Which section heading best belongs in a lesson about Python Functions — Writing Your Own Tools?
Which section heading best belongs in a lesson about Python Functions — Writing Your Own Tools?