Baransel

Posted on

Python Embedded Functions

What is an embedded function?

Embedded functions are functions that come ready with the Python installation, unlike the functions we have created. Since these functions were previously developed by Python developers, we do not need to define these functions. E.g; These functions that we have used before, such as `print()`, `len()`, `type()`, `input()`, are embedded functions. We can directly call and use these functions without defining them. In this lesson, we will cover some important embedded functions that will be useful to us.

Some Embedded Functions

In this lesson, I will cover the most used embedded functions, if you wish, you can find other functions here.

`map()`

This function provides convenience in the operations you want to do with functions on variables, let me show you with an example before you get too confused, then you will understand very well. Before that, let's see the general outline of the `map()` function first.

`map(function,iter1,iter2)`

What we call an iterator can be a `list`, a `tuple`, or any other data type.

Let's have a list, and we square all the elements of this list.

``````list = [1,2,3,4,5,6,7,8,9,10]

def getSquare(number):
return number**2

map1 = map(getSquare,list)
``````

As you can see, we have assigned the `map()` function to the variable named `map1` and let's print it out immediately.

``````print(map1)

# <map object at 0x000002DFD0A76860>
``````

As you can see, we got a `map()` object, so we can see it, so let's convert the variable to a list type or another data type. Let's convert it to list type.

``````map1 = list(map1)
print(map1)

# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
``````

As you can see, we have squared all the elements of the list. We said that we can apply embedded functions much more easily with the `lambda()` function that we have processed in our previous lesson. Let's do the same example with the lambda function, then you will understand much better.

``````list = [1,2,3,4,5,6,7,8,9,10]

map2 = map(lambda number:number**2,list)

print(list(map2))
``````

As you can see, we can do it much more easily with the `lambda()` function.

If we make a concise explanation, the `map()` function takes multiple parameters, the first being a `function`, and applies the function to other parameters. So it's not limited to applying to just one data type. Let's show it with an example.

``````list1 = [1,3,5,9,7,6]
list2 = [6,8,7,9,4,6]
list3 = [8,4,9,3,1,5]

map2 = map(lambda x,y,z:x*y*z,list1,list2,list3)

print(list(map2))
``````

As you can see, we can apply the `map()` function on more than one data at the same time.

`reduce()`

Continue this post on my blog! Python Embedded Functions.