Python Deep Dive

A concise tutorial to a concise language

Prakat Solutions

9th July 2016


  1. Classes and objects (revisit)
  2. Tuples
  3. Modules & Packages
  4. Regular Expressions
  5. Exception Handling
  6. Automated testing
  7. Simple website setup
  8. Generators
  9. A simple scripting project

Classes and Objects

  • Objects are an encapsulation of variables and functions
  • Class is a template for an object
  • Defining a class 
  • Creating objects
  • Accessing object variables and functions
class MyClass(object):
    variable = "blah"

    def function(self):
        print "This is a message inside the class."
myobjectx = MyClass()

myobjecty = MyClass()
myobjecty.variable = "yackity"
print myobjectx.variable   # This would print "blah".
print myobjecty.variable   # This would print "yackity".


print type(myobjectx)


In this exercise, we are going to create a banking system.

Create a class called Account that represents a customer account. It must contain the following variables and functions:

  • Account Type- savings, personal or corporate
  • User name- A string
  • Account number- A number that uniquely identifies each account. This must be auto-generated
  • Current balance- a floating point value that represents the current balance
  • displayBal()- A function that displays the current balance
  • DepositCash(amt) - A function that adds ‘amt’ to the current balance and updates it. In the case of savings accounts, the current balance cannot fall below 500
  • DrawCash(amt) - A function that carries out cash withdrawals of ‘amt’ from the current balance. The funds must first be checked and then updated.

Create two objects of type Account:

  • ‘Acct1’ is a savings account that belongs to “Mishra” and has a current balance of Rs. 10000.00. He makes a deposit of Rs. 1500.00 into it, followed by a withdrawal of Rs. 7000.

  • ‘Acct2’ is a personal account that belongs to “Chopra” and has  a current balance of Rs. 5000. He makes 2 withdrawals of Rs. 4000 and Rs. 2000 each.


  • Like lists, but cannot be changed
  • Tuples are immutable lists
  • Used when data integrity is essential
myList = [1,2,3]


print (myList)

myTuple = (1,2,3)

print (myTuple)

#Generates an error!

print (myTuple)


  • A module is a .py file containing Python definitions and statements
  • Importing modules
  • Writing your own modules
# Fibonacci numbers module

# write Fibonacci series up to n
def fib(n):    
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b

# return Fibonacci series up to n
def fib2(n):   
    result = []
    a, b = 0, 1
    while b < n:
        a, b = b, a+b
    return result

>>> import fibo
>>> fibo.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987                                                                                                             
>>> fibo.fib2(100)                                                                                                                                                                                       
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
>>> fibo.__name__


  • Packages contain multiple packages and modules themselves
  • For ex, the module name A.B designates a submodule named B in a package named A
  • Like directories, but with a twist
#Package foo

#Defines packages & modules in foo

#a list of packages and modules                                                                                                                                                                                                                                                                                                                                                        
__all__ = ["bar"]

>>> import                                                                                                                                                                                                                                 
>>> from foo import bar

Exception Handling

  • Exceptions are run-time errors that halt program execution
  • try-except-else block
  • multiple arguments to except
  • except without type
  • finally block
import sys

	numerator = int(input("Enter the numerator: "))
	denominator = int(input("Enter the denominator: "))
	ans = numerator/denominator

except ZeroDivisionError: #when division by zero occurs
	print "answer is undefined"
	print "Oops!",sys.exc_info()[0],"occured :("
	print "Please try again!"
except ValueError: #when incorrect value is entered
	print "Incorrect value entered"
	print "Oops!",sys.exc_info()[0],"occured :("
	print "Please try again!"

except: #Handles all other exceptions
	print "Oops!",sys.exc_info()[0],"occured :("
	print "Please try again!"

	ans= numerator/denominator	
	print ans

	print "This is in the finally block"


You need to iterate over the list of 20 numbers, but the list is made from user input, and might not have 20 numbers in it.

Ask the user to input a list of integers. Iterate over a list of 20 numbers and display it. Check for Index errors(index value out of range) and any other errors encountered during user input.

In the case of index errors,  make the rest of the numbers as a 0 for display.

In the case of any other errors, print “Exception has been handled”


RE- Raw strings

  • The python parser interprets ‘\’ (backslashes) as escape characters in string literals.
  • Raw strings signal Python not to interpret backslashes and meta-characters in the string
  • begin with a special prefix (r)
string = 'This is a\nnormal string'
rawString = r'and this is a\nraw string'
print string
#This is a
#normal string
print rawString
#and this is a\nraw string

RE- functions

  • re package
  • re.match()- matches beginning
  • matches anywhere
  • re.findall()- all matching objects
  •, input_str, flags=0)
import re

match = re.match(r'dog', 'dog cat dog')

match = re.match(r'cat', 'dog cat dog')
#NoneType object error

match ='cat', 'dog cat dog')

re.findall(r'dog', 'dog cat dog')
#['dog', 'dog']

re.findall(r'cat', 'dog cat dog')

RE- match objects

  • search() and match() return match objects
  • stores group members
  • match.start()
  • match.end()
import re

match = re.match(r'dog', 'dog cat dog')

print match.start()
print match.end()

RE- metacharacters

  • ^ -beginning of a string.
  • $ - end of a string.
  • (x|y|z)  -x, y or z.
  • x? -zero or one times
  • x* - zero or more times.
  • x+x one or more times.
  • x{m,n} - m-n times.
  • (x) -remembered group.
