Python Lambdas and Closures — Explain Like I'm 5

Sticky Notes and Pocket Tools

Think of two helpful things:

  • a pocket tool for quick jobs
  • a sticky note that remembers a number you wrote earlier

In Python, a lambda is the pocket tool. It is a tiny function written in one line.

double = lambda x: x * 2
print(double(5))  # 10

A closure is a function that remembers values from where it was made.

def make_multiplier(n):
    return lambda x: x * n

triple = make_multiplier(3)
print(triple(4))  # 12

triple remembers n = 3, even after make_multiplier is done.

That memory is the closure part.

Why people use them

Lambdas are handy when you need a tiny action once, like sorting:

names = ["Maya", "Bo", "Amir"]
names.sort(key=lambda name: len(name))

Closures are handy when you want custom behavior with saved settings:

  • discount calculators
  • custom formatters
  • small reusable rules

When to keep it simple

If a lambda gets long or hard to read, write a normal def function instead. Clear code beats clever code.

One Thing to Remember

A lambda is a quick one-line function, and a closure is a function that remembers values from where it was created.

pythonlambdaclosuresbeginners

See Also

  • Python Async Await Async/await helps one Python program juggle many waiting jobs at once, like a chef who keeps multiple pots moving without standing still.
  • Python Basics Python is the programming language that reads like plain English — here's why millions of beginners (and experts) choose it first.
  • Python Booleans Make Booleans click with one clear analogy you can reuse whenever Python feels confusing.
  • Python Break Continue Make Break Continue click with one clear analogy you can reuse whenever Python feels confusing.
  • Python Closures See how Python functions can remember private information, even after the outer function has already finished.