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
- Write functions with parameters and return values
- Explain frames, scope and recursion
- Pass functions as values
- Reuse modules from the standard library
Lessons
- 1
Functions and recursion
Write functions, follow the call stack, and see how a function can call itself.
- 2
More about functions
Default values, keyword arguments, and why a variable inside a function is not the one outside it.
- 3
Functions as values
Store functions in variables and dictionaries, and write small ones with lambda, map and filter.
- 4
Using modules
Import ready-made tools from math and random, and learn the two ways to import.
Module practice
- The bigger of twoEasy
- FactorialMedium
- Fibonacci numberMedium
- Greatest common divisorMedium
- Prime or notMedium
- How many ways to reach a total?Hard
- Full name with an optional titleEasy
- Power with a defaultEasy
- Final price with optionsMedium
- The basket that never emptiesHard
- Apply it twiceEasy
- Calculator with a dictionaryMedium
- A pipeline of functionsHard
- Area of a circleEasy
- HypotenuseEasy
- How many boxes?Medium
- Class statisticsHard