📺 Develpreneur YouTube Episode

Video + transcript

Python Certification Training - Lambdas

2021-08-19 •Youtube

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
0.46

[Music]

26.4

two

27.359

one well hello and welcome back we

30.4

are continuing our series of examples

32.559

working our way through the python

34

certification

35.52

in this episode we are going to look at

37.52

lambdas

39.36

a lambda is maybe familiar to you if

42.64

you've dealt with other languages

44.48

and particularly if you go back to lisp

48.079

or scheme or those lisp related kinds of

50.719

languages

52.32

lambdas are used constantly throughout

55.76

we also see them in java supports it

58.96

c sharp i think supports it c plus plus

62.079

and it is a slightly different way

65.6

without getting too deep into the world

67.439

of lambdas and lambda arithmetic and

69.2

such

70.32

it is a different way to approach uh

72.64

functions and definitions and being able

74.64

to do some sort of

76.24

operation on values

79.92

uh simplest way you do a lambda is

83.759

you're going to use the keyword lambda

86.159

you have a variable

87.84

that you are this is basically your

89.439

parameter and then you have something

91.759

that it does

93.119

that it returns and then here this is an

95.68

example of actually calling it with a

98.079

a value so it's actually this one would

100.64

be if i called this

103.439

add1

109.04

and gave it let's just call it x

114.24

and then i need to return

117.439

x plus one

122.64

but oh i didn't want to comment that out

126.24

why doesn't it like that add one it's

127.84

probably something stupid

132.239

oh function name should be lowercase

133.84

okay fine

135.36

um

140.64

oh and it just says it's okay so anyways

144.16

so let's do uh

147.28

print i don't have to do a string

152.4

add one one

156.239

and then let's do the same thing let's

157.92

do it this way

162.64

so in this case um did i balance those

165.28

right

167.28

yes in this case i'm going to come in

170.08

and i'm going to

172.48

call add one i'm going to send it the

174

value of one so it comes up here

176.959

and it says okay one return one plus one

179.44

which happens to be two

180.56

spoiler alert this it says i'm gonna

183.92

give it a value

185.12

and then i'm gonna return that value

186.56

plus one and so if i send it one

188.879

notice i'm declaring it and calling it

190.56

all at the same time in this case

193.2

and then i'm gonna print the value and

195.12

if i

196.4

go out there

199.44

we'll see that here i've printed those

202

two

203.44

i've got those um

207.04

let's do it this way because we may end

209.04

up not

213.68

make it hard to follow so let's do this

215.84

printhead one

216.879

and this is going to be print

224.84

this

227.84

now we can also do um

232.48

we can do the same kind of thing as we

234.239

can take this lambda

237.2

and here now at this point we don't have

242.48

uh we don't have a name for this and

244.799

we'll show you

245.599

we'll address that a little bit later

246.799

but like right now if i try to call

249.04

i can't call this again this lambda i

250.72

just created this is on the fly

253.36

it's not stored anywhere or anything

254.879

like that now i can do

258.799

um i can create it let me see how i want

261.84

to do that let's create this one

264.4

so let's do x plus two

273.04

and i can do about oh i can do

276.08

actually print it's going to store it so

279.44

i can actually do

280.84

underscore and send it a value which is

283.52

two

285.36

and i think that's going to work for me

287.199

um well

291.26

[Music]

292.8

i could do it that way let me make sure

294.96

i can do that

297.84

and that happens because it only that'll

299.759

only work in the

300.96

interpreter so i'm not going to bother

302.56

with that right now i'm going to go back

304.4

to this

306.32

uh so that's if you're doing the

307.199

interpreter you can actually see it but

308.479

it basically it doesn't exist

311.759

um let's do this

314.88

outside of you know just right there and

316.88

of course if i were to create another

317.919

lambda then i would immediately

320.4

um be screwing it up uh well what does

323.84

it do here's what i do

326.479

uh and buy it well i'd immediately you

328.8

know overwrite it if i create a call

330.08

another lambda

332.4

so this time i'm gonna do

