#Program to read 5 data items from input and store them in an array and display their sum
#Local definitions
a = []
sum = 0
for i in range(0,5):
a.append(0)
print "Enter 5 integer numbers"
for i in range(0,5):
a[i] = eval(raw_input())
for i in range(0,5):
sum = sum + a[i]
print "The sum of given numbers is : %d\n" %sum
#Program to accept 5 numbers and print whether the number is even or not
#Local Variable
a=[]
for i in range(5):
a.append(0)
for i in range(0,5):
i+1
a[i]=eval(raw_input("Enter the %d number: "%(i+1)))
print "\n"
for i in range (0,5):
if a[i]%2==0:
print "%d Number is Even"%a[i]
else:
print "%d Number is Odd"%a[i]
#Program to illustrate how a character array can be used.
#Local definition
word=['F', 'R', 'I', 'E', 'N', 'D', 'S']
for i in range(0,7):
word.append(0)
for i in range(0,7):
print word[i],
#Program to sort the given strings alphabetically
b = raw_input("Enter the text to sort : ")
print "Sorted Text are : ",''.join(sorted(b, key=lambda v: (v.upper(), v[0].islower())))
#Program using two dimension array
#Local definition
stud = [[0 for i in xrange(0,4)] for i in xrange(0,4)] #stud is a array name with 4 rows and 2 columns
for i in range(0,4):
print "Enter the %d Student roll no and Mark:" %i
stud[i][1], stud[i][2] = input()
for i in range(0,4):
print "%d Student roll no %d mark %d" %(i, stud[i][1], stud[i][2])
#Program to illustrate Matrix Multiplication
a = [[0 for i in xrange(0,5)] for i in xrange(0,5)]
b = [[0 for i in xrange(0,5)] for i in xrange(0,5)]
c = [[0 for i in xrange(0,5)] for i in xrange(0,5)]
while True:
r1, c1 = input("Enter the size of the matrix A....")
r2, c2 = input("Enter the size of the matrix B....")
if c1 == r2:
print "Enter matrix A elements..."
for i in range (0,r1):
for j in range (0,c1):
a[i][j] = input()
print "Enter matrix B elements..."
for i in range (0,r2):
for j in range (0,c2):
b[i][j] = input()
for i in range(0,r1):
for j in range(0,c1):
c[i][j] = 0
for k in range(0,c1):
c[i][j] += a[i][k] * b[k][j]
print "The resultant matrix is...."
for i in range(0,r1):
for j in range(0,c1):
print "%d\t" %c[i][j],
print "\n"
break
else:
print "Multiplication is not possible"
#Program to print two dimensional array by row by row
arr = [[0 for i in xrange(0,4)] for i in xrange(0,4)]
for i in range(0,3):
for j in range(0,4):
arr[i][j] = (i * 4) + j + 1
print "Printing array contents :"
for i in range(0,3):
for j in range(0,4):
print "%3d" %arr[i][j],
print "\n"
#Program to add and display the results of 5 nos
#Function definition
def add(x,ar):
sum = 0
for i in range(0,5):
sum += ar[i]
print "Sum is ... %d" %sum
a = []
n = 5
for i in range(0,5):
a.append(0)
print "Enter 5 values"
for i in range(0,5):
a[i] = input()
add(n,a) #function call
#Program for capture and store n values into array and print them
#Function definition
def arr(a = []):
for i in range(0,5):
print "Value in array %d\n" %a[i]
a = []
for i in range(0,5):
a.append(0)
for i in range(0,5):
a[i] = i
arr(a) #Function call
#Program to print the character string from the array
name = "LAK"
print "%s" %name
#Program for explaining the working of 4 dimensional array
#Local definition
array_4d = [[[[0 for i in xrange(0,3)] for i in xrange(0,3)]for i in xrange(0,3)] for i in xrange(0,3)]
#looping statements
for a in range(0,3):
for b in range(0,3):
for c in range(0,3):
for d in range(0,3):
array_4d[a][b][c][d] = a + b + c + d
#Result
for a in range(0,3) :
print "\n"
for b in range(0,3):
for c in range(0,3):
for d in range(0,3):
print "%3d" %array_4d[a][b][c][d],
print "\n"