Docstrings
Python · Unit 19: Clean code
- Full plan
- Python
- 10 exercises
A docstring is a text between triple quotes, on the first line of a function's body.
It says what the function does, what it takes and what it gives back. Python stores it, and __doc__ reads it.
def double(n):
"""Gives back twice n."""
return n * 2
print(double.__doc__)Prints
Gives back twice n.
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Predict the output
What does this code print?
3. Complete the code
Complete it so the function carries its description.
4. Find the bug
The docstring says what the function should do. Which line has the error?
5. Multiple choice
shipping(order)gives back what shipping costs. Which docstring helps most?6. Put the lines in order
Put the program in order. It takes 10% off the price and also prints the function's description.
7. Predict the output
What does this code print?
8. Predict the output
What does this code print?
9. Find the case that fails
Its docstring promises the initial of each text, skipping the ones that come in empty. Which call fails to keep that promise?
10. Find the bug
The docstring promises to give back the sum. Which line has the error?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.