You are on page 1of 4

Lambda Functions – used to create anonymous functions at runtime

using a construct called lambda.
g = lambda x: x**2
print g(8)
Filter( ), map( ), reduce( )
The advantage of the lambda operator can be seen when it is used in
combination with the map(), reduce and filter function.
map() is a function with two arguments:
r = map(func, seq)
Celsius = [39.2, 36.5, 37.3, 37.8] >>> Fahrenheit = map(lambda x:
(float(9)/5)*x + 32, Celsius) >>> print Fahrenheit [102.56,
97.700000000000003, 99.140000000000001, 100.03999999999999]
The function filter(function, list) offers an elegant way to filter out all the
elements of a list, for which the function function returns True.
fib = [0,1,1,2,3,5,8,13,21,34,55]
result = filter(lambda x: x % 2 == 0, fib) >>> print result [0, 2,
8, 34]
The function reduce(func, seq) continually applies the function func() to the
sequence seq. It returns a single value.
reduce(lambda x,y: x+y, [47,11,42,13])

List – A list in python is like an array in other lang/java. Python keeps
track of data types internally.
It supports negative indexing, slicing.
A negative index accesses elements from the end of the list counting
backwards. The last element of any non-empty list is always li[-1].
Slicing = list[1:3]
List[:3] = 2
List.index(“a” ) – returns the index of the element. Tuple doesn’t have
The difference between extend and append: Lists have two
methods, extend and append, that look like they do the same thing,
but are in fact completely different. extend takes a single argument,
which is always a list, and adds each of the elements of that list to the
original list.

Remove the item at the given position in the list, and return it. If no index is
specified, a.pop() removes and returns the last item in the list.
A tuple is an immutable list. A tuple cannot be changed in any way
once it is created.
Similarity to list
 A tuple is defined in the same way as a list, except that the
whole set of elements is enclosed in parentheses instead of
square brackets.
 The elements of a tuple have a defined order, just like a list.
Tuples indices are zero-based, just like a list, so the first element
of a non-empty tuple is always t[0].
 Negative indices count from the end of the tuple, just as with a
 Slicing works too, just like a list. Note that when you slice a list,
you get a new list; when you slice a tuple, you get a new tuple.
Different from list:
 You can't add elements to a tuple. Tuples have no append
or extend method. However, You can concatenate tuples –
(1,2) + (3,4) = 1,2,3,4)
 You can't remove elements from a tuple. Tuples have no
remove or pop method.
 You can't find elements in a tuple. Tuples have no index
 You can, however, use in to see if an element exists in the
Benefits of tuple:
 Faster than list
 Protected data – immutable
 Tuples can be used, as keys in dictionaries while list can’t
be. Because dictionary keys have to be immutable.
Tuples can be converted into lists, and vice-versa. The built-in tuple
function takes a list and returns a tuple with the same elements, and
the list function takes a tuple and returns a list. In effect, tuple freezes
a list, and list thaws a tuple.
The Python join() method is a string method, and takes a list of things to
join with the string. A simpler example might help explain:

>>> ",".join(["a", "b", "c"]) 'a,b,c'

One of Python's built-in datatypes is the dictionary, which defines oneto-one relationships between keys and values. It’s like a hash in other
del lets you delete individual items from a dictionary by key. E.g. - del
d[“ a” ]
clear deletes all items from a dictionary. Note that the set of empty
curly braces signifies a dictionary without any items.
Local & Global variable in python: Variables inside a function are local
and outside a function are global. If we have a variable s both inside
and outside function it will be treated separately. To modify the value of
global variable inside a function we need to declare it global i.e. global
s inside function.
Assert Statement: It is used to verify input or output.
assert (Expreesion), Exception comment
Sorting a list in python:
a = [1,4,3] => a.sort( ) => [1,3,4]
Range ­ The range() function has two sets of parameters, as follows:

stop :

Number of integers (whole numbers) to
starting from zero. eg. range(3) == [0, 1, 2] .


range([start], stop[, step])

start : Starting number of the sequence.

stop : Generate numbers up to, but not including this number.

step : Diff erence between each number in the sequence.
for i in range(4, 10, 2):

xrange vs range: For the most part, xrange and range are the exact same in terms 
of functionality. They both provide a way to generate a list of integers for you to 
use, however you please. The only difference is that range returns a 
Python list object and xrange returns an xrange object. It means 
that xrange doesn't actually generate a static list at run­time like range does. 
 Okay, now what does that mean? That means that if you have a really gigantic 
range you'd like to generate a list for, say one billion, xrange is the function to use.
This is especially true if you have a really memory sensitive system such as a cell 
phone that you are working with, as range will use as much memory as it can to 
create your array of integers, which can result in a MemoryError and crash your 
program. It's a memory hungry beast.
That being said, if you'd like to iterate over the list multiple times, it's probably 
better to use range. This is because xrange has to generate an integer object every 
time you access an index, whereas range is a static list and the integers are already 
"there" to use.