Chapter 9: Arrays, Strings & Vectors

example 9.1, page no. 157

In [3]:
number = [55, 40, 80, 65, 71]
print "Given List: ",
for num in number:
    print num,
number = sorted(number)[: : -1]
print "\nSorted List: ",
for num in number:
    print num,
Given List:  55 40 80 65 71 
Sorted List:  80 71 65 55 40

example 9.2, page no. 160

In [17]:
rows = 20
columns = 20
product = [[0 for x in range(rows)] for x in xrange(columns)]
print "Multiplication Table"
for i in range(10, rows):
    for j in range(10, columns):
        product[i][j] = i*j
        print " ", product[i][j],
    print " "
Multiplication Table
  100   110   120   130   140   150   160   170   180   190  
  110   121   132   143   154   165   176   187   198   209  
  120   132   144   156   168   180   192   204   216   228  
  130   143   156   169   182   195   208   221   234   247  
  140   154   168   182   196   210   224   238   252   266  
  150   165   180   195   210   225   240   255   270   285  
  160   176   192   208   224   240   256   272   288   304  
  170   187   204   221   238   255   272   289   306   323  
  180   198   216   234   252   270   288   306   324   342  
  190   209   228   247   266   285   304   323   342   361  

example 9.3, page no. 163

In [18]:
name = ["Madras", "Delhi", "Ahmedabad", "Calcutta", "Bombay"]
name = sorted(name)

for n in name:
    print n
Ahmedabad
Bombay
Calcutta
Delhi
Madras

example 9.4, page no. 164

In [4]:
aString = "Object language"
print "Original String : ", aString
print "Length of String: ", len(aString)
for i in range(len(aString)):
    print "Character at position: %d is %c" %(i+1,aString[i])

aString = aString.split(" ")
aString.insert(1, "-Oriented") 
aString = " ".join(aString)
print "Modified String: ", aString
aString = aString + " improves security."
print "Appended String: ", aString

# Note : In python, string is immutable so we can not edit/update it.
Original String :  Object language
Length of String:  15
Character at position: 1 is O
Character at position: 2 is b
Character at position: 3 is j
Character at position: 4 is e
Character at position: 5 is c
Character at position: 6 is t
Character at position: 7 is  
Character at position: 8 is l
Character at position: 9 is a
Character at position: 10 is n
Character at position: 11 is g
Character at position: 12 is u
Character at position: 13 is a
Character at position: 14 is g
Character at position: 15 is e
Modified String:  Object -Oriented language
Appended String:  Object -Oriented language improves security.

example 9.5, page no. 166

In [2]:
#vector is an array only in Python & commandline arguments cannot be taken in IPython Notebook

v1 = []
v1.append("Ada")
v1.append("BASIC")
v1.append("COBOL")
v1.append("C++")
v1.append("FORTRAN")
v1.append("Python")

print "List of Languages: "
for ele in v1:
    print ele
List of Languages: 
Ada
BASIC
COBOL
C++
FORTRAN
Python

example 9.6, page no. 169

In [4]:
"""
There is no wrapper class in Python.
"""

principalAmount = float(raw_input("Enter Principal Amount: "))
interestRate = float(raw_input("Enter Interest Rate: "))
numYears = float(raw_input("Enter Number of Years: "))

def loan(p, r, n):
    year = 1
    my_sum = p
    while year<=n:
        my_sum = my_sum*(1+r)
        year += 1
    return my_sum

def printline():
    for i in range(1, 30):
        print "-",
    print ""

value = loan(principalAmount, interestRate, numYears)
printline()
print "Final Value: ", round(value,2)
printline()
Enter Principal Amount: 5000
Enter Interest Rate: 0.15
Enter Number of Years: 4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Final Value:  8745.03
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

example 9.7, page no. 195

In [10]:
"""
There is no inbuilt module to use stack as it is done in the book "java.util.stack". We will use normal lists as stack in Python
"""

stack = []
stack.append(10)
stack.append(20)
stksum1 = stack.pop()
stksum2 = stack.pop()
stksum = stksum1 + stksum2
print "The top most element from the stack is: ", stksum2
print "The next top most element from the stack is: ", stksum1
print "The sum of two elements from the stack is: ", stksum
The top most element from the stack is:  10
The next top most element from the stack is:  20
The sum of two elements from the stack is:  30

example 9.8, page no. 196

In [18]:
"""
Enum is not supported by default in Python 2.X version. It has been backported to various versions of Python.
But, the package has to be installed seperately.
Using normal class here.
"""

class WorkingDays:
    Sunday = 0
    Monday = 1
    Tuesday = 2
    Wednesday = 3
    Thursday = 4
    Friday = 5
    Saturday = 6

def weekend(d):
    if d == WorkingDays.Sunday:
        print "Value = Sunday is a Holiday"
    elif d == WorkingDays.Monday:
        print "Value = Monday is a working day"
    elif d == WorkingDays.Tuesday:
        print "Value = Tuesday is a working day"
    elif d == WorkingDays.Wednesday:
        print "Value = Wednesday is a working day"
    elif d == WorkingDays.Thursday:
        print "Value = Thursday is a working day"    
    elif d == WorkingDays.Friday:
        print "Value = Friday is a working day"
    else:
        print "Value = Saturday is a working day"

for i in range(WorkingDays.Sunday, WorkingDays.Saturday+1):
    weekend(i)
Value = Sunday is a Holiday
Value = Monday is a working day
Value = Tuesday is a working day
Value = Wednesday is a working day
Value = Thursday is a working day
Value = Friday is a working day
Value = Saturday is a working day

example 9.9, page no. 199

In [50]:
"""
There is no concept of annotations in Python
"""

class MySingle:
    value = 0
    def __init__(self, x):
        self.value = 100
    def display(self):
         print "The value is: ", self.value

ob = MySingle(100)
try:
    ob.display()
except:
    print "No such method found..."
The value is:  100