return — give the result back
square(4) printed the result. But what if we want to use that result in another calculation? print writes to the screen and… it’s gone. return, on the other hand, gives the result back — so you can store it in a variable, add it up, or compare it.
return, not print
Section titled “return, not print”Inputs (for input())
One value per line — read in order by input().
The call add(3, 4) becomes the value 7 — as if 7 were written right there. That’s why add(10, 20) + add(1, 2) = 30 + 3 = 33 works. You couldn’t do this with a print-based function!
Remember: return ends the function immediately — any lines after it don’t run.
The secret of len() revealed
Section titled “The secret of len() revealed”Now you know how len() works: it returns its result too. That’s why print(len(my_list)) nests neatly: len returns first, then print prints. You can already write functions like that yourself.
Missions
Section titled “Missions”An add function
+15 XP ✓ CompletedWrite add(a, b) — it should return the sum. The checker will call the function itself to test it, so print won’t be enough — you really need return!
Inputs (for input())
One value per line — read in order by input().
Square 2.0
+15 XP ✓ CompletedRewrite square with return and print the sum of two squares: square(5) + square(3) → 34.
Inputs (for input())
One value per line — read in order by input().
Average grade
+20 XP ✓ CompletedWrite an average(numbers) function — it should return the average of the numbers. Test it with your own grades! (A new friend: sum() returns the total of a list.)
Inputs (for input())
One value per line — read in order by input().
🏅 You finished this module! You now have all of Python’s core tools: variables, conditions, loops, collections, and functions. The final module is the proving ground: four real projects are waiting for you. On to the projects →