Welcome to our lesson on anonymous functions, also known as lambda functions, in Python. We'll explore what lambda
functions are, how to create them, and when to use them. By the end, you'll understand the syntax and benefits of lambda
functions, enabling you to write more concise and readable code.
Lambda
functions, or anonymous functions, are small, unnamed functions defined with the lambda
keyword. They allow you to create simple functions concisely.
Lambda
functions have the following syntax:
Python1lambda arguments: expression
The expression is evaluated and returned. Lambda functions can have any amount of arguments, but only one expression.
Lambda
functions are useful when a small function is needed briefly. They offer:
- Conciseness: Reduce verbosity by defining functions on the fly.
- Readability: Improve readability in specific contexts.
- Functional Programming: Support functional programming practices.
Let's start with a basic example to understand lambda
functions. Suppose you need a function to print numbers:
Python1# Regular function to print a number 2def print_number(n): 3 print(n, end=' ') 4 5# Lambda equivalent 6print_number_lambda = lambda n: print(n, end=' ') 7 8# Use the lambda function 9numbers = [1, 2, 3, 4, 5] 10for n in numbers: 11 print_number_lambda(n) 12print() # 1 2 3 4 5
Here, lambda n: print(n, end=' ')
is a lambda
function that takes a single argument, n
, and prints it. It's equivalent to defining a regular function, but more compact.
Lambda
functions can also handle multiple arguments and complex expressions. Consider needing to multiply numbers by a factor:
Python1multiply_by = lambda n, factor: n * factor 2 3# Use the lambda function 4numbers = [1, 2, 3, 4, 5] 5for n in numbers: 6 print(multiply_by(n, 2)) 7print()
Output:
12 24 36 48 510
Here, lambda n, factor: n * factor
is a lambda
function that takes n
and factor
and returns their product.
lambda n, factor
: Defines the arguments.n * factor
: The expression that returns the product.
This example shows how lambda
functions can simplify your code by allowing you to define small functions concisely.
Lambda
functions are useful where small, one-off functions are needed. Let's consider one common scenario:
Python1# List of numbers 2numbers = [10, 20, 30, 40] 3# Calculate squares using lambda function 4squares = [ (lambda x: x * x)(x) for x in numbers] 5for square in squares: 6 print(square) # 100, 400, 900, 1600
This code squares each element in the numbers
list using a lambda
function, creating a new squared
list in one line.
Great job! You now understand the fundamentals of lambda
functions in Python. We've covered:
- What
lambda
functions are and their syntax. - Using
lambda
functions for simple and complex expressions. - Practical use cases for
lambda
functions.
Now that you've learned the theory behind lambda
functions, it's time for hands-on practice. You'll create and use lambda
functions in various scenarios, reinforcing your understanding and skills. Let's get started!