Python Quest

Learning map · Track 5 · beginner

Functions and Program Structure

Up to now your programs have run from the first line to the last, and every time you needed the same calculation twice you wrote it twice. This track teaches you to package logic into functions: named, reusable pieces of code that take inputs through parameters and hand back results with return. You will learn the difference between returning and printing (the single most common source of confusion for new programmers), how default and keyword arguments make functions flexible, how scope keeps each function's variables to itself, and how docstrings and type hints document what a function expects.

The second half is about structure: breaking a problem such as an invoice or a grade report into small functions that call each other, writing a main() function, accepting any number of arguments with *args and **kwargs, keeping functions pure so they can be tested in isolation, solving problems recursively, and passing functions around as values with lambda and key=. It is for learners who already know variables, types, f-strings, conditions, loops and simple lists. Every exercise from here on is a function that the grader calls with many different inputs, including the awkward ones.

By the end of this track you can

  • Define functions with positional, default, keyword, *args and **kwargs parameters and call them correctly
  • Return values (including several at once) instead of printing them, and use the results in further calculations
  • Explain local and global scope and write functions that communicate only through parameters and return values
  • Document functions with docstrings and type hints and break a problem into small, testable functions with a main()
  • Write recursive functions with a correct base case and recursive case
  • Pass functions as values, write lambda expressions and use key= with sorted(), max() and min()

Lessons

  1. 1Defining and calling functionsGive a block of code a name with def, run it whenever you need it by calling it, and choose good function names.25 min · 4 exFree
  2. 2Parameters and argumentsGive functions inputs: declare parameters in the definition and pass arguments in the call, in the right order.30 min · 4 exMembers
  3. 3Return valuesSend a result back to the caller with return, understand why that is different from print, and recognise None.35 min · 4 exMembers
  4. 4Default and keyword argumentsMake parameters optional with default values, pass arguments by name, and avoid the mutable default trap.35 min · 4 exMembers
  5. 5Scope: local and global variablesUnderstand where a variable exists, why assigning inside a function creates a local variable, and why global state causes trouble.35 min · 4 exMembers
  6. 6Returning multiple valuesReturn several results at once as a tuple and unpack them into separate variables at the call site.30 min · 4 exMembers
  7. 7Docstrings and type hintsDocument what a function does with a docstring and what it expects and returns with type hints.30 min · 4 exMembers
  8. 8Decomposition: building programs from functionsBreak a problem into small functions that each do one job, combine them, and organise the program around a main() function.40 min · 4 exMembers
  9. 9Any number of arguments: *args and **kwargsAccept any number of positional arguments with *args and named arguments with **kwargs, and unpack lists and dictionaries into calls.35 min · 4 exMembers
  10. 10Pure functions and side effectsWrite predictable functions that depend only on their arguments, avoid surprising changes to the caller's lists, and check functions with assert.35 min · 4 exMembers
  11. 11Recursion: functions that call themselvesSolve a problem by reducing it to a smaller version of itself, with a base case that stops the recursion.40 min · 4 exMembers
  12. 12Functions as values, lambda and key=Treat functions as values you can store and pass around, write short lambda functions, and customise sorted(), max() and min() with key=.40 min · 4 exMembers