338.4

i'm just going to do it like this oh um

341.29

[Music]

342.88

that's how i want to do it so i'm going

345.12

to take that

347.039

and so i can also send multiple

348.96

parameters

351.759

in and so now if i do

355.919

uh three comma two

361.039

somewhere in here then we'll see here

363.36

that so i sent in three plus two

366.08

it took those guys and it

369.44

uh added them and returned them now i

372.72

can also do

373.919

i can store these if i want to so now i

376.08

can come back

377.919

and let's do this um

383.84

that's gonna be my lamb equals

391.12

and let's just uh well let's do this

393.759

this will be a multiplier just cut

396.96

and then i can do print my lamb

401.919

uh let's do two times three

405.919

and we'll see here that i get six so i

408.24

can also

409.039

store off if i want to i can start off

411.68

and then i just call it by

413.12

using that variable name and then the um

417.28

send the parameter string or the list of

419.36

parameters in as i need to

422.16

now we can do this in a way that allows

424.72

us to

425.84

uh a lot of times less to write stuff

428.24

that can

428.88

uh can do some sort of recursion or some

431.52

sort of

432.4

uh string of lambdas is fairly common so

435.759

that we would have stuff

437.84

that gives us a pretty a short way

441.52

for example um

447.84

let me use like it's going to be a i'm

449.599

not going to write the guy

451.039

out but there is

455.12

uh if you go to

458.24

it's uh i think it's scheme it may be

459.84

list but i'm trying i think it's lisp

461.84

um they have a thing called lambda which

464.24

basically takes a string

466.16

and it returns um

472.4

uh basically the last uh word

475.759

or lan you know last space in something

477.759

or takes the last thing in a series

479.68

last thing in the list um i'm sorry

483.12

car is the first and correct me don't

485.84

bother

486.319

i'm sorry don't correct me if i'm wrong

489.12

this is just for

490.84

purposes

496.08

and this would be last

499.599

and so what i could do is it gives a

502.879

parameter

504

and then uh sends out regardless it just

507.52

returns

508.24

you know a string in this case but now

510.879

what i can do

511.68

is i can do print oh i can let me do it

514.159

this way

516.399

um let's just do it this way let's do

519.519

add

520.88

and subtract

525.36

so what we want to do here is let's do

528.16

this

528.8

is what we can do is

534.64

and jump here and so we can actually put

537.6

these together so we can do

539.04

and so we're going to add a value add a

542.16

value

543.839

add a value uh subtract a value

548.8

and subtract a value

552

and then let's just do that to one so

555.519

that's gonna be one subtract one is zero

558.399

minus one

559.36

plus one gets us to zero one two so it

562.399

should end up with two

564.32

and then let's do result equals

569.279

now this is a little bit contrived

574.08

but you've seen this we've we've got

576.16

situations where we have

578.24

um you know combined out functions and

580.24

so we can do that

584.16

as we do here and so we printed that out

587.04

we got two as our result

590.08

so we can actually combine lambdas now

592.48

another thing that we can do

594.959

is

598.72

we can use variable arguments and there

601.6

are some things that will

602.72

there are functions out there that take

604.959

variable arguments

606

so we can do it rather simply or we can

607.68

create one that does it so if we wanted

609.519

to add a series of numbers

612.24

then we can do a lambda that takes a

614.079

variable number of arguments

615.519

and then prints that out

619.12

and then sums them so if we do print

623.12

solve two

627.04

then we're gonna see here that that's

628.16

six one plus two plus three

631.519

so we can we can use uh variable

634.16

arguments we can use keyword arguments

635.839

if we want to do it so we could do

638

um

641.839

let's see what's a good one for this and

644

so that would be kw

645.279

args and then

648.32

uh

651.36

a equals one p equals two

656.16

oh these need to be that's not strings

658.88

those values so now

660

i can just say that i've sent this stuff

661.68

in

663.04

let's make this result well we'll leave

664.64

that result too

666.48

and so here it's going to go through and

668.32

it's going to grab the value which is

