0Pricing
Python For Kids · Lesson

Lambda Functions

Discover anonymous functions in Python.

Lambda Functions is a free Python For Kids lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python For Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

2

What are Lambda Functions?

Lambda functions are small, anonymous functions defined using the lambda keyword. They are useful for creating simple functions without the need to formally define them using def.

  • Anonymous: They do not have a name.
  • Concise: Typically used for short, simple operations.

Let's explore how lambda functions work!

3

Syntax of Lambda Functions

The basic syntax of a lambda function is:

lambda parameters: expression

Example:

# Defines a lambda function that adds two numbers
add = lambda x, y: x + y

4

Using Lambda Functions

Once you've defined a lambda function, you can use it just like any other function by calling it with arguments.

Example:

# Calls the lambda function to add 5 and 3
result = add(5, 3)
print("Sum:", result)  # Output: Sum: 8

5

Lambda Functions with map()

The map() function applies a lambda function to each item in an iterable (like a list) and returns a map object.

Example:

# List of numbers
numbers = [1, 2, 3, 4, 5]

# Uses map with a lambda to square each number
squared = map(lambda x: x ** 2, numbers)

# Converts the map object to a list and prints it
print(list(squared))  # Output: [1, 4, 9, 16, 25]

6

Lambda Functions with filter()

The filter() function uses a lambda function to filter items in an iterable based on a condition.

Example:

# List of numbers
numbers = [10, 15, 20, 25, 30]

# Uses filter with a lambda to keep only even numbers
even_numbers = filter(lambda x: x % 2 == 0, numbers)

# Converts the filter object to a list and prints it
print(list(even_numbers))  # Output: [10, 20, 30]

7

Lambda Functions with sorted()

The sorted() function can use a lambda function as the key to determine the sort order based on specific criteria.

Example:

# List of tuples with names and ages
people = [("Alice", 30), ("Bob", 25), ("Charlie", 35)]

# Sorts the list by age using a lambda as the key
sorted_people = sorted(people, key=lambda person: person[1])

# Prints the sorted list
print(sorted_people)  # Output: [('Bob', 25), ('Alice', 30), ('Charlie', 35)]

8

Limitations of Lambda Functions

While lambda functions are powerful, they have some limitations:

  • Single Expression: They can only contain a single expression and cannot include multiple statements.
  • No Annotations: Lambda functions do not support type annotations.
  • Readability: Overusing lambda functions can make code harder to read.

Use lambda functions judiciously to keep your code clean and understandable.

10

Great Job!

You've learned how to use lambda functions to create small, anonymous functions in Python. Lambda functions can make your code more concise and are especially useful with functions like map(), filter(), and sorted(). Keep practicing by incorporating lambda functions into your programs to see how they can enhance your coding!

Lambda Functions — illustration 8

9

# Defines a lambda function that multiplies two numbers
multiply = lambda x, y: x * y

# Uses map with the lambda function to multiply each number by 2
numbers = [1, 2, 3, 4]
result = map(multiply, numbers, [2, 2, 2, 2])

# Converts the map object to a list and prints it
print(list(result))

1

Lambda Functions

Welcome to the Functions lesson! Today, we'll discover lambda functions in Python, which are small anonymous functions that can make your code more concise and efficient.

Lambda Functions — illustration 10

Frequently asked questions

Is the “Lambda Functions” lesson free?

Yes — the full text of “Lambda Functions” is free to read here on the web, and the Python For Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python For Kids course, upgrade to CoddyKit PRO.

What will I learn in “Lambda Functions”?

Discover anonymous functions in Python. You practise Python For Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python For Kids?

No prior experience is required. Python For Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Lambda Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python For Kids lesson?

Yes. Every Python For Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What are Functions?
  2. Defining Your Own Functions
  3. Arguments and Return Values
  4. Lambda Functions
← Back to Python For Kids