Python Programming -2

  • Database access
           What is MySQLdb?
           MySQLdb is an interface for connecting to a MySQL database server from Python. 
           Installing MySQLdb
           Download Installer from and install
           Install xampp
           Start apache and mysql
           Open phpmyadmin and create database and tables
  •  Class
            class Employee:
            'Common base class for all employees'
            empCount = 0
            def __init__(self, name, salary):
   = name
            self.salary = salary
            Employee.empCount += 1

            def displayCount(self):
            print "Total Employee %d" % Employee.empCount
            def displayEmployee(self):
            print "Name : ",, ", Salary: ", self.salary
            "This would create first object of Employee class"
            emp1 = Employee("Zara", 2000)
            "This would create second object of Employee class"
            emp2 = Employee("Manni", 5000)
            print "Total Employee %d" % Employee.empCount
  • Inheritance
           class Parent: # define parent class
           parentAttr = 100
           def __init__(self):
           print "Calling parent constructor"
           def parentMethod(self):
           print 'Calling parent method'
           def setAttr(self, attr):
           Parent.parentAttr = attr
           def getAttr(self):
           print "Parent attribute :", Parent.parentAttr
           class Child(Parent): # define child class
           def __init__(self):
           print "Calling child constructor"
           def childMethod(self):
           print 'Calling child method'
           c = Child() # instance of child
           c.childMethod() # child calls its method
           c.parentMethod() # calls parent's method
           c.setAttr(200) # again call parent's method
           c.getAttr() # again call parent's method
  • Overloading
           class Vector:
           def __init__(self, a, b):
           self.a = a
           self.b = b

           def __str__(self):
           return 'Vector (%d, %d)' % (self.a, self.b)

           def __add__(self,other):
           return Vector(self.a + other.a, self.b + other.b)

           v1 = Vector(2,10)
           v2 = Vector(5,-2)
           print v1 + v2

  • Overriding
           class Parent: # define parent class
           def myMethod(self):
           print 'Calling parent method'
           class Child(Parent): # define child class
           def myMethod(self):
           print 'Calling child method'
           c = Child() # instance of child
           c.myMethod() # child calls overridden method
  •  Multithresding
            Thread Module
            import thread
            import time
            # Define a function for the thread
            def print_time( threadName, delay):
            count = 0
            while count < 5:
            count += 1
            print "%s: %s" % ( threadName, time.ctime(time.time()) )
           # Create two threads as follows
           thread.start_new_thread( print_time, ("Thread-1", 2, ) )
           thread.start_new_thread( print_time, ("Thread-2", 4, ) )
           print "Error: unable to start thread"
           while 1:

          Threading Module:

          GUI Programing

          Tkinter Programming
          Tkinter is the standard GUI library for Python. Python when combined with Tkinter
          provides a fast and easy way to create GUI applications. Tkinter provides a powerful
          object-oriented interface to the Tk GUI toolkit.

  • CGI Programing