Function Basics#

Functions are reusable blocks of code that perform a specific task. They help organize your code, reduce duplication, and make programs easier to read and maintain. Python functions are first-class objects — they can be passed as arguments, returned from other functions, and assigned to variables.

Functions

Defining and calling functions

Functions encapsulate reusable blocks of code

Lambda Functions#

Lambda functions are small, anonymous functions defined in a single expression. They are most useful as short callbacks passed to higher-order functions like sorted(), map(), and filter(). For anything more complex, prefer regular def functions.

Lambda Functions

Anonymous single-expression functions

Lambdas are small anonymous functions for simple operations

Decorators#

Decorators let you modify or extend the behavior of functions and methods without changing their source code. They use the @decorator syntax and are widely used in Python frameworks for logging, authentication, caching, and more.

Decorators

Functions that modify other functions

Decorators wrap functions to add behavior