Posts tagged

Functions

Advanced Functions: Default Arguments, Lambda Functions, and Scope

Builds on basic Python functions by introducing three practical concepts: default arguments that make parameters optional and simplify call sites, lambda expressions for writing short anonymous functions inline, and Python's scoping rules distinguishing local from global variables. Includes examples of using lambdas as sort keys with sorted(), modifying global state with the global keyword, and a practical exercise that builds a flexible number-sorting function using these techniques together.

Introduction to Functions: Organizing Code with Functions

Introduces functions as named, reusable blocks of code and makes the case for using them: reduced duplication, logical organization, easier maintenance, and improved readability. Demonstrates defining a function with def, calling it, passing parameters to make behavior dynamic, and using return to send results back to the caller. Practical exercise builds a four-operation calculator where each operation is its own function, with a division guard for zero input. Sets up the next lesson on default arguments, lambdas, and scope.