return, print and changing the caller's list
x is None because add_and_print only prints. The list functions show the difference between changing in place and returning a new list.
A good function does one job, has a clear name and a small signature, returns its result instead of printing it, avoids changing its inputs or hidden state, and uses early returns to keep the main path flat. These habits make code easy to read, test and reuse.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
This function prints instead of returning. Rewrite it so it returns: def area(w, h): print(w * h). Use it to print area(3, 4) + 1.
Write normalised(numbers) that returns a new list with every number divided by the largest one, without changing the input. Show that the original list is unchanged.
Rewrite with guard clauses: def grade(score): returns "invalid" if score is below 0 or above 100, "pass" for 40 or more, otherwise "fail".
Explain it without notes
What is a pure function, and why do programmers like them?
Why should a function return its result instead of printing it?
How should a function treat a list argument it receives?
What is a guard clause, and how does it improve a function?
Give signs that a function should be split.
Expected output
5
x is None
y is 15
after in place: ['pen', 'book']
new cart: ['pen', 'book', 'bag'] old cart: ['pen', 'book']