Lambda Functions in Python: A Simple Introduction

A lambda function is an anonymous function in Python. It starts with the keyword lambda, followed by a comma-separated list of zero or more arguments, followed by the colon and the return expression. For example, lambda x, y, z: x+y+z would calculate the sum of the three argument values x+y+z.

Here’s a practical example where lambda functions are used to generate an incrementor function:

Exercise: Add another parameter to the lambda function!

Watch the video or read the article to learn about lambda functions in Python:

Puzzle. Here’s a small code puzzle to test your skills:

def make_incrementor(n):
    return lambda x: x + n
f = make_incrementor(42)
print(f(0))
print(f(1))

To test your understanding, you can solve this exact code puzzle with the topic “lambda functions in Python” at my Finxter code puzzle app.

When to use lambda functions?

“If you don’t mind, can you please explain, with examples, how we are supposed to use ‘lambda’ in our Python programming codes?”Colen, Finxter user

Lambda functions are anonymous functions that are not defined in the namespace (they have no names). The syntax is:

lambda <argument name>  : <return expression>. 

First of all, don’t use lambda functions if it doesn’t feel natural. In contrast to many other Python coders, I’m no big fan of creating fancy Pythonic code that nobody understands.

Having said this, I must admit that I use lambda functions quite frequently. Here is how I use lambda functions in one of my puzzles (you may recognize it from the CBP book).

def encrypt(s1):
    s2 = map(lambda c : chr(ord(c) + 2), s1)
    return ''.join(s2)


def decrypt(s1):
    s2 = map(lambda c : chr(ord(c) - 2), s1)
    return ''.join(s2)


s = "xtherussiansarecomingx"
print(decrypt(encrypt(encrypt(s)))==encrypt(s))

Exercise: What’s the output of this code?

The encrypt function shifts the string by two Unicode positions to the right. The decrypt function does the exact opposite shifting the string s1 to the left. Hence, the output is “True”.

To answer the question, I use lambda functions only as an input argument for functions such as map() or filter(). For example, the map function applies the argument function (anonymous or not – doesn’t matter) to each element of a sequence. But it’s often cleaner to define the function first and giving it a human-readable name.

Let’s have a look at an interactive video course devoted only to the wonderful Python lambda function!

Lambda Functions Video Course

Overview

Applications min() and max()

Parameterless Lambdas

Map Function and Lambdas

Stacking Lambdas

The Filter Function

If-Else Loops

Customize Sort()

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!