4.2 - Types of functions.mp4
4.2 - Types of functions.mp4
As we just
saw, there are two broad types of functions we have built in functions and user defined
functions. Built in functions are inbuilt into Python, and they are extremely useful, as we'll
see later. User defined functions, on the other hand, are defined by the programmer, and
they can be used for different purposes as compared to built in functions. We'll see a few
examples of each of them so that we have a better understanding of these concepts. So let's
look at some very simple built in functions at the outset. So we have a function called abs,
which basically returns the absolute value of a number. So if you have a value of -100
assigned to a variable num, and if you say abs num, it basically returns a value of 100 to you.
Now, absolute value, as some of you may know, is basically if I have a value of -3.2 we'll
convert that into plus 3.2. Absolute value converts all negative numbers to positive
numbers, and if you have a positive number, it will just return the same number. That's
what an absolute value function mathematics does, right? So now there is another function
called all. This is sometimes very useful. So let's see what the description says. The all
function returns true if all the elements in an iterable are true. What does this iterable
mean? Iterable basically refers to any data structure that we have on which we can iterate.
So for example, take a list, right? Take a list. Imagine if I have these four elements in a list. I
can iterate through this list. Iterate. Basically, think of it like this. Can you run a for loop by
accessing each element one after the other? This is a list, right? This is a list. Such data
structures are set to be iterable. Similarly, if you take a tuple. Suppose if I have a tuple like
this, then this tuple is also iterable because I can access one item after the other iteratively,
right? So anywhere I use the word iterable, it basically means a data structure whose
elements I can iterate one after the other. Typically, I'm mostly referring to data structures
like lists and tuples. Okay, so let's look at this example. So what is it saying? What does the
function all do? The function all returns true if all the elements in an iterable. Easy way for
you to remember is whenever you see the word iterable, read it as list or tuple or any other
data structure or a set. Right? Think about that way. That makes your understanding much
more simpler. So if all the elements are true, it returns true. If any element is false, it returns
false. Let's look at an example here, a bunch of examples here. Let's go one by one. Imagine
if I have a list containing these elements 1234. And if I say print all of list now it returns true
because all these elements are nonzero. Now let's look at a slightly different example.
Imagine if I have a tuple here. This is not a list, this is a tuple, right? Imagine if I have a tuple
here with four elements, and if one element is zero, because zero is treated to be false
internally within python. When I say print all of lst, or in this case it's a tuple, it basically
returns false. Now what happens if I have an empty list? Now let's look at it here. So when I
have an empty list, when I say all of list, it returns true. This is how the function all is
defined. Now I could also have a list with one value being boolean, which is false. These two
are integer. These two are integer. This is boolean, right? Now when I say all of list here,
since I have one value which is equivalent to boolean false, it returns false for me, right?
Very simple function, nothing very fancy, very, very simple. Now let's look at another
function called dir. This is very, very useful. Let me explain what it does, right? So for
example, imagine if I create a list of numbers and assign this to a variable called numbers.
Now, numbers belong to the data type list. Now whenever I say print dir of numbers, what it
returns to me is another list. This is a list of all the attributes and functions that have for the
list data type. For example, for list, you have these functions, right? You have append, clear,
copy, sort, remove, pop, insert. We saw some of them when we studied list data structure.
So the DIr function tells me or returns me all the variables or all the attributes associated
with the data type. So the data type of numbers is list. So it returns to me all the attributes,
functions, and variables that are associated with the data type list. Okay? So here is another
example. Div mod is a function. All these are inbuilt functions, by the way, just to reiterate
that point. So div mod basically takes two numbers. So if you say div mod nine, comma two,
what it returns to me is a tuple first. And how does the tuple look? So when I divide nine by
two, when I divide nine by 292408 and I have a reminder of one. So this is my reminder.
And this is my quotient, right? So when I use the function div mod, what it returns to me, it
returns me the quotient. And the reminder when I divide the first number with the second
number, okay, that's what the function div mod does. Now let's move on to another function
called enumerate. So what enumerate does is as follows. Let's look at an example so that it
becomes easier for us to understand. Imagine if I have a list of numbers like 1020, 30, 40
assigned to a variable called numbers. Now, if I write this for loop, let's understand this for
loop a little. I am saying, see, where am I use enumerate here. I'm using enumerate here. So
when I say enumerate numbers, what it returns to me is it takes this list called numbers,
and it returns to me a pair of values. The pair is such that the first value I have, index and
num, right? Enumerate returns to me pairs of numbers where it takes the first value and it
also returns to me the index. So when I print this, let's look at it and see what it returns
when I print it, so that it's easier for us to understand. So what it prints for me is enumerate,
basically enumerates, enumerates in English. Enumerate in English. Enumerate in English
basically means to list. What it is doing is it's listing each of the numbers. And when it's
listing, it's also returning to me the index, right? So if you want to list a bunch of numbers in
any iterable data structure, it could be a list itself, or it could be a tuple, or it could be a set,
right? And what it returns to me is it returns to me these pairs of numbers called index and
the actual value itself. And for the syntax. So this is what I get. So if I want to get both, the
index at which a value is stored, right, this is a very simple way to do it. But here there is a
catch. If you look at the enumerate functions definitions, I need to enter an iterable, and my
default start value is zero if I change the default start value slightly. So let's look at it with an
example, right? So if I say comma five, let's run this example now. Now what happens, see,
earlier, I didn't give any value here, the default value to assume she is zero. So when I say
enumerate numbers, comma five, it starts the first index at five, right? Now, if I change it
just to show it to you. If I change this to ten and run this example, my first value starts at ten.
Is this clear? This is very, very simple idea. So enumerate is used to list. An it list, or
enumerate in English simply means to list. So what enumerate does, it returns these pairs
for you with index and the value itself. So the next very important functions are. So filter is a
very, very useful function. Now, to understand how filter exactly works, let's look at it from
an example. So imagine if I write a function called five positive number, and if I input a value
to it, it returns the same number. It returns the same number if that num, it returns num, if
num is greater than zero, or else it doesn't return anything, right? So this is important. So
what it does is if you input a positive number, it returns a number, or else it returns nothing.
Now let's look at it this way. Imagine, so you understood this function, right? Very simple
function here. Now let's imagine that I'm creating a list. So when I say range minus ten to
plus ten, and when I'm trying to print this, what it creates is this. I'm creating a list of
numbers in the range of minus ten to plus ten, okay? I have values from minus ten to up to
nine, right? I have a list of numbers here in this range. So of course all these are negative
numbers, right? Zero is neither negative nor positive, and these are all positive numbers,
right? Now this is where the function filter is extremely useful. Let's look at what it does
here. I'm using the function filter. For the function filter, the first value I'm giving is a
function. This is the name of a function. This is the name of a function, and this is a list,
right? This list is nothing but this numbers list is nothing but this list from minus ten to plus
nine, right? What about the name of the function? We just defined this function, which is
find positive number, which returns the number if it is greater than zero, or else it doesn't
return anything. Now look at the fun part here. So the filter has two variables. The name of
the function that you want to apply to filter out the points or to filter out the data, and a list,
right? Now what happens is when I apply the filter function, let's assume it returns me a list
called positive number list, right? Now, if I print the positive number list, this is what it
returns to me. So what filter did internally was for every number in this list. For every
number in the number list, it applied this function. It simply applied this function. And
wherever the function returned the value, it kept it in the new list. Wherever it didn't
return, it just discarded it. Right? So this is extremely useful. So this function is called filter,
because this function, if you have a larger list, let's say you have a list a and you want to
apply a function through which you want to filter out a bunch of points, right? If you want to
filter out a bunch of points from your larger list and create a new list, filter is the function to
use, as you just saw, play with this example. And you could also change this function here so
that you understand the function filter much better. It's used extensively in data science
because you always want to operate on lists like this. You want to apply a functional list. Of
course, you would have done this without the filter function by using a for loop. But filter
function makes the code much more elegant. Okay, so this is about simplicity of code. Of
course, we can do it with a simple for loop, without doubt. But this is more beautiful
because the language provides it for us. Now, there is a function called ease instance. So
what ease instance does is suppose. Imagine if I've created a variable called LST, which is a
list of 1234. Now, if I say print ease instance of the variable name, and this is the data type,
right? So what it tells me, what I'm asking here is ease LsT, a data variable of type list or not,
for which it returns true. Now let's now create a tuple, t, say 1234. It's a tuple. Remember,
this is a tuple, right? Now, when I say ease instance, t list, of course t is a tuple, not a list. So
when I say this, it returns false. So ease instance is used to find out whether a variable
belongs to a specific type of class or data type. Okay, next comes a class, very, very useful
and interesting data types, not data types, inbuilt functions map. These are some of my
favorite functions. You have functions like map and reduce, which are super useful. Let's
look at these examples. I've used map, filter and reduce a lot in data science. As you'll see
later. The map function again is a very elegant function. So imagine, let's take a simple
example. Imagine I have a list of numbers 1234. And imagine for each of these numbers, I
want to compute the square of these numbers, right? So I want to finally get a list which is
one square, which is one two square which is four three square which is nine, and four
square, which is 16. Imagine this is what I want to do. How do I do it. I can create. I mean,
this is the classical way of doing it. This is the old way of doing it without using our map
function. Okay, what I would do, I create a new list called squared for num. In numbers, I
just say squared append num power two. So this double star is basically means num power
two. This is what it means, right? This is an old way. And then I say print squared, and I get
the value. This is an old way of doing it. Is there a more elegant way? See, what am I doing
here? I'm taking each element of the list, and I'm using a for loop to achieve the task that I
want to do. This is perfectly all right, but map makes it much more elegant and simple and
without having to write the for loops. Now, let's look at it. So I have the same function
number. Sorry, I have the same variable numbers here. I'm defining a function. I'm defining
a function called power of two that given a number, it returns the square of the number. So
this function returns square of a number. It returns the square of a number. Now, without
having to write the for loop, you can just compute your new variable called squared, which
has the squares of all of these numbers using one line of code. This is the elegance of map.
So it works like this. I say map. This is the function name. This is the function name that I
want to apply to each of the values in this list. So here I have a list, right? So what I want to
do here is I want to apply this function to each of the values in this list. And the output, I
want to convert it back to a list. This is very, very simple. The first thing is basically the
function that I want to apply to each of the elements. I want to apply this function to each of
the elements, to each of the elements in this list. And whatever it returns, that's what map
does. What does filter do? Filter applies the same. Filter also behaves very similarly. It
applies this function to each element in the list. And wherever it returns false, it just
discards that element. So filter is, in a way, taking a subset of your list or any iterable
element, iterable data structure, and creating a new list. Here it's applying this function to
the list that you gave, and it's returning a new list. Very, very useful. So you will use filter
and map a lot. Similarly, there is a reduce function here, right? Reduce is also very, very
useful. So let's look at an example just to understand. Imagine I have a list of numbers, 1234,
and I want to find the product of all the elements in the list. How do I do it? I create a new
variable called product, give it a value of one, then use a simple for loop. I'm just using a
simple for loop, and I'm saying for number. In list, product equals to product multiplied by
number. And when I print product, I get 24, which is a product of all these numbers. This is
a classical way of doing it without using reduce. Now let's see how to do the same thing
while using reduce. Unfortunately, the reduce function used to be inbuilt in Python two. In
Python three, it's not readily available, so you need to import it. It's available, so you can just
import reduce using this, you can say from function tools, funk tools, import reduce. This
will help you import this function. Okay, now, the way I do it is this. I first define a function
called multiply. Given two numbers, it will just return the multiplication output. Now, the
reduce function works like this. You have the function name and you have a list. Now, what
it does is. So let's look at it. Okay, what it does is it will first apply this function on
consequent elements. It'll first multiply these two. What does that return? That returns two.
Now. Now, on this two, it will now apply it with this function, it'll apply the function
multiply with three. What does that return? Six. Now it'll apply this function, multiply
between six and four. What does that return? 24. That's what it finally returns at the end of
it. So, unlike map, which operates on each element individually, remember, map and filter
apply the function on each element individually. Right. But reduce operates differently.
Reduce basically applies the function from one element to other element consecutively.
Okay, so what it does is it applies a rolling computation. It applies something called a rolling
computation. So we have these elements. Right. So what does rolling computation mean? It
first applies the multiplication operator operation between one and two that returns two.
Now, instead of applying between two and three, it does a rolling operation, which means it
applies the multiplication operation, or the function multiply between two and three that
returns six. Now, instead of operating on three as a rolling computation, it operates on six.
And now when I multiply six and four, I get 24. This is how a rolling computation works.
This is oftentimes very useful when you want to find sum of numbers in an array or product
of numbers in an array or array or list in this case. Right? So this is what the reduce function
does. Now, of all the functions that we saw here, filter, map, and reduce are the most used
and most used from a data science perspective, these are very, very important inbuilt
functions for data science because these functions simply avoid the for loop. These
functions simply avoid the for loop, especially in data science, where, of course, we can
achieve what they're achieving using a for loop. But if we can avoid it, it'll make life simpler
and code more elegant. Okay, having said that, now let's go to some simple user defined
functions. These are very, very simple. Why do we write our own functions? Because
whenever we want to reuse the function, whenever you see the elegance of functions are,
suppose you write your function here. Suppose you write your function here. Now, if you
have to call this function multiple times at multiple parts of your code, now, instead of
writing the same code again and again here, you're reusing the code. That's one of the most
important reasons for defining your own functions. It makes code much more elegant, much
more manageable, and it avoids retyping the same code again and again. Now, let's come
here. Okay, so imagine if I have a function called product of the numbers, right? So what it's
doing, it's basically simply computing a product. Here, I'm defining a function. We know
how to define a function. So here, product numbers is a user defined function. This is a user
defined function. It's not ildmilt into Python. We are defining this function, and we can
simply call this function, as you all know, we can simply call this function like this, right?
Product of numbers, and I can give it two numbers. Very simple example. We all know this.
Now let's use all the stuff that we know to create a very simple calculator. It's a very, very
simple calculator. I'll just walk you through this code. It's a very, very simple example. So
I'm creating a function called add, which adds numbers. Multiply, subtract, and divide,
right? Very, very simple. I'm creating four simple functions. Now, I am saying, print. Print
these four values. Print, select option one for addition, two for subtraction, three for
multiplication, four for division, and I'm using the input function here, which says, which
lets me input a number, right? So whatever input the user gives, whether it's 1234, I put in a
variable called choice. Then I say input your number one and number two, right? Now, if
choice equals to one, what I'll do, I'll add these two numbers and print them. I'm using the
add function here. If choice equals to two, I subtract. If choice equals to three, multiply. If
choice equals to four, divide. Otherwise, else I just say invalid choice. Very, very simple code.
We can just run this right now, it's like straightforward. So if I say, let's say I want to do
multiplication, let's say my first number is 12.2, my second number is 2.3, right? So the
multiplication of both these numbers is 28.6, like you have 9995, but this is roughly equal to
28.6 because it's nine, nine, nine here, right? So very simple code here. The reason we
wanted to show this code here is because we are using all the concepts we learned, a lot of
concepts we learned here. So we are using the if else we are getting input, using the function
input and we are defining our own user defined functions, which are addition,
multiplication, subtraction, division, very, very simple example, nothing very fancy. So.