Python Notes-11

In continuation from
Python Notes-10
Python Notes-9
Python Notes-8
Python Notes-7
Python Notes-6
Python Notes-5
Python Notes-4
Python Notes-3
Python Notes-2
Python Notes-1

Exception


def spam(divideBy):
try:
return 42 / divideBy
except ZeroDivisionError:
print('Error: Invalid argument.')

try:
    ## Either of these two lines could throw an IOError, say
    ## if the file does not exist or the read() encounters a low level error.
    f = open(filename, 'rU')
    text = f.read()
    f.close()
  except IOError:
    ## Control jumps directly to here if any of the above lines throws IOError.
    sys.stderr.write('problem reading:' + filename)
  ## In any case, the code then continues with the line after the try/except

#!/usr/bin/python
import time; # This is required to include time module.

ticks = time.time()

#!/usr/bin/python
import time;

localtime = time.localtime(time.time())
print("Local current time :", localtime)

This would produce the following result, which could be formatted in any other presentable form −

Local current time : time.struct_time(tm_year=2013, tm_mon=7,
tm_mday=17, tm_hour=21, tm_min=26, tm_sec=3, tm_wday=2, tm_yday=198, tm_isds


localtime = time.asctime( time.localtime(time.time()) )

#!/usr/bin/python
import calendar

cal = calendar.month(2008, 1)
print ("Here is the calendar:")
print (cal)
This would produce the following result −

Here is the calendar:
January 2008
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31

Too much useless information above 🙂 I will compensate it with next post which will introduce pandas and numpy(Two great libraries).

To be continued …

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