A Class Example

#!/usr/bin/python

class Fraction:
      hi = low = 0
      def set(self, x, y):
          self.hi = x
          self.low = y
      def plus(self, num):
          self.hi = self.hi*num.low + num.hi*self.low
          self.low = self.low*num.low
      def minus(self, num):
          self.hi = self.hi*num.low - num.hi*self.low
          self.low = self.low*num.low
      def times(self, num):
          self.hi = self.hi*num.hi
          self.low = self.low*num.low
      def divide(self, num):
          self.hi = self.hi*num.low
          self.low = self.low*num.hi
      def print1(self):
          print "The fraction is ", self.hi, "/", self.low
      def tostring(self):
          return repr(self.hi) + " / " + repr(self.low)
      def reduce(self):
          factor = 2
          while (factor <= self.hi and factor <= self.low):
               if (self.hi % factor == 0 and self.low % factor == 0):
                  self.hi = self.hi / factor
                  self.low = self.low / factor
               else:
                  factor = factor+1

a = Fraction()
b = Fraction()
b.set(1,2)
a.set(1,3)
print "The fractions are",a.tostring(), "and", b.tostring()
a.minus(b)
a.print1()
a.plus(b)
a.print1()
a.times(b)
a.print1()
a.divide(b)
a.print1()
a.reduce()
a.print1()

The Output

The fractions are 1 / 3 and 1 / 2
The fraction is  -1 / 6
The fraction is  4 / 12
The fraction is  4 / 24
The fraction is  8 / 24
The fraction is  1 / 3