Hello everyone and welcome to the land the expression overview or review lecture and this lecture we're
going to give a brief background and review of land the expressions of Python since we're going to be
using lambda expressions.
So often with Sparke and RDD these have Python it's important to review them.
If you already feel pretty comfortable if land the expressions you can go ahead and feel free to skip
this lecture.
But I'm going to go ahead and give them one quick review by jumping to a Jupiter notebook.
Let's go ahead and get started.
All right.
We're hearing them at a Jupiter notebook.
Now we've seen lambda expressions used and we're working off the Apply statement in Pandas and Python
.
We're going to be using them a lot with sparking already.
So let's go ahead and break down a lambda expression.
Remember that a land expression allows us to create anonymous functions which basically means that we
can quickly make ad hoc functions without needing to properly define a function using DPF.
So to review how to create a normal function you would say something like the EFF will say square now
and then we'll go in and say result is equal to.
Number two the power of two and then return result.
Then if I call square on some numbers such as 4 returns a square that number which is 16 Let's go ahead
and break this down.
So one thing I don't have to do is redefine results here so I can just say the F square of Naam and
then just return
num squared and I'll work the exact same way.
So now if I call Square 4 I still get 16.
Now we can actually write this in one line so I can continue to put this in one line.
It's bad style to do this but Python does allow for it and this will still work.
So let's check that out.
If I run the cell again.
So now it's in five run the cell again.
You get the same result.
And now we can do is take this syntax and turn this into a land expression.
So turning this into a land the expression would look like this.
We say Lamda we skip DPF and you skip naming the function but you do keep whatever variable name you
want.
So then we say lambda numb colon and then you skip typing in return and you type whatever transformation
you want to return.
So then we'll say no.
The power.
And that's a lambda expression.
So if you wanted to get this back you could say assign this Although you usually wouldn't assign a lambda
expression because that kind of defeats the purpose of land expression.
But this still works so.
Eskew a 5 is 25.
So we're going to go ahead and do is a couple of more lambda expressions using some strings and numbers
just to get an idea of how we can use them.
So if we wanted to check if a number was even we could say something like Lambda numb and then say No
mod 2 is equal to zero and I'm going to call that even.
So I'll say even if three is false even a four is true.
And just to reiterate usually we wouldn't assign a lambda expression would use it as part of a method
and we'll see that later on when we deal with SPARK inland expressions.
But let me go ahead and show you just a few more examples to really drive this home.
We can go head and say let's say we wanted to grab the first character of a string.
Well say Lamda take an s return s index 0.
So then saying first of some string it returns a.
Then if we wanted to reverse a string we could say for instance R V is equal to lambda X and then return
S..
You can actually it's kind of an indexing trick here.
They'll print everything back in reverse.
So you can see a b c d and it returns back EDC b a.
Ok one last example.
Just like a normal function you can actually except more than one function into a land expression.
So he should know what I mean by that.
I can say the F atter X comma y return x plus y which means I can say after two and three it returns
five.
Just like I broke down the last function we can break this down into a land expression as well.
So to put the logic out I can put this all in one line.
Get rid of return.
I don't need these parentheses.
Get rid of the name and ADF and just replace this with lambda and that's all in the expression as well
.
So if I re-assign this to be at or lamb and I call at or lamb on two and three it works the exact same
way.
So it's show that one more time three and three that's six.
OK so Landler really slammed expressions really shine when used in conjunction with some sort of map
filter or reduce method and those methods are transformations we have sparked our Didi's.
So let's go ahead and end this lecture here.
Feel free to post any questions about land expressions to the Q&A forums.
But everything reviewed here is really all you need to know as we move forward.
Thanks everyone and I'll see you at the next lecture