Hello everyone.
Welcome to part four of the Python crash course lecture series in this series of lectures we'll be discussing
lambda expressions the map and filter functions as well as just various methods you can use on different
data types in Python.
Let's go ahead and jump back to Jupiter notebook to get started.
All right.
Here I am at the Jupiter notebook.
Last time we left off on discussing how you can create your own functions in Python now we're going
to do is discuss the map and filter functions and then how we can use lambda expressions instead of
writing a full function out.
Let me go ahead and quickly review how to write a function you just use the keyword D for death.
The function name in this case I call this function times to accept some variable as an argument.
They are.
And then I'll just go ahead and return that variable multiplied by two.
Which means if I call times 2 on a number such as 5 it returns 10.
Now let's go ahead and discuss the map function.
I'm going to make a sequence called CQ and it's going to be a list.
One two three four five.
Now let's say I wanted to actually apply this function times to to every single element in this list
and then output the list.
Well I could use a for loop to do that and then append the results to another list.
However map is a built in function in Python that allows you to save that step.
Do you go ahead and type in math.
You can do shift enter to actually see the documentation string for map.
But basically it's going to create an iterator that computes the function using arguments from each
of the iterable.
Let me go and explain just in kind of clear simple English what that actually means.
You're going to say map pasan a function for instance times 2.
And then the sequence you want to map that function to.
And if you run that they'll report back that you have a map at this particular place in memory.
If you actually want to execute this map and get the result back as a list you cast this into a list
by using the built in list function.
And then this will finally return this function mapped to every elements in that sequence and return
it as a list.
And now we can see that we have every element multiplied by two.
So now we have two four six eight ten instead of one two three four five.
OK.
So you may be wondering do I have to completely define a full function such as times 2 or is there an
easier way to go about this.
Because usually when you're using something like map you're not going actually want to write and define
an entire function.
This will probably just be a one time thing and that's where the land expression comes into play.
I'm going to show you how we can rewrite this function the times to into a lambda expression and I'm
into slowly just break it down.
This function times too can actually be written on one line of times to the way our colon return the
r times 2.
And if I run these two cells again it still works out just fine.
A land expression will try to replicate this sort of structure except it's going to remove words that
we don't need by default.
When you kind of think about it you don't really need the F or return because those are kind of just
given values of course in a function.
You're going to have to define it name it and return it for the land expression.
Otherwise known as an anonymous function.
You just want to replace in an instance where you don't want to have to define an entire function which
is the lambda expression and you can do that by essentially reading out this function.
But getting rid of the F and the name and instead just typing in lambda and then you go ahead and get
rid of return and just type out what you actually want to return.
And this is the land the expression.
Now I could if I wanted to say t is equal to this land the expression and then call t of some number
such as 6.
And I can see that I get in return six times two.
So I usually wouldn't use the land expression like this but it is possible to use it.
Well you will be using lambda expression is for something like map or instead of having to define an
entire function.
You would just type in lambda expression here you would say lambda what you actually want to pasan colon
and then what you want to return out in this case is go ahead and multiply everything by three.
And now if I run this on the same sequence I've grabbed every element in the sequence and multiply it
by three and I no longer have to define an entire function formally.
I can just use a lambda expression lambda expressions or something where to use a lot especially when
we start learning about the Pandurs library.
So if you have any questions on this please feel free to post the Q&A forums.
But it's basically just the way of rewriting an entire function into a single line of clean code.
And the reason for this is because you usually don't want to have to write and define an entire function
.
However that always is an option for you if you're ever confused by land expressions.
You can just define a formal function like we did for times 2 and use that instead.
Now let's go ahead and discuss the builtin filter function.
Filter is going to have a very similar structure to map.
But instead of mapping a function to every element in a sequence we will filter out elements from a
sequence.
Let me go in and show you how you can do this.
You'll go ahead and call filter and then you're going to pasan either a function or a lambda expression
that filters out and returns boolean values.
So for instance I can say lambda of a number colon that number maade two equals zero and then pass in
the sequence.
And this will return just the filter expression.
Actually I want to get the results back.
I need to cast it to a list.
And this returns back only the even numbers into my sequence 2 and 4.
So let's break down what's actually happening here.
I'm saying filter and this lambda expression takes in a number and returns a conditional true or false
value based off some sort of conditional operation.
So a filter must be using some sort of function or lambda expression that returns a boolean value.
True or false.
And then what you're going to go ahead and do is pass it into an iterable object.
And when you call list on it it will only return the values where this statement happened to be true
.
In this case we just filtered out for even numbers.
All right.
Now let's go ahead and discuss methods.
Methods are essentially just cause you can make off an object that will affect the object or return
a result in some manner.
Let me go ahead and show you what I mean.
I'm going to make a string called S and this will say something like.
Hello my name is capital S. Am.
Sam I can call s and then dots and if I click tab and Jupiter note book I will get a result of all the
available whip's tab.
Scroll down.
I'll get a result of all the available methods on this string object.
Now we don't have time to go over all these and all of these won't be use throughout the course we'll
just use quite a few of them.
Let me go in to show the important ones.
Lower will lower case every single letter in a string.
And note that you have to call close parentheses.
If you just call lower like this or return back and say oh you're asking about the function lower.
Like I mentioned earlier you have to actually execute it by closing those parentheses if you want to
do the opposite.
In this case uppercase everything you can just call s thought up.
All right one other useful function or method excuse me for strings is the split method the split method
by default will split a string on all the whitespace of that string.
So here we have Hello my name is Sam.
When we say estopped split it will split this on all the whitespace.
That's going to be really useful for text analysis later on in this course.
Let me go in and show an example.
Let's say we have a tweet that says something like.
Go sports hash tag sports.
If we say tweet thoughts splits this will split the text into a list based off the whitespace.
But we can actually pasand a string into split to split on particular elements or characters.
If I pass an hash tag and run this it will split the string where it finds a hash tag meaning it'll
have the message and then at the end have a hash tag.
And this will actually return a list though we can that index.
So if I just want to grab the hash tag I can say brackets one and all return sports so we can already
see how this would be useful if you just wanted to grab hash tags from some example tweet strings.
OK.
Those are useful methods for E-string upper lower end split.
Those are the good.
Those are the ones we're going to be using throughout the course.
Issue something you can do with dictionaries.
So remember a dictionary.
Let's go to and see if we have an old one.
Well the re-assign this then to be K-1 with a value of one.
And then we'll say Kate two is a key of a value of two.
All right.
So here's my dictionary.
K. One is one k to is two.
Now we can say d dot and you can click tab to see the various methods available to you.
Some useful ones are the keys method and this just returns the keys to the dictionary.
If you ask for items this returns the dictionary items and then we can also just ask for if we look
at the.
You can say pop or get or values.
So if you just want the values of the dictionary.
This returns the values.
Keep in mind dictionaries don't retain an order.
We won't really be using keys items or values from the course but I do want you to be aware of them
some useful methods for lists.
I'm going to go out to make a list.
Ls t.
Say 1 to 3.
You can pop an item off a list by calling pop and this will pop and return the last item in the list
and then when you call this that change is permanent.
Which means if we go ahead and say list is one two three four five you can say item is equal to ls t
pop and that will pop the last item off of that list and if we call that item it's been reassigned.
5 is now item which was the list that pop and you can actually with the pop method passing an index
value to pop off.
So if you say first is equal to LS T-top pop 0 and run this.
Take a look at LS T.
Now the index 0 has been popped off and our first variable is that one which was the first element.
So pop is a really useful method and so is append.
But we've actually already seen that.
That's just allows you to append a new item to the end of the list.
All right.
Finally a few more things I want to show you is the in operator.
So if you want to check if something is inside of a list you can say the thing you want to check and
then in and then the list itself.
So X in 1 to 3 that returns false versus x in a list containing x y and z that returns true.
And finally the very last thing in this python crash course that we're going to cover is tuple unpacking
tuple unpacking is a really neat feature and I'm going to show you how it works.
Imagine that you have a variable x which contains a list of tuples.
We'll have one two three four and then five six.
So here if I look at X the first item in X is a tuple of 1 and 2 and I can actually grab items from
that tuple so I can grab one or I can grab two.
I can also do something called tuple unpacking and that's going to work when we're iterating through
a list of tuples.
This sort of formation of a list of tuples is really common in Python and a lot of functions will return
a result in this format.
Which is why we have tuple and packing as an option so we can say for item in X prints item.
And when I run that it goes ahead and prints all the tuples for me tuple and packing allows me to actually
unpack these tuples as I go along so I can do something like this for a comma B in X prints a.
And now this will print 1 3 5.
Or if I say Prince B.
This will print the second item in those tuples.
2 4 6 and that's tuple and packing.
Or instead of just saying an item you actually unpack this tuple inside of that for loop and something
else.
Note is that these parentheses surrounding this are actually just a formality.
This is how you are going to see tuple in packing in the real world all the time.
You'll see for the tuple and packing in X which is some iterable of tuples inside of it.
And then you can just print off the individual items in that tuple and that's all you can do it all
right.
It will remind you of tuple and packing when we reach it again later in this course.
It's not going to come up until a while from now but hopefully this was enough of a general overview
for Python if any of this seemed just impossibly hard or was just completely unfamiliar to you and you
really didn't understand the concept.
This course may be too advanced for you and you may be in need of a course that's geared towards beginner
in programming such as my complete Python bootcamp course.
Always feel free to post any questions to Kewaunee forums and coming up next is an exercise to just
test your knowledge in this python crash course topic series.
Thanks everyone and I'll see at the next lecture