llampda

Python Lambda Functions

Python Lambda Functions

A lambda function is a small anonymous function defined using the lambda keyword.

Syntax

    
      lambda arguments: expression
    
  

Here is an example of a simple lambda function:

      
        add = lambda x, y: x + y
        print(add(5, 3))  # Output: 8
      
    

Lambda functions are often used in places where you need a small function for a short period of time.

Activity

Try It Yourself!

Write a lambda function that returns the square of a number and use it in the map() function to square a list of numbers.

Quick Quiz

Quick Quiz

  1. What is the syntax of a lambda function in Python?
  2. Where are lambda functions commonly used?

Answers: The syntax is lambda arguments: expression. Lambda functions are commonly used in functions like map(), filter(), and sorted().