What Are Python LAMBDA Functions and How to Use Them!

LeMaster Tech
183 followers
2 Video Views·Apr 20, 2025
A 'lambda' function in Python is a short, single-line function that returns a simple expression based on passed parameters, that does not require a formal function definition.
This makes it what is known as an 'anonymous' function, because it does not go through the standard Python process of defining the function, but it can still be stored in a variable and used throughout your code!
It is a useful python beginner trick to start writing simpler and easier to read code, but it also is not a good replacement for complex, multi-line functions!
Thanks for watching, be sure to leave a like and subscribe to the channel, and comment any questions you may have or what you'd like to see next on the channel! Cheers!
what's up you guys welcome back to the
channel in today's video we are going to
talk about single line Lambda functions
in Python what they are when to use them
and how to use them
[Music]
so essentially you've probably seen or
heard of Lambda functions if you've been
programming in Python for a while but if
you've never really taken the time to
dive into what they are or when you
should use them you can kind of just
ignore them because if you haven't gone
out of your way to learn about them and
you're self-taught it's not something
that comes up super often but it can do
some really useful stuff for you and so
the term for a Lambda function is that
it's an anonymous function meaning
you're not going through the formal
python steps of Define function name
here's the parameters and I want to be
able to call this repeatedly you're
saying I'm going to use this function
for a small piece of code that I am only
going to use in this one location and so
let's take a look at just some of the
basic times you might want to use a
Lambda function and so here I have two
examples of Lambda functions one that
just takes an input variable as a string
and Returns the uppercase version of
that and the other one that just takes a
numerical value and Returns the cubed
value of that so in Python these two
stars mean raised to the power and so to
talk quickly about the format of it to
call a Lambda function you just say
Lambda the keyword Lambda which is a
Greek letter but rather than using the
Lambda Greek letter we just type the
word out and then you say the parameters
that you're going to pass in and so
important to note here is that you can
pass in multiple parameters to a Lambda
function and the thing is you can use
all of them in your expression but a
Lambda function can only have one
expression total so it's not going to
like Cube the value take that value and
do something else with it on the next
line it has to be an expression that
fits into one line but you are able to
pass in multiple parameters separated by
commas just like this and then you just
say what you want the expression to
return on the right of these colons of
this colon so that is the entire format
of how you write a Lambda function
and then the way you call this is you
could either just print uh cubed of one
number but let's say you have a list of
numbers like I have here one two three
four and five or a list of names like
Joe Mike and Sarah well you could say
for every item in my uh list I want you
to run this little function and I want
you to return the value but so if I were
just to run this code for the names and
the numbers what we should see when we
run it is we get
okay 1 8 27 64 and 25 which if you were
to take your calculator and Cube every
number in numbers you would get that
it's cubed
um and so we've set up this simple
little function rather than going
through the steps of okay Define cubed
as a function that's going to take some
number in X is actually going to be
equal to x to the third power and then
return X we basically extracted this
formal way of defining a function in
Python and we've just sort of uh put it
into one line using this Lambda function
and hopefully the understanding you take
out of this video is that you don't need
Lambda functions instead of traditional
python functions it's a tool that you
the programmer have access to that can
simplify your code a little bit okay so
let's go ahead and get rid of this top
stuff because that was just sort of to
introduce the concept of Lambda
functions but honestly those functions
sending a string to uppercase and cubing
something aren't even necessarily
something you would want a python
function for like that's not the power
of a Lambda function so I want to create
a function that actually is going to
look like a traditional python function
with a Lambda function inside of it to
show you some of the real power of a
Lambda function so in this case let's
say we create the function sum power and
the actual first parameter that it takes
n in this case is the power that we want
to return a value to and then the Lambda
function is going to take a parameter a
which is the number that we want to
raise to the power and so all I did is I
defined some power as a function and
we're going to return some value to some
power but then in my code I'm going to
set up a squared a cubed and a quoted
variable which are equal to some power
to the second power the third power and
the fourth power so here I'm setting up
instances of this function where my n
value is two three and four and now I
can call squared cubed or quoted and
then I'm passing in a value here at the
end that is going to be a so you can
think of this as me taking the same
function
and giving it a different initial
parameter in this case n and now if what
you'll see when I print out 5 squared 5
cubed and 5 quoted which I have a note
here I looked it up it's also called
super cubed I'd never heard that
terminology so I'm just saying quadded
like quadrupled
um but so if I run this then what you'll
see down here is I get 25
125 and 625 and if I were to change this
from 10 to 15 to 4 to the fourth power
you'll see I get these huge numbers and
so it works better than that even let's
say that quad 8 isn't raised to the
fourth it's raised to the eighth but I
want to do 3 to the eighth power okay
well now I have this super powerful tool
whereby defining my power function
separately whoops Ctrl Z there we go
defining my power function separately I
can set up these functions ahead of time
where I have a squared cubed quoted
function and then all I have to do down
here is pass in the value that I want
raised to the power and I was able to do
this with a two line function called sum
power thanks to an embedded Lambda
function so just to recap because I know
we moved kind of quickly and I know this
isn't my usual setup and not quite as
organized as some of my videos I'm
traveling we'll be back to our normal
setup next week but just to recap a
Lambda function is an anonymous function
where you're defining a simple
expression of code in one line and it
essentially can take in any number of
parameters but it has to use one
expression for the return value
and you want to use it anywhere that
this is code that you only need to call
in one place in your code because as
soon as you start needing to call it
multiple times inside of different
functions then it's time to set it up
like a traditional function otherwise
you're going to be copying this line of
code over and over again but in this
case even though the sum power function
gets called multiple places the Lambda
function itself that actually raises
some value to some power is only needed
in one location so it's a fairly simple
python concept but it is something where
if you're a total beginner it takes a
little bit of time to get familiar with
and start looking for opportunities to
use it instead of a traditional function
so I hope this was useful please be sure
to keep leaving comments on videos
letting me know what you'd like to see
more of a lot of the recent videos have
come from user suggestions so you never
know when your suggestion will be there
as well thank you for watching again
we've got some big projects coming up so
be sure to subscribe to stay up to date
for all the newest stuff on the channel
and as always good luck with your code
and thanks for watching bye