import re

regex = r"([0-9]{1,3}\.){3}[0-9]{1,3}"

IP = raw_input ("Please, enter your IP address: ")
if re.match(regex, IP):
	match =, IP)
	print "Invalid IP address!"

RE- metacharacters

  • \b - word boundary.
  • \d - numeric digit.
  • \D -non-numeric character.
  • \w alphanumeric character
  • \W non-alphanumeric character
  • \s - whitespace
  • \S - not whitespace
import re
# Lets use a regular expression to match a date string. 
regex = r"(\w+) (\d+)"
if, "June 24"):
    match =, "June 24")
    # This will print [0, 7), since it matches at the beginning and end of the string
    print "Match at index %s, %s" % (match.start(), match.end())
    # So this will print "June 24"
    print "Full match: %s" % (
    # So this will print "June"
    print "Month: %s" % (
    # So this will print "24"
    print "Day: %s" % (
    # If does not match, then None is returned
    print "The regex pattern does not match. :("



Write a program that inputs an email ID from a user and checks if it is a valid one using regular expressions

Also, extract the domain name of each matched string

Automated Testing

Mike Cohn's test automation pyramid

Good read



So, where does Python fit in?


Code Introspection



>>> help(len)
Help on built-in function len in module __builtin__:

    len(object) -> integer
    Return the number of items of a sequence or collection.

>>> dir()
>>> x = 10
>>> id(x)

>>> callable(x)
>>> callable(len)


  • Used to create iterators
  • Return an iterable set of items, one at a time, in a special way
  • Returns control to for loop after yield statement
import random

def lottery():
    # returns 6 numbers between 1 and 40
    for i in range(6):
        yield random.randint(1, 40)

lottery_list = lottery()

for random_number in lottery_list:
    print "And the next number is... %d!" % random_number

Simple website setup

  • HTML
  • CSS
  • DOM
  • Hosting
  • Create a basic index.html page

Setup web framework

  • Install lpth.web

  • Simple hello world project
  • Directory structure
    • goweb/
      ├── bin
      ├── docs
      ├── goweb
      │   └──
      ├── templates
      └── tests


Simple hello world project

import web

urls = (
  '/', 'index'

app = web.application(urls, globals())

class index:
    def GET(self):
        greeting = "Hello World"
        return greeting

if __name__ == "__main__":

Run the app

  • Configure routes in bin/
  • Run the app
[abhiram@localhost goweb]$ python bin/ - - [08/Jul/2016 20:25:49] "HTTP/1.1 GET /" - 200 OK - - [08/Jul/2016 20:25:49] "HTTP/1.1 GET /favicon.ico" - 404 Not Found
  • Fixing errors by removing the greeting line
  • Creating simple templates - index.html

Create basic templates

  • Create templates/index.html
$def with (greeting)

        <title>Python at Prakat</title>

$if greeting:
    I just wanted to say <em style="color: green; font-size: 2em;">$greeting</em>.
    <em>Hello</em>, world!


Create basic templates

  • Configure routes in bin/
  • Run the app
import web

urls = (
  '/', 'index'

app = web.application(urls, globals())

render = web.template.render('templates/')

class index:
    def GET(self):
        greeting = "Hello World"
        return render.index(greeting= greeting)

if __name__ == "__main__":

A small scripting project

>>> python
Hello! What is your name?
Well, Abhiram, I am thinking of a number between 1 and 20.
Take a guess.
Your guess is too low.
Take a guess.
Your guess is too high.
Take a guess.
Good job, Abhiram! You guessed my number in 3 guesses!

Write a program able to play the "Guess the number"-game, where the number to be guessed is randomly chosen between 1 and 20. This is how it should work when run in a terminal:

Another python script

The International Civil Aviation Organization (ICAO) alphabet assigns code words to the letters of the English alphabet acrophonically (Alfa for A, Bravo for B, etc.) so that critical combinations of letters (and numbers) can be pronounced and understood by those who transmit and receive voice messages by radio or telephone regardless of their native language, especially when the safety of navigation or persons is essential. Here is a Python dictionary covering one version of the ICAO alphabet:

d = {'a':'alfa', 'b':'bravo', 'c':'charlie', 'd':'delta', 'e':'echo', 'f':'foxtrot',
     'g':'golf', 'h':'hotel', 'i':'india', 'j':'juliett', 'k':'kilo', 'l':'lima',
     'm':'mike', 'n':'november', 'o':'oscar', 'p':'papa', 'q':'quebec', 'r':'romeo',
     's':'sierra', 't':'tango', 'u':'uniform', 'v':'victor', 'w':'whiskey', 
     'x':'x-ray', 'y':'yankee', 'z':'zulu'}

Another python script

Your task in this exercise is to write a procedure speak_ICAO() able to translate any text (i.e. any string) into spoken ICAO words. You need to import at least two libraries: os and time. On Linux, you have access to the system TTS (Text-To-Speech) as follows: os.system('say ' + msg), where msg is the string to be spoken. (pyttsx)(Under Windows and Mac, something similar might exist.) Apart from the text to be spoken, your procedure also needs to accept two additional parameters: a float indicating the length of the pause between each spoken ICAO word, and a float indicating the length of the pause between each word spoken.

Thank You!


Python Deep Dive

By Abhiram Ravikumar

Python Deep Dive

Second lecture in the python workshop series

  • 2,064
Loading comments...

More from Abhiram Ravikumar