1.21. builtin functions
Contents
Note
Click here to download the full example code or to run this example in your browser via Binder
1.21. builtin functions#
This lesson shows the usage of builtin functions available in python.
Important
This lesson is still under development.
slice#
zip#
zip is used to iterate over two or more than two sequences.
1 11
2 12
3 13
hash#
all#
False
True
all#
True
False
True
False
sorted#
a
b
c
a
b
c
1
3
5
10.0
reversed#
c
b
a
enumerate#
0 a
1 b
2 c
map#
Suppose we have function which takes an input and returns square of it
def square(x):
return x**2
Now if we want to call this function on several values, we can make a list of all the values on which we want to call it
vals = [1,2,3,4,5]
and then we can call this function in a loss
1
4
9
16
25
an alternative to calling the function in an explicit for loop
is to make use of map
function.
THe map
function returns an iterator which we can convert into a list
print(type(mapper))
<class 'map'>
True
We can extract all values from iterator by calling list
method on it.
list(mapper)
[1, 4, 9, 16, 25]
we can also provide any builtin function as first argument to map
list(map(float, vals))
[1.0, 2.0, 3.0, 4.0, 5.0]
we can also use map with functions which take multiple input input arguments.
[1, 4, 27, 256]
- Map has several advantages over an explicit for loop e.g
It is fast since it is written in C
It is memory efficient as it returns an iterator
Total running time of the script: ( 0 minutes 0.007 seconds)