map in Python

The built-in map function is used to apply a function on the items of an iterable and return the results in another iterable.

# Syntax: map( function, iterable )
list( map( abs, [ -1, 4, -99 ] ) )
# [1,4,99]

If the function being used in map requires more than one input, that many number of parameters can be fed to it, by passing in that many number of iterables to map.

# Syntax: map( function, iterable0, iterable1, ... )
# pow() takes two input parameters
list( map( pow, [2,3,4], [3,3,3] ) )
# [8,27,64]

For more info, see Built-in Functions.

Tried with: Python 3.2.2

About these ads

unpack operator in Python

In Python, the * (asterisk) character is not only used for multiplication and replication, but also for unpacking. There does not seem to be any name for this kind of * operator and thus searching for it online is difficult. But, it is commonly called as the unpack or splat operator in this role.

Applying * on any iterable object, by placing it to the left of the object, produces the individual elements of the iterable.

I imagine this operator as shattering the container that holds the items together, so they are now free and individual. The look of the asterisk character helps bolster this imagination.

def foo( x, y, z ):
    print( "First is ", x, " then ", y, " lastly ", z )

a = [ 1, 50, 99 ]

foo( a )
# TypeError: foo() takes exactly 3 arguments (1 given)

foo( *a )
# First is 1 then 50 lastly 99

b = [ [55,66,77], 88, 99 ]
foo( *b )
# First is [55,66,77] then 88 lastly 99

For more info, see More Control Flow Tools.

Tried with: Python 3.2.2

iterable in Python

iterable is Python jargon for any object whose elements can be iterated. Typically, the built-in iter() method is applied on an iterable object to obtain an iterator. This iterator can only be used to move forward through the items of the iterable object from the beginning to the end, only once.

iterable is not a keyword. However, you will see it mentioned as the input parameter name in lots of built-in functions. This is an indication to the user that the function expects an iterable object as input.

For more info, see Glossary.

lambda in Python

In Python, lambda is a mechanism to create a small anonymous function. The function must take at least one parameter or more. The function body is restricted to a single expression. The lambda function can be passed anonymously to other functions that take a function as a parameter. Or it can be assigned a name and can be used just like a normal function, by calling by that name and passing it parameters.

# Syntax: lambda parameter(s): expression
lambda x: x     # Maps a parameter to itself
lambda x: x + 1 # Increments input parameter
lambda x: x * 2 # Doubles input

# Assign lambda a name
foo = lambda x: x * 2
foo( 10 )
# 20

For more info, see More Control Flow Tools.

Tried with: Python 3.2.2

Conditional Expression in Python

Python does not have the ternary operator seen in C-like languages. Instead it has the conditional expression. It is of the form X if C else Y:

i = 99
x = True if 99 == i else False
# True

In X if C else Y, either or both of X and Y can themselves be conditional expressions:

i = 99
j = 42
x = ( list() if 42 == j else tuple() ) if 99 == i else False
# []

The conditional expression can be used as a cool and easy-to-read alternative to a chain of if-else statements:

# Peter Norvig's example in Udacity CS212
return (
    9 if (5, ) == counts else
    8 if straight and flush else
    7 if (4, 1) == counts else
    6 if (3, 2) == counts else
    5 if flush else
    4 if straight else
    3 if (3, 1, 1) == counts else
    2 if (2, 2, 1) == counts else
    1 if (2, 1, 1, 1) == counts else
    0)

For more info on conditional expressions, see Expressions and PEP 308.

Tried with: Python 3.2.2

max and min in Python

With multiple input arguments, max returns the maximum amongst them.

max( 33, 99, 45 )
# 99

With single argument, which must be an iterable, max returns the maximum item in the iterable.

a = ( 33, 99, 45 )
max( a )
# 99
max( "Gandhi" )
# 'n'

max also takes an optional ordering function through its key argument. This function will be called with each item and is expected to return a value that max can use for finding its maximum.

def myfoo( i ):
    if "a" == i: return 10
    if "b" == i: return 99
    if "c" == i: return 50
max( "abc" )
# 'c'
max( "abc", key=myfoo )
# 'b'

All of the above applies to the min function too.

For more information, see Built-in Functions.

Tried with: Python 3.2.2

assert in Python

i = 99

# assert expression
assert 99 == i

# assert expression, message
assert 99 == i, "This is printed when this assert fails"
assert 99 == i, "Value of i is {}, not 99".format( i )

When an assert statement fails, AssertionError exception is raised. If the assert statement has a message, that message is printed along with the AssertionError message.

Tried with: Python 3.2.2