669.44

one two and three

671.04

and i should still see six oh uh

675.04

let's see

682.399

see i may not have built my array

686.839

correctly

688.079

i missed an asterisk so now if i do that

693.92

then i'm going to see that i get my six

695.44

skin

697.12

so i can either use args uh which was

699.76

the

700.16

uh let's do this let's bring that back

703.76

in to make sure i don't lose it

709.04

and let's see where did i have my prior

714.839

one

716.16

whoops let's get that back there because

717.839

i forgot i didn't want that there we go

720.8

uh so it's gonna be just result

724

two

728.32

and then result three so make sure we've

730.48

got those so we've got a couple

731.6

different ways we can build out our

732.639

lambdas

734.16

and we have ways that they can be called

736

now

737.839

the usefulness uh depends you can do a

740.16

lot of this stuff based on

741.92

with functions as well so it's

745.76

until you get too deep into it i'm not

747.92

sure that it's it

748.959

isn't better to just use functions

751.12

particularly since we have

752.72

um we also have with functions now

756.8

that i can do

759.839

uh this

766.8

i can do like that

770.56

and i can add these hints so now i can

773.04

have something that

773.92

implies a little better what i'm dealing

775.76

with what i'm going to send and what i'm

777.36

going to

778.8

return so now if i go back and do print

784.56

add one i'm gonna do a string well if i

786.8

just do print add one

788.56

and i'm gonna send it a let's see what

791.2

that does

792.56

this will be at the very back of it

796.56

and we'll see here

799.68

now the first thing here is i can only

801.36

concatenate a string

804.959

so which

810.32

and then if i run it

814.56

you can commonly catenate a string here

817.12

to string so that's the same thing as

818.639

now it looks like you know i'm running

819.839

into this ant

821.839

so it's basically to help people out and

823.839

give something that's a little easier to

824.959

read

826.399

whereas lambdas can get like here i mean

829.839

they're pretty simple but they can get

831.839

sort of complex to read

833.68

uh so once you start putting tight hints

835.199

and such and that it just helps a little

836.959

bit as far as reading and then there's

838.24

some

838.959

other side effects that are pretty nice

840.959

bonuses to have

842.72

i think that'll wrap this up for now

844.079

that's sort of where i wanted to go with

845.36

lambdas

846.24

as far as certification

850.24

there i don't expect you to see too much

852.72

but you will see a little bit of that so

854.16

it's good to understand the basics

856.56

and things like actually let's do one

859.6

more just a note

860.959

that which probably doesn't need to be

862.8

noted but active my lamb too

865.519

and i could add another parameter just

867.839

as easy

873.6

and i'll still run it oops

876.639

let's get rid of that

882.32

okay um and so it's going to be that 24

885.44

there is it what because it's 2 times 3

887.279

times 4.

888.639

so i can put any number of parameters

890.32

including a variable or

891.92

even keyword related and

895.04

i can wheel and deal with it that way

897.839

and then

898.399

it ends up being very similar to as we

900.399

saw up here it's very similar to a

901.839

function so

902.8

if you're comfortable with functions

904.56

lambdas are not going to be

905.92

terribly difficult either just realize

908.24

that that is

909.76

creating a function type

912.959

object we'll call it that you can pass

914.56

around so i can actually do this

916.88

and then now this is my this is going to

919.04

call that function so i could actually

920.48

dynamically

922.16

at some point i could actually

923.04

dynamically create some functions and

924.48

things like that and it becomes pretty

926.079

useful

926.8

when you want to do those different

928.72

tasks that that's very useful for

931.279

that being said probably gone a little

933.36

longer than needed on this but i want to

934.959

play around with lambdas a little bit

936.88

and this will be in the repository as

939.04

always

940.079

and we'll come back next time and we'll

941.839

dig into some other pieces

943.6

of this continuing looking at some

945.279

classes and functions and some of the

946.8

things that python provides us

949.12

and until then go out there have

950.8

yourself a great day

952.56

a great week and we will talk to you

955.44

next time

971.92

you