Functions and modules

Splitting programs into named pieces, understanding the call stack and scope, passing functions around, and reusing the standard library.

About 8 hours of study

Learning objectives

Lessons

  1. 1

    Functions and recursion

    Write functions, follow the call stack, and see how a function can call itself.

  2. 2

    More about functions

    Default values, keyword arguments, and why a variable inside a function is not the one outside it.

  3. 3

    Functions as values

    Store functions in variables and dictionaries, and write small ones with lambda, map and filter.

  4. 4

    Using modules

    Import ready-made tools from math and random, and learn the two ways to import.

Module practice

Module quiz

1. What is the value of x after x = print("hi")?
2. Which definition is a SyntaxError?
3. What happens with def f(n): return f(n - 1)?
4. What does sorted(words, key=len) do?
5. A function assigns x = 1 inside it, and a global x also exists. What does the assignment change?
6. Why does naming your file random.py cause trouble?

‹ Strings and listsWorking with data ›