Skip to contentSkip to navigation

Module 6

Functional Python and Modules

Harness lambdas, higher-order functions, modular architecture, decorators, and generator expressions.

// Lessons

5

// Quiz questions

75

// Reading time

125 min

// Exam length

15 Q

// Coding exercises

9

  1. 1Lambda Functions & Higher-Order BuiltinsWrite inline anonymous expressions and combine them with map(), filter(), zip(), and sorted(). 20 min 15 questions
  2. 2Functional Data PipelinesBuild side-effect-free data transformations using pure functions, predicate filters, and functools.reduce. 25 min 15 questions
  3. 3Modules & Package ArchitectureOrganize multi-file projects, configure namespace imports, and use the __name__ == '__main__' entry point idiom. 25 min 15 questions
  4. 4Decorators & Function WrappersIntercept and enhance execution behavior using first-class closures, the @ syntax, and functools.wraps. 30 min 15 questions
  5. 5Generator Expressions & Lazy StreamingProcess large sequence streams with O(1) memory overhead using lazy evaluation syntax. 25 min 15 questions

Coding practice: write it yourself

Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (4 exercises)

All module exercises

Coding practice needs an account. Sign in or create a free ID to run these exercises.

Lab 1: Map squares

Sign-in required

Given nums = [1, 2, 3, 4, 5], use map() and a lambda to square each number. Print the resulting list: [1, 4, 9, 16, 25].

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 2: Filter odd numbers

Sign-in required

Given nums = [10, 15, 20, 25, 30], use filter() to keep only odd numbers. Print the resulting list: [15, 25].

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 3: Sort by price

Sign-in required

Given items = [("Pen", 1.5), ("Notebook", 4.0), ("Eraser", 0.5)], sort items by price in ascending order using sorted() and a lambda key. Print: [('Eraser', 0.5), ('Pen', 1.5), ('Notebook', 4.0)].

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

Lab 4: Reduce product

Sign-in required

Use functools.reduce and a lambda to calculate the product of [2, 3, 4, 5]. Print: Product: 120.

Coding Practice is Locked

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and earn XP.

References & Further Reading

Related entries from PyLabs's own reference library.

  • map() (Apply a function to each item in one or more iterables.)
  • filter() (Keep items that pass a test.)