Skip to contentSkip to navigation

Module 4 of 6, Python in Depth

Robust Code

Describe types, match the shape of data, and fail early and clearly.

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
28 min
exam length
15 Q
coding exercises
4

Lessons

  1. Type AnnotationsDescribe the types your code expects with annotations: parameters, return values and variables, list[int] and dict[str, float], X | None, Callable and generics, and how type checkers use them while Python itself does not enforce them. 9 min 12 questions
  2. Structural Pattern MatchingPick data apart by its shape with match and case: literal, wildcard and or-patterns, capturing values, sequence and mapping patterns, class patterns, guards, and the traps of bare names and case order. 9 min 12 questions
  3. Assertions and Defensive ProgrammingState your code's assumptions with assert, validate outside input with real exceptions, fail fast with guard clauses, protect shared data with defensive copies, and know why asserts must never guard user input. 10 min 12 questions

Lab: write it yourself

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

All module exercises

The Lab needs an account. Sign in or create a free ID to run these exercises.

Lab 1: Annotate divider

Sign-in required

Define def divide(a: float, b: float) -> float: return a / b. Test with divide(10.0, 4.0) and print: Result: 2.5.

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.