Skip to contentSkip to navigation

Module 1 of 6, Practical Python

Functional Tools and Modules

Write small functions inline, build collections in one expression, and split code into modules.

module checksSign in to track

  • Lessons complete-, not done
  • Lesson quizzes taken-, not done
  • Module Labs passed-, not done
  • Module exam passed at 80%-, not done
lessons
3
quiz questions
36
reading time
24 min
exam length
15 Q
coding exercises
7

Lessons

  1. Lambda Functions and Higher-Order BuiltinsPass functions as values: write small lambdas and use them with sorted, min, max, map and filter, then pair data with zip and test it with any and all. 8 min 12 questions
  2. List ComprehensionsBuild new lists, dictionaries and sets in one readable line: transform every item, keep only the ones you want, and know when a plain loop is clearer. 8 min 12 questions
  3. Modules and PackagesReuse code across files: import modules from the standard library and your own project, choose between import styles, and guard a script's main code with __name__. 8 min 12 questions

Lab: write it yourself

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

All module exercises

The Lab 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].

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award 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].

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award 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)].

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

Lab 4: Statistics mean

Sign-in required

Compute the arithmetic mean of [10, 20, 30, 40] using statistics.mean(). Print: Average: 25.0.

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award 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.)