Build-In function in Python : map()

map(function, sequence)

map() has two parameters : 1. function 2. sequence. It calls the function for each element in the sequence.

To see how map() works, let us first define a function called square.

def square(i): return i*i
square(4)

16

Now, apply map() on square function for range 0 to 5.

map(square, range(6))

[0, 1, 4, 9, 16, 25]

Now, apply map() on square function for range 13 to 23.

map(square,range(13,24))

[169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529]

Next, we define a remainder function.

def remainder(j): return j % 3
remainder(14)

2

Now, apply map() on remainder function for range 1 to 10.

map(remainder,range(1,11))

[1, 2, 0, 1, 2, 0, 1, 2, 0, 1]

Now, apply map() on remainder function for range 91 to 100.

map(remainder,range(91,101))

[1, 2, 0, 1, 2, 0, 1, 2, 0, 1]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s