Radio Inspire

How To Learn Sign Language

Lambda Expressions & Anonymous Functions || Python Tutorial || Learn Python Programming


A mysterious engineer once told me to
sort a massive amount of data. When I asked for the sorting function, I was
told a function has no name. Is this possible? Doesn’t every function need a
name? The answer is no. There are functions that have no name at all. These
nameless functions are known as anonymous functions or lambda
expressions. Today, we will learn how to write and used lambda expressions in
Python. Get ready for some pithy anonymity. Suppose you want to write a function
that will compute the value of 3x plus 1. The standard approach would be to define
a function. Let’s call it f with a single input X. Next, you would return the value
3x plus 1. If you input 2, you get the value 7, so it works nicely.
Let us now do this using anonymous functions. Before we get started, a quick
note. Throughout this video we will use the terms “anonymous functions” and “lambda
expressions” interchangeably. They both mean the same thing. To create a lambda
expression, you type the keyword lambda, followed by your inputs. Next, type a
colon. Finally, enter an expression that will be the return value. This anonymous
function will take the input X and return 3x plus 1, just like the earlier
function f. There is a problem, however. We cannot use this function because it does
not have a name. It is, after all anonymous. lambda is not the name of the
function. It is a Python keyword that says what
follows is an anonymous function. So how do you use it? One way is to give it a
name. Let us call this lambda expression G. Now, you can use this like any other
function. If you input 2, you still get 7. Let us now see a lambda expression with
more than one input. Suppose you are processing user data from a web
registration form, and would like to combine the first and last names into a
single full name for displaying on the user interface. We will call this lambda
expression full name. This anonymous function will have two inputs: first name
and last name. For both the first and last names, we
will remove the leading and trailing whitespace with the strip function. We
will also ensure that only the first letter of each string is capitalized
with the title function. This is necessary because humans are sloppy when
typing. Notice we separated the first and last names with a space.
Let us now test this lambda expression. You use it just like any other function.
Outstanding. We should not judge Euler’s typing skills. This is the first time he
has ever used a computer. Here is the general way to create a lambda
expression: you type the keyword lambda followed by zero or more inputs. Just
like functions, it is perfectly acceptable to have anonymous functions
with no inputs. Next, type a colon. Then finally, you enter a single expression.
This expression is the return value. You cannot use lambda expressions for
multi-line functions. Let us now see a common use of lambda expressions where
we do not give it a name. Suppose we have a list of science fiction authors. We
would like to sort this list by last name. Notice that some of these authors
have a middle name, while others have initials. Our strategy will be to create
an anonymous function that extracts the last name, and uses that as the sorting
value. Lists have a built-in method called sort. To see how to use it call
the help function on the method name. The key argument is a function that will be
used for sorting. We will pass it a lambda expression. To access the last
name, split the string into pieces wherever it has a space. Next, access the
last piece by index negative 1. As a final precaution, convert the string to
lowercase. This way, the sorting is not case-sensitive.
Trust me – some people do not know how to use the shift key. The list is now in
alphabetical order. These names are a pleasure to read. We must go deeper. Next
we will write a function that makes functions. Suppose you are working with
quadratic functions. Perhaps you are computing the trajectories of
cannonballs – something you should know how to do before becoming a pirate. To do this, let’s write a function called build quadratic function. The inputs are the
three coefficients A, B, and C. Naturally, we write a docstring.
And with a single line we return an anonymous quadratic function with these
coefficients. Let’s test this by creating the function 2x squared plus 3x minus 5.
If you test this for the input 0 1 & 2 you can see this function works
correctly. And just for bites and giggles, let’s make and use a quadratic function
without ever giving it a name. Let’s create a different function and then
pass in the value 2. This code creates the function 3x squared plus 1 and
passes in the value 2 which should give us 13… and it does. This is a useful
demonstration, but it is not the most readable code. Sometimes, an extra line is
perfectly fine. Lambda expressions are quite useful when you need a short, throwaway function. Something simple that you will only use once. Common
applications are sorting and filtering data. And while we are on the subject, did
you know that Socratica has a sorted list of Python videos? Unlike lambda
expressions, we would prefer NOT to remain anonymous… so if you know someone
who is learning Python or SHOULD learn Python, please, send them our way. We will upgrade their knowledge banks as best we can…

100 Replies to “Lambda Expressions & Anonymous Functions || Python Tutorial || Learn Python Programming”

Leave a Reply

Your email address will not be published. Required fields are marked *