Detailed Notes
We look at lambda functions in Python for this episode focused on getting to certification.
Useful Links: https://realpython.com/python-lambda/
Learn more about the overall certification and syllabus here: https://pythoninstitute.org/certification/pcap-certification-associate/pcap-exam-syllabus/
Github repository for code examples: https://github.com/robbroadhead/PythonCertificationSeries
Transcript Text
[Music] two one well hello and welcome back we are continuing our series of examples working our way through the python certification in this episode we are going to look at lambdas a lambda is maybe familiar to you if you've dealt with other languages and particularly if you go back to lisp or scheme or those lisp related kinds of languages lambdas are used constantly throughout we also see them in java supports it c sharp i think supports it c plus plus and it is a slightly different way without getting too deep into the world of lambdas and lambda arithmetic and such it is a different way to approach uh functions and definitions and being able to do some sort of operation on values uh simplest way you do a lambda is you're going to use the keyword lambda you have a variable that you are this is basically your parameter and then you have something that it does that it returns and then here this is an example of actually calling it with a a value so it's actually this one would be if i called this add1 and gave it let's just call it x and then i need to return x plus one but oh i didn't want to comment that out why doesn't it like that add one it's probably something stupid oh function name should be lowercase okay fine um oh and it just says it's okay so anyways so let's do uh print i don't have to do a string add one one and then let's do the same thing let's do it this way so in this case um did i balance those right yes in this case i'm going to come in and i'm going to call add one i'm going to send it the value of one so it comes up here and it says okay one return one plus one which happens to be two spoiler alert this it says i'm gonna give it a value and then i'm gonna return that value plus one and so if i send it one notice i'm declaring it and calling it all at the same time in this case and then i'm gonna print the value and if i go out there we'll see that here i've printed those two i've got those um let's do it this way because we may end up not make it hard to follow so let's do this printhead one and this is going to be print this now we can also do um we can do the same kind of thing as we can take this lambda and here now at this point we don't have uh we don't have a name for this and we'll show you we'll address that a little bit later but like right now if i try to call i can't call this again this lambda i just created this is on the fly it's not stored anywhere or anything like that now i can do um i can create it let me see how i want to do that let's create this one so let's do x plus two and i can do about oh i can do actually print it's going to store it so i can actually do underscore and send it a value which is two and i think that's going to work for me um well [Music] i could do it that way let me make sure i can do that and that happens because it only that'll only work in the interpreter so i'm not going to bother with that right now i'm going to go back to this uh so that's if you're doing the interpreter you can actually see it but it basically it doesn't exist um let's do this outside of you know just right there and of course if i were to create another lambda then i would immediately um be screwing it up uh well what does it do here's what i do uh and buy it well i'd immediately you know overwrite it if i create a call another lambda so this time i'm gonna do i'm just going to do it like this oh um [Music] that's how i want to do it so i'm going to take that and so i can also send multiple parameters in and so now if i do uh three comma two somewhere in here then we'll see here that so i sent in three plus two it took those guys and it uh added them and returned them now i can also do i can store these if i want to so now i can come back and let's do this um that's gonna be my lamb equals and let's just uh well let's do this this will be a multiplier just cut and then i can do print my lamb uh let's do two times three and we'll see here that i get six so i can also store off if i want to i can start off and then i just call it by using that variable name and then the um send the parameter string or the list of parameters in as i need to now we can do this in a way that allows us to uh a lot of times less to write stuff that can uh can do some sort of recursion or some sort of uh string of lambdas is fairly common so that we would have stuff that gives us a pretty a short way for example um let me use like it's going to be a i'm not going to write the guy out but there is uh if you go to it's uh i think it's scheme it may be list but i'm trying i think it's lisp um they have a thing called lambda which basically takes a string and it returns um uh basically the last uh word or lan you know last space in something or takes the last thing in a series last thing in the list um i'm sorry car is the first and correct me don't bother i'm sorry don't correct me if i'm wrong this is just for purposes and this would be last and so what i could do is it gives a parameter and then uh sends out regardless it just returns you know a string in this case but now what i can do is i can do print oh i can let me do it this way um let's just do it this way let's do add and subtract so what we want to do here is let's do this is what we can do is and jump here and so we can actually put these together so we can do and so we're going to add a value add a value add a value uh subtract a value and subtract a value and then let's just do that to one so that's gonna be one subtract one is zero minus one plus one gets us to zero one two so it should end up with two and then let's do result equals now this is a little bit contrived but you've seen this we've we've got situations where we have um you know combined out functions and so we can do that as we do here and so we printed that out we got two as our result so we can actually combine lambdas now another thing that we can do is we can use variable arguments and there are some things that will there are functions out there that take variable arguments so we can do it rather simply or we can create one that does it so if we wanted to add a series of numbers then we can do a lambda that takes a variable number of arguments and then prints that out and then sums them so if we do print solve two then we're gonna see here that that's six one plus two plus three so we can we can use uh variable arguments we can use keyword arguments if we want to do it so we could do um let's see what's a good one for this and so that would be kw args and then uh a equals one p equals two oh these need to be that's not strings those values so now i can just say that i've sent this stuff in let's make this result well we'll leave that result too and so here it's going to go through and it's going to grab the value which is one two and three and i should still see six oh uh let's see see i may not have built my array correctly i missed an asterisk so now if i do that then i'm going to see that i get my six skin so i can either use args uh which was the uh let's do this let's bring that back in to make sure i don't lose it and let's see where did i have my prior one whoops let's get that back there because i forgot i didn't want that there we go uh so it's gonna be just result two and then result three so make sure we've got those so we've got a couple different ways we can build out our lambdas and we have ways that they can be called now the usefulness uh depends you can do a lot of this stuff based on with functions as well so it's until you get too deep into it i'm not sure that it's it isn't better to just use functions particularly since we have um we also have with functions now that i can do uh this i can do like that and i can add these hints so now i can have something that implies a little better what i'm dealing with what i'm going to send and what i'm going to return so now if i go back and do print add one i'm gonna do a string well if i just do print add one and i'm gonna send it a let's see what that does this will be at the very back of it and we'll see here now the first thing here is i can only concatenate a string so which and then if i run it you can commonly catenate a string here to string so that's the same thing as now it looks like you know i'm running into this ant so it's basically to help people out and give something that's a little easier to read whereas lambdas can get like here i mean they're pretty simple but they can get sort of complex to read uh so once you start putting tight hints and such and that it just helps a little bit as far as reading and then there's some other side effects that are pretty nice bonuses to have i think that'll wrap this up for now that's sort of where i wanted to go with lambdas as far as certification there i don't expect you to see too much but you will see a little bit of that so it's good to understand the basics and things like actually let's do one more just a note that which probably doesn't need to be noted but active my lamb too and i could add another parameter just as easy and i'll still run it oops let's get rid of that okay um and so it's going to be that 24 there is it what because it's 2 times 3 times 4. so i can put any number of parameters including a variable or even keyword related and i can wheel and deal with it that way and then it ends up being very similar to as we saw up here it's very similar to a function so if you're comfortable with functions lambdas are not going to be terribly difficult either just realize that that is creating a function type object we'll call it that you can pass around so i can actually do this and then now this is my this is going to call that function so i could actually dynamically at some point i could actually dynamically create some functions and things like that and it becomes pretty useful when you want to do those different tasks that that's very useful for that being said probably gone a little longer than needed on this but i want to play around with lambdas a little bit and this will be in the repository as always and we'll come back next time and we'll dig into some other pieces of this continuing looking at some classes and functions and some of the things that python provides us and until then go out there have yourself a great day a great week and we will talk to you next time you
Transcript Segments
[Music]
two
one well hello and welcome back we
are continuing our series of examples
working our way through the python
certification
in this episode we are going to look at
lambdas
a lambda is maybe familiar to you if
you've dealt with other languages
and particularly if you go back to lisp
or scheme or those lisp related kinds of
languages
lambdas are used constantly throughout
we also see them in java supports it
c sharp i think supports it c plus plus
and it is a slightly different way
without getting too deep into the world
of lambdas and lambda arithmetic and
such
it is a different way to approach uh
functions and definitions and being able
to do some sort of
operation on values
uh simplest way you do a lambda is
you're going to use the keyword lambda
you have a variable
that you are this is basically your
parameter and then you have something
that it does
that it returns and then here this is an
example of actually calling it with a
a value so it's actually this one would
be if i called this
add1
and gave it let's just call it x
and then i need to return
x plus one
but oh i didn't want to comment that out
why doesn't it like that add one it's
probably something stupid
oh function name should be lowercase
okay fine
um
oh and it just says it's okay so anyways
so let's do uh
print i don't have to do a string
add one one
and then let's do the same thing let's
do it this way
so in this case um did i balance those
right
yes in this case i'm going to come in
and i'm going to
call add one i'm going to send it the
value of one so it comes up here
and it says okay one return one plus one
which happens to be two
spoiler alert this it says i'm gonna
give it a value
and then i'm gonna return that value
plus one and so if i send it one
notice i'm declaring it and calling it
all at the same time in this case
and then i'm gonna print the value and
if i
go out there
we'll see that here i've printed those
two
i've got those um
let's do it this way because we may end
up not
make it hard to follow so let's do this
printhead one
and this is going to be print
this
now we can also do um
we can do the same kind of thing as we
can take this lambda
and here now at this point we don't have
uh we don't have a name for this and
we'll show you
we'll address that a little bit later
but like right now if i try to call
i can't call this again this lambda i
just created this is on the fly
it's not stored anywhere or anything
like that now i can do
um i can create it let me see how i want
to do that let's create this one
so let's do x plus two
and i can do about oh i can do
actually print it's going to store it so
i can actually do
underscore and send it a value which is
two
and i think that's going to work for me
um well
[Music]
i could do it that way let me make sure
i can do that
and that happens because it only that'll
only work in the
interpreter so i'm not going to bother
with that right now i'm going to go back
to this
uh so that's if you're doing the
interpreter you can actually see it but
it basically it doesn't exist
um let's do this
outside of you know just right there and
of course if i were to create another
lambda then i would immediately
um be screwing it up uh well what does
it do here's what i do
uh and buy it well i'd immediately you
know overwrite it if i create a call
another lambda
so this time i'm gonna do
i'm just going to do it like this oh um
[Music]
that's how i want to do it so i'm going
to take that
and so i can also send multiple
parameters
in and so now if i do
uh three comma two
somewhere in here then we'll see here
that so i sent in three plus two
it took those guys and it
uh added them and returned them now i
can also do
i can store these if i want to so now i
can come back
and let's do this um
that's gonna be my lamb equals
and let's just uh well let's do this
this will be a multiplier just cut
and then i can do print my lamb
uh let's do two times three
and we'll see here that i get six so i
can also
store off if i want to i can start off
and then i just call it by
using that variable name and then the um
send the parameter string or the list of
parameters in as i need to
now we can do this in a way that allows
us to
uh a lot of times less to write stuff
that can
uh can do some sort of recursion or some
sort of
uh string of lambdas is fairly common so
that we would have stuff
that gives us a pretty a short way
for example um
let me use like it's going to be a i'm
not going to write the guy
out but there is
uh if you go to
it's uh i think it's scheme it may be
list but i'm trying i think it's lisp
um they have a thing called lambda which
basically takes a string
and it returns um
uh basically the last uh word
or lan you know last space in something
or takes the last thing in a series
last thing in the list um i'm sorry
car is the first and correct me don't
bother
i'm sorry don't correct me if i'm wrong
this is just for
purposes
and this would be last
and so what i could do is it gives a
parameter
and then uh sends out regardless it just
returns
you know a string in this case but now
what i can do
is i can do print oh i can let me do it
this way
um let's just do it this way let's do
add
and subtract
so what we want to do here is let's do
this
is what we can do is
and jump here and so we can actually put
these together so we can do
and so we're going to add a value add a
value
add a value uh subtract a value
and subtract a value
and then let's just do that to one so
that's gonna be one subtract one is zero
minus one
plus one gets us to zero one two so it
should end up with two
and then let's do result equals
now this is a little bit contrived
but you've seen this we've we've got
situations where we have
um you know combined out functions and
so we can do that
as we do here and so we printed that out
we got two as our result
so we can actually combine lambdas now
another thing that we can do
is
we can use variable arguments and there
are some things that will
there are functions out there that take
variable arguments
so we can do it rather simply or we can
create one that does it so if we wanted
to add a series of numbers
then we can do a lambda that takes a
variable number of arguments
and then prints that out
and then sums them so if we do print
solve two
then we're gonna see here that that's
six one plus two plus three
so we can we can use uh variable
arguments we can use keyword arguments
if we want to do it so we could do
um
let's see what's a good one for this and
so that would be kw
args and then
uh
a equals one p equals two
oh these need to be that's not strings
those values so now
i can just say that i've sent this stuff
in
let's make this result well we'll leave
that result too
and so here it's going to go through and
it's going to grab the value which is
one two and three
and i should still see six oh uh
let's see
see i may not have built my array
correctly
i missed an asterisk so now if i do that
then i'm going to see that i get my six
skin
so i can either use args uh which was
the
uh let's do this let's bring that back
in to make sure i don't lose it
and let's see where did i have my prior
one
whoops let's get that back there because
i forgot i didn't want that there we go
uh so it's gonna be just result
two
and then result three so make sure we've
got those so we've got a couple
different ways we can build out our
lambdas
and we have ways that they can be called
now
the usefulness uh depends you can do a
lot of this stuff based on
with functions as well so it's
until you get too deep into it i'm not
sure that it's it
isn't better to just use functions
particularly since we have
um we also have with functions now
that i can do
uh this
i can do like that
and i can add these hints so now i can
have something that
implies a little better what i'm dealing
with what i'm going to send and what i'm
going to
return so now if i go back and do print
add one i'm gonna do a string well if i
just do print add one
and i'm gonna send it a let's see what
that does
this will be at the very back of it
and we'll see here
now the first thing here is i can only
concatenate a string
so which
and then if i run it
you can commonly catenate a string here
to string so that's the same thing as
now it looks like you know i'm running
into this ant
so it's basically to help people out and
give something that's a little easier to
read
whereas lambdas can get like here i mean
they're pretty simple but they can get
sort of complex to read
uh so once you start putting tight hints
and such and that it just helps a little
bit as far as reading and then there's
some
other side effects that are pretty nice
bonuses to have
i think that'll wrap this up for now
that's sort of where i wanted to go with
lambdas
as far as certification
there i don't expect you to see too much
but you will see a little bit of that so
it's good to understand the basics
and things like actually let's do one
more just a note
that which probably doesn't need to be
noted but active my lamb too
and i could add another parameter just
as easy
and i'll still run it oops
let's get rid of that
okay um and so it's going to be that 24
there is it what because it's 2 times 3
times 4.
so i can put any number of parameters
including a variable or
even keyword related and
i can wheel and deal with it that way
and then
it ends up being very similar to as we
saw up here it's very similar to a
function so
if you're comfortable with functions
lambdas are not going to be
terribly difficult either just realize
that that is
creating a function type
object we'll call it that you can pass
around so i can actually do this
and then now this is my this is going to
call that function so i could actually
dynamically
at some point i could actually
dynamically create some functions and
things like that and it becomes pretty
useful
when you want to do those different
tasks that that's very useful for
that being said probably gone a little
longer than needed on this but i want to
play around with lambdas a little bit
and this will be in the repository as
always
and we'll come back next time and we'll
dig into some other pieces
of this continuing looking at some
classes and functions and some of the
things that python provides us
and until then go out there have
yourself a great day
a great week and we will talk to you
next time
you