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
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
Applications min() and max()
Map Function and Lambdas
The Filter Function
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become 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.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.