Python IV # Default Functions

Python’s Standard Tools/Default Functions

These default function generally are used by almost all experienced Python programmer to make their codes simple & easy to read. Knowing these will also help you save some time and also read other programmers code.

range: To generate a list of numbers we use range function.

>>> print(range(5))
[0, 1, 2, 3, 4]

In python3, let us say we have a bit of laziness so we have to do print(list(range(5))) instead of just range(5) to print. But on the other side of this lazy, we actually gain speed while working in real time.

lambda: To define anonymous functions on instinct

>>> f = lambda x : return x*2
>>> f(2)
4

map: To map things like function over a list of objects

>>> f = lambda x : x * 2
>>> list(map(f, [ 1, 2, 3 ]))
[ 2, 4, 6]

filter: Similar to map except, we select items from the list based on a filter function

>>> f = lambda x : x % 2 == 0 # filter funciton for finding odd number
>>> list(range(12))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> list(filter(f, range(12)))
[2, 4, 6, 8, 10]
>>>

reduce: Similar to map except, we take a list apply a function and reduce it to one. In Python3, this function is moved to functools. So to use reduce from functools import reduce for this job.

>>> f = lambda x, y : x + y
>>> reduce( f, [ 1, 2, 3 ])
6

all: To checks if all values are True or not.

>>> all([True, True, True ]])
True

any: To checks if any value is True or Not.

>>> any([True, False, False, False])
True

zip: While iterating helps us to take two items at a time.

>>> for x, y in zip ([0, 1, 3], [3, 1, 0]):
...     print x, y
0 3
1 1
3 0

sorted: Similar to sort function list. Here we are using key and reverse parameters to define criteria of the sort.

>>> sorted([2, 4, 1, 9, 4, 2], key = lambda x: -x, reverse=True)
[1, 2, 2, 4, 4 9]

hash: Hashes

>>> hash('hash my name')
>>> -8636703764421508254

counter:

from collections import Counter
x = Counter([1, 1, 12, 12, 21, 21, 21, 12, 122, 21, 21, 2 , 2, 2, 1, 1])
In [13]: x
Out[13]: Counter({21: 5, 1: 4, 2: 3, 12: 3, 122: 1})

Multiple Assignment Operator:

a = b = c = 12 # valid
a, b = 2, 1 # valid

List Comprehension:

List comprehensions are the simplest and quick forms of generating lists when we need to use multiple operators like filters and mappers.

Example 1:

>>> f = lambda x : x % 2 == 0 # filter funciton for finding odd number
>>> list(filter(f, range(12)))
[0, 2, 4, 6, 8, 10]
>>>
>>> # List comprehension
>>> [x for x in range(12) if x % 2 == 0]
[0, 2, 4, 6, 8, 10]

Example 2:

>>> list(map(lambda x: x ** 2, filter(lambda x: x % 2, range(12))))
[1, 9, 25, 49, 81, 121]
>>>
>>> [ x ** 2  for x in range(12) if x % 2]
[1, 9, 25, 49, 81, 121]

Example 3:

We are doing a list comprehension with two lists and also doing a filtering.

>>> [ x * y for x in range(12) for y in range(12) if x == y ]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121]

Conditional Assignment

>>> x = 12 if 1 == 1 else 1
>>> x
12
>>> x = 12 if 1 == 11 else 1
>>> x
1

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