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,
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 " "
name = ["Madras", "Delhi", "Ahmedabad", "Calcutta", "Bombay"]
name = sorted(name)
for n in name:
print n
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.
#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
"""
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()
"""
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
"""
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)
"""
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..."