Sunday, June 26, 2011

Exceptions and Exception Handling

In this post I want to explain about the exception handling in Python.

Exceptions are the run time errors and we usually get this error when we run the code. These exceptions can be efficiently handled using some techniques. Exceptions arof different types and most common exceptions are ZeroDivisionError, NameError and TypeError.

ZeroDivisionError exception occurs when we try to divide by 0.

>>> 10 * (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ZeroDivisionError: integer division or modulo by zero

The last line of the error message shows the type of exception.

NameError occurs when we try to process on an undefined variable.

>>> 4 + spam*3
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'spam' is not defined

TypeError occurs when variables of different types are used in an expression.

>>> '2' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects

Other main exceptions are,
AssertionError, AttributeError, EOFError, OverflowError, IndentationError, ValueError etc

We can handle these exceptions by writing codes for them. We can add a ‘try/except’ structure to your code to handle exceptions.

When we write the try and except clauses, first the try clause is executed. If no exception occurs, the except clause is skipped and execution of the try statement is finished. If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement. If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.

Example code is given below.

If the we want to get an integer number as an input we can write the following code,

x = int(raw_input("Please enter a number: "))

But if the user inputs a non-integer value it will print an exception of type ValueError.
Here we can handle this exception by using the try and except. Look at the code given below.

    x = int(raw_input("Please enter a number: "))
except ValueError:
    print "Oops!  That was not a valid number.  Try again..."

A try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may name multiple exceptions as a parenthesized tuple, for example:

except (RuntimeError, TypeError, NameError):
    print “Enter a valid option”

We can also use except without any exception names. Look at the code below.

except IOError as (errno, strerror):
    print "I/O error({0}): {1}".format(errno, strerror)
except ValueError:
    print "Could not convert data to an integer."
    print "Unexpected error:", sys.exc_info()[0]

The try/except statement has an optional else clause, which must follow all except clauses.

    f = open(arg, 'r')
except IOError:
    print 'cannot open', arg
    print arg, 'has', len(f.readlines()), 'lines'

These are basic details about the exceptions and their handling.



List Comprehensions

I have published a post regarding the functional programming tools in Python. I explained about map(), reduce(), filter() and lambda() in that post. 

There is also another way to create lists using conditions and that method is called as list comprehension. This method is more clearer than the map(),lambda() etc. Each list comprehension consists of an expression followed by a ‘for’ clause, then zero or more for or ‘if’ clauses. The result will be a list resulting from evaluating the expression in the context of the ‘for’ and ‘if’ clauses which follow it. If the expression would evaluate to a tuple, it must be parenthesized.

The syntax is [ expr for var in list ]

Sample code is given below,

nums = [1, 2, 3, 4]
squares = [ n * n for n in nums ]
Output will be [1, 4, 9, 16]

Some more examples that can be experimented on interactive mode are given below.

>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [3*x for x in vec if x > 3]
[12, 18]
>>> [3*x for x in vec if x < 2]
>>> [[x,x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>> [(x, x**2) for x in vec]
[(2, 4), (4, 16), (6, 36)]

We can also add the ‘if’ clause as given below.

fruits = ['apple', 'cherry', 'bannana', 'lemon']
afruits = [ s.upper() for s in fruits if 'a' in s ]
Output will be ['APPLE', 'BANNANA']

List comprehensions can be nested. They are a powerful tool, but like all powerful tools, they need to be used carefully, if at all.



Saturday, June 25, 2011

More Sample Programs

If you have read my previous post, then you can continue with this post. Otherwise please read that post (click here)  to get some awareness about assert function.

Sample 1 : Two sorted lists are given. We need to merge them and keep the merged list also in ascending order. Don't use sort/sorted functions.

def merge(a, b):

    while len(list1) and len(list2):
        if list1[0] < list2[0]:

    return list3

def main():
    assert(merge([1], [2]) == [1,2])
    assert(merge([10, 20], [30]) == [10, 20, 30])
    assert(merge([10, 30, 40], [32, 33, 45, 57]) == [10,30,32,33,40,45,57])
    assert(merge([20, 25, 27, 34], [1, 10, 23]) == [1,10,20,23,25,27,34])
    assert(merge([1], []) == [1])
    assert(merge([], [1, 2]) == [1,2])
if __name__=='__main__':

Sample 2 : Insert an integer number to the sorted list and keep the list in sorted order even after the insertion. Don't use sort/sorted functions.

def insert_into(a, n):
    if len(list1)==0:
        return list1
    while i<len(list1):
       if list1[i]>n:
           return list1
       elif i==(len(list1)-1):
           return list1

def main():
    assert(insert_into([10, 20, 30], 24) == [10,20,24,30])
    assert(insert_into([5, 10, 20], 3) == [3,5,10,20])
    assert(insert_into([1, 10, 20], 30) == [1,10,20,30])
    assert(insert_into([], 10) == [10])
    assert(insert_into([1, 4, 18, 24, 27, 35, 87], 19) == [1,4,18,19,24,27,35,87])
if __name__=='__main__':

Sample 3 : This program is used to flatten the lists. i.e a list like this [1, [2, [3, [4]]]] will be flatten to a list [1,2,3,4].

def flatten(a):
    for small_list in main_list:
        if isinstance(small_list,list):
    return lista

def main():
    assert(flatten([[1,[2,3]]]) == [1,2,3])
    assert(flatten([[[1,2]]]) == [1, 2])
    assert(flatten([[[]]]) == [])
    assert(flatten([1, [2, [3, [4]]]]) == [1,2,3,4])
if __name__=='__main__':

Sample 4 : We have to partition a given list into two lists based on the first element of the list. We will have two partitioned lists as output, one holds all elements that are lesser than the first element of the input list and the second list holds all elements that are greater than the first element of the input list.

 def partition(a):
    for item in list1:
        if item<list1[0]:
        elif item>list1[0]:
    return list4

def main():
    assert(partition([10,8,2,11,14,6,1,13]) == [[8,2,6,1],[11,14,13]])
    assert(partition([1,2,3,4]) == [[],[2,3,4]])
    assert(partition([1]) == [[],[]])
    assert(partition([4,3,2,1]) == [[3,2,1],[]])
if __name__ == '__main__':

Sample 5 : Here we have to define two functions. add() adds a key "k" with value "v" to dictionary "d". get() return value corresponding to key "k" or return None if key k is not present. Main objective is to simulate a dictionary using  a list.

def add(d, k, v):
    if len(d)==0:
        return d
    for lists in d:
        if lists[0]==k:
            return d
    return d

def get(d, k):
    for lists in d:
        if lists[0]==k:
            return v
    return None

def main():
    assert(add([], "hello", 10) == [["hello", 10]])
    assert(add([["hello", 10]], "world", 20) == [["hello", 10], ["world", 20]])
    assert(add([["hello",10],["world",20]], "hello", 30) == [["hello",30],["world",20]])
    assert(get([["hello",10],["world",20]], "world") == 20)
    assert(get([["abc",1],["def",2]], "ijk") == None)
if __name__=='__main__':

I hope you will learn the basic concepts through these sample programs. You can also download these programs as a zip file. Download here.



Sample Python Programs

In this post I wish to add some basic and simple programs in Python which are experimented by me. This will be helpful to beginners of Python language. This examples are for script mode execution.

Before directly go to the program, I want to explain about assert function. Its a function generally used to check the correctness of another function. Programmers generally use this function to check the return value of a function. It checks the expression, and if the expression is true it doesn't perform any action. Otherwise if the expression is false, we get assertion error.

In my sample programs, I used the assertion function. If you run this program it will not display anything,because the code returns expected values. If you want to experiment on it insert print() in the functions and you can understand what is happening inside the programs. After modification,if you get an assertion error it means the code returns wrong values.

Sample 1 :  This program checks whether a given number is a factorial of any number.

def is_factorial(n):
    while i<=n:
        if f==n:
            return 1
        if f>n:
    return 0
def main():
    assert(not is_factorial(100))
if __name__=='__main__':

Sample 2 : This program checks whether the given tuple is in ascending order. Don't use sort/sorted functions.

def is_sorted(a):
   while i<len(x):
       if x[i-1]>x[i]:
           return 0
   return 1 

def main():
    assert(is_sorted((10, 20, 30, 32, 33)))
    assert(not is_sorted((2, 1)))
    assert(not is_sorted((1, 4, 7, 8, 6)))
    assert(not is_sorted((10, 20, 30, 25, 34, 45, 67)))
if __name__=='__main__':

Note that these are not professional codes and you can find out more simple and efficient codes for these problems. Five more samples will be published in my next post.



Wednesday, June 22, 2011

Functional Programming in Python

In C language we use functions, and normal parameters for function are variables and pointers. Did you ever think to pass another function as the parameter of a function? It is very complicated in C because we have to use complicated pointers for that purpose.

Being a very high level language, Python provides us a simple way to pass another function as a parameter to a function. This is called as higher order functions. Higher order functions have any of the following.
  • take one or more functions as an input
  • output a function
Python functions support this higher order functions and it is easy to implement. There are number of ways in which we can implement higher order function calls.

>>>def sqr(x): return x*x
>>>def cube(x): return x*x*x
>>>def compose(f, g,x): return f(g(x))
>>>compose(sqr, cube, 2)

Here 2 functions, sqr() and cube() are defined. Then we use these functions as parameters in the compose(). Compose takes sqr,cube and 2 as input and performs the action defined for it, i.e f(g(x)). This is an example where Python handles functions as first class objects.

We usually use the keyword ‘def’ to define functions. We can also implement functions that are not bound to a name. Python supports the creation of anonymous functions at runtime, using a construct called lambda. It is a very powerful concept that's well integrated into Python and is often used in conjunction with typical functional concepts like filter(), map() and reduce(). These functions are often called as functional programming tools. We will discuss about them after lambda.

How the lambda functions differ from normal functions? Take a look at the following codes,

#Normal function definition
>>> def f (x): return x**2
>>> print f(8)
#Lambda function definition
>>> g = lambda x: x**2
>>> print g(8)

Note that the lambda definition does not include a "return" statement, it always contains an expression which is returned. Also note that you can put a lambda definition anywhere a function is expected and you don't have to assign it to a variable at all.

Now we can discuss about the functional programming tools that are mentioned earlier in this post. The three built-in functions filter(), map() and reduce() are very useful when we use them with lists.

filter() has the syntax : filter(function, sequence). filter returns a sequence consisting of those items from the sequence for which function(item) is true. Have alook at the example code.

>>> def f(x): return x % 2 != 0 and x % 3 != 0
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]

Ultimately this codes finds prime numbers between 2 and 25. It takes the range as input and forms a list that includes only the values that satisfies the condition in parameter f(which is a function).

map() has the syntax : map(function, sequence). map calls the function(item) for all the values in the list. Example code is given below.

>>> def cube(x): return x*x*x
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>> map(cube, [1,2,3])
[1, 8, 27]

reduce() has the syntax as filter and map. It returns a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the next item, and so on.

>>> def add(x,y): return x+y
>>> reduce(add, range(1, 11))

This code is used to find the sum of numbers from 1 to 10.

We can use these three functions with lambda also. Take a look at the code given below.

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>> print map(lambda x: x + 10, foo)
>>> print reduce(lambda x, y: x + y, foo)

Here filter filters the elements in the list ‘foo’ and outputs the list of elements that are divisible by 3. map is used to perform x+10 on each element in the list. reduce find the sum of elements of the list ‘foo’.

The functional programming is the key feature of Python and these functions are usually used for that purpose.



Sunday, June 19, 2011

Script mode

Python is a dynamic, interpreted language. Source code does not declare the types of variables or parameters or methods. This makes the code short and flexible, and you lose the compile-time type checking in the source code.

We have already worked with interactive mode Python interpreter. Now we need to experiment with the Script mode. In script mode we have to write a set of statements in a file. The file must have the extension ‘.py’. The .py file can be run on terminal using the command,


We have to create a .py file using the vi editor. For that use the command,


Now we can write the statements and save it using ‘:wq’ in the file and execute it. I got a simple program from Google’s Python class,

# import modules used here -- sys is a very standard one
import sys

# Gather our code in a main() function
def main():
  print 'Hello there', sys.argv[1]
  # Command line args are in sys.argv[1], sys.argv[2] ..
  # sys.argv[0] is the script name itself and can be ignored

# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':

I wrote these codes in vi editor and executed it.

'python Ajay' is used to pass an argument of ‘Ajay’.

One file of Python code is called a module. The file "" is also known as the module "binky". A python module can be run directly -- as above "python Bob" -- or it can be imported and used by some other module. When a python file is run directly, the special variable "__name__" is set to "__main__". Therefore, it's common to have the boilerplate if __name__ ==... shown above to call a main() function when the module is run directly, but not when the module is imported by some other module.

We have the standard sys module that contains some standard system facilities, like the argv list, and exit() function. With the statement "import sys" another module can access the definitions in the sys module. The import does not actively import all the definitions; it just makes them available by their fully-qualified name, e.g. sys.exit().

One unusual Python feature is that the whitespace indentation of a piece of code affects its meaning. A logical block of statements such as the ones that make up a function should all have the same indentation, set in from the indentation of their parent function or "if" or whatever. If one of the lines in a group has a different indentation, it is flagged as a syntax error. Look at the above codes where I used indentation.

Inside the python interpreter, the help() function pulls up documentation strings for various modules, functions, and methods. These doc strings are similar to Java's javadoc. This is one way to get quick access to docs. Here are some ways to call help() from inside the interpreter:

  • help(len) -- docs for the built in len function (note here you type "len" not "len()" which would be a call to the function).
  • help(sys) -- overview docs for the sys module (must do an "import sys" first).
  • dir(sys) -- dir() is like help() but just gives a quick list of the defined symbols.
  • help(sys.exit) -- docs for the exit() function inside of sys.
  • help('xyz'.split) -- it turns out that the module "str" contains the built-in string code, but if you did not know that, you can call help() just using an example of the sort of call you mean: here 'xyz'.foo meaning the foo() method that runs on strings.
  • help(list) -- docs for the built in "list" module.
  • help(list.append) -- docs for the append() function in the list module.
Commonly used standard python modules include:
  • sys -- access to exit(), argv, stdin, stdout etc
  • re -- regular expressions
  • os -- operating system interface, file system
These are preliminary information regarding the script mode execution in Python Interpreter and I will post more about other features soon.



Exercises on Statements and Expressions

The Think Python author has given 2 more sample problems in order to understand the various statement and expression behaviours.

Assume that we execute the following assignment statements:
width = 17
height = 12.0
delimiter = '.'
For each of the following expressions, write the value of the expression and the type (of the value of the expression).
  1. width/2
  2. width/2.0
  3. height/3
  4. 1 + 2 * 5
  5. delimiter * 5
Use the Python interpreter to check your answers.

I’m trying to guess the answers first.
           1.      8
           2.     8.5
           3.     4.0
           4.     11
           5.     …..

Now I’m trying the same using interactive mode and the snapshot is given below.

Next problem is
Practice using the Python interpreter as a calculator:
The volume of a sphere with radius r is 4/3 π r3. What is the volume of a sphere with radius 5? Hint: 392.6 is wrong!

We should carefully use the integers and floating point values in division.