34
2014 - 2015 Computer Practical File Done By, Elton s dsouza XI B

Computer Practical File

Embed Size (px)

DESCRIPTION

using pythonelectronic storepratical file

Citation preview

Page 1: Computer Practical File

2014 - 2015

Computer Practical File

Done By,

Elton s dsouza

XI B

Page 2: Computer Practical File

Grade-11 Programs

1 Write a menu driven program to print the sum, difference, product and quotient and remainder of 2 numbers

2 Write a program to find the roots of a quadratic equation

3 Write a program to calculate area of a triangle using Heron's formula

4 Write a menu driven program to print the natural numbers, even numbers and odd numbers in the given range

5 Write a menu driven program to print the sum of natural numbers, even numbers and odd numbers in the given range

6 Program to print the multiplication table of a number

7 Write a menu driven program to print Factors of a number Factorial of a number Check the number is divisible by 8

8 Write a menu driven program to Check the number is palindrome Check the number is Armstrong number Print the sum of digits

9 Write a menu driven program to enter a number and do the following : Check the number is prime check the number is even or odd

10 Write a program to print the Fibonacci series

11 Write a program print the following pattern

******

12 Write a program to print the following pattern

111222333

13 Write a program to print the following pattern

Page 3: Computer Practical File

123123123

14 Write a program to print the following pattern

112123

15 Write a program to print the following pattern

321323

16 Write a program to print the following pattern

1234123121

17 Write a program to print the following pattern *

* * * * ** * * *

18 Write a program to print the following pattern

* * ** * * * * *

19 Write a program to print the following pattern 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1

20 Program to print the sum of following series:

Page 4: Computer Practical File

1+5+9+13+....+n terms

21 Program to print the sum of following series:1+x+x2+x3+....+n terms

22 Program to print the sum of following series:(1/3)+(2/5)+(3/7)+....+n terms

23 Program to print the sum of following series:(1/1!)+(2/2!)+(3/3!)+....+n terms

24 Program to print the sum of following series: 1+(1+2) +(1+2+3) +(1+2+3+4) +….

25 WAP to input a string and do the following:-1. Check if the string contains only alphabets2. Check if the string contains only digits3. Check if the string contains only alphanumeric characters

26 WAP to check the number of alphabets , digits and space in a string.

27 WAP to find the no. of vowels and consonants in a given string

28 WAP to check if the given string is palindrome.

29 WAP to input a string and convert to title case using built in function.

30 WAP to input a string and convert to swapcase:1) Using built in function2) Without using built in function

31 Write a program to enter ‘n’ data in a list and print highest and lowest value.

32 Write a program to enter ‘n’ data in a list and print the sum and average of the values.

33 Write a program to exchange the first half of the list with the next half.

34 Write a program to enter ‘n’ data in a list and exchange the values in adjacent places.a. using temporary variableb. without using temporary variable

35 Write a menu driven program to create a list and edit, append, delete and display elements

36 Write a program to print elements above and below the main and off diagonals.

37 Write a program to enter a matrix and print border and middle elements.

38 Write a menu driven program to create a matrix and display the following:1)Display main diagonal2)Display off diagonal3)Display main diagonal using single loop4)Display off diagonal using single loop

Page 5: Computer Practical File

39 Write a program to print sum and difference of 2 matrices.

40 Write a program to print row-wise and column-wise sum of a matrix.

41 Write a program to print the sum of elements above and below main and off diagonals.

42 Write a program to print the sum of main and off diagonals.

43 WAP to enter name and phone number in a dictionary.

44 WAP to enter name and marks of students in the form of a dictionary. Also search a student’s mark.

45 WAP to enter name and phone number in a dictionary.46 WAP to enter name and marks of students in the form of a dictionary. Also search a student’s

mark.

Answers

# 1 - menu-driven pgm to print sum, difference, product, quotient and remainder of 2 nos.

def main():

print "\n\n\t\t\tMENU - OPTIONS"

print "\n\t\t1 --> Sum"

print "\t\t2 --> Difference"

print "\t\t3 --> Product"

print "\t\t4 --> Quotient"

print "\t\t5 --> Remainder upon Dividing"

print "\t\t6 --> Exit"

choice=raw_input("\n\tEnter your choice - ")

n1=input("\n\tEnter first number - ")

n2=input("\n\tEnter second number - ")

if int(choice)==1:

print "\n\tSum =",n1+n2

Page 6: Computer Practical File

elif int(choice)==2:

print "\n\tDifference =",n1-n2

elif int(choice)==3:

print "\n\tProduct =", n1*n2

elif int(choice)==4:

print "\n\tQuotient =",n1/n2

elif int(choice)==5:

print "\n\tRemainder =",n1%n2

elif int(choice)==6:

print "\n\tExinting..."

exit()

main()

main()

# 2 - roots of quadratic eqn.

print "\n\n\t\tQuadratic Equation: ax^2 + bx + c"

a=float(input("\n\tEnter the value of 'a' - "))

b=input("\n\tEnter the value of 'b' - ")

c=input("\n\tEnter the value of 'c' - ")

d=(b**2)-(4*a*c)

if d<0:

print "\n\n\t\tNO REAL ROOTS FOUND!"

else:

print "\n\t\tSolution to equation - ",(((-1)*b)+(d**0.5))/(2*a),(((-1)*b)-(d**0.5))/(2*a)

# 3 - area of triangle using Heron's Formula

Page 7: Computer Practical File

print "\n\n\tHeron's Formula: sqrt(s*(s-a)*(s-b)*(s-c)), where s : semi-perimeter; a,b,c : lengths of three sides"

a=float(input("\n\tEnter the length of the first side - "))

b=input("\n\tEnter the length of the second side - ")

c=input("\n\tEnter the length of the third side - ")

s=(a+b+c)/2

print "\n\t\tAREA OF TRIANGLE =",round(((s*(s-a)*(s-b)*(s-c))**0.5),3),"sq. units"

# 4 - display natural, even and odd nos. within range

def main():

import math

print "\n\t\t\tMENU - OPTIONS"

print "\n\t\t1 --> Display natural nos."

print "\t\t2 --> Display even nos."

print "\t\t3 --> Display odd nos."

print "\t\t4 --> Exit"

choice=input("\n\tEnter your choice - ")

r1=float(input("\n\tEnter the initial range - "))

r2=input("\n\tEnter the final range - ")

for i in range(int(math.ceil(r1)),int(math.floor(r2))+1,1):

if choice==1:

print i

elif choice==2:

if i%2==0:

print i

elif choice==3:

if i%2!=0:

print i

elif choice==4:

print "Exiting..."

exit()

Page 8: Computer Practical File

main()

main()

# 5 - display sum of natural, even and odd nos. within range

def main():

import math

print "\n\t\t\tMENU - OPTIONS"

print "\n\t\t1 --> Sum of natural nos."

print "\t\t2 --> Sum of even nos."

print "\t\t3 --> Sum of odd nos."

print "\t\t4 --> Exit"

choice=input("\n\tEnter your choice - ")

if choice==4:

print "Exiting..."

exit()

r1=float(input("\n\tEnter the initial range - "))

r2=input("\n\tEnter the final range - ")

sum=0

for i in range(int(math.ceil(r1)),int(math.floor(r2))+1,1):

if choice==1:

sum+=i

elif choice==2:

if i%2==0:

sum+=i

elif choice==3:

if i%2!=0:

sum+=i

print "\n\tSum =",sum

main()

main()

# 6 - print multiplication table of given no.

Page 9: Computer Practical File

n=input("\n\tEnter the number - ")

m=input("\n\tEnter the number of multiples - ")

print "\n\tMultiplication Table of",n

print

for i in range(n,n*m+1,n):

print n,"x",i/n,"=",i

# 7 - menu driven pgm to find factors, factorial, and divisiblity test for 8

import math

def main():

print "\n\t\t\tMENU - OPTIONS"

print "\n\t1 --> Factors of a number"

print "\t2 --> Factorial of a number"

print "\t3 --> Divisibility test for 8"

print "\t4 --> Exit"

choice=input("\n\tEnter your choice - ")

if choice==1:

n=input("\n\tEnter a number - ")

print "\n\tFactors of",n,":"

for i in range(1,n+1,1):

if n%i==0:

print i

elif choice==2:

n=input("\n\tEnter a number - ")

print "\n\tFactorial of",n,"=",math.factorial(n)

elif choice==3:

n=input("\n\tEnter a number - ")

if n%8==0:

print "\n\tDivisible by 8 - 8 x",n/8,"=",n

else:

print "\n\tNot divisible by 8"

elif choice==4:

Page 10: Computer Practical File

print "\n\tExiting..."

exit()

main()

main()

# 8 - menu driven pgm to check if no. is palindrome or armstrong and to print the sum of digits

def main():

print "\n\t\tMENU - OPTIONS"

print "\n\t1 --> Palindrome check"

print "\t2 --> Armstrong no. check"

print "\t3 --> Sum of digits"

print "\t4 --> Exit"

choice=input("\n\tEnter your choice - ")

if choice==4:

print "\n\tExiting..."

exit()

n=input("\n\tEnter your number - ")

if choice==1:

if str(n)==str(n)[::-1]:

print "\n\tPalindrome"

else:

print "\n\tNot a palindrome"

elif choice==2:

s=0

for i in str(n):

s+=int(i)**len(str(n))

if s==n:

print "\n\tArmstrong Number"

else: print "\n\tNot an Armstrong number"

elif choice==3:

s=0

for i in str(n):

Page 11: Computer Practical File

s+=int(i)

print "\n\tSum of digits =",s

main()

main()

#9 - menu driven pgm to check if no. is even or odd and to check if it is prime

def main():

print "\n\t\tMENU - OPTIONS"

print "\n\t1 --> Prime check"

print "\t2 --> Even or odd no. check"

print "\t3 --> Exit"

choice=input("\n\tEnter your choice - ")

if choice==3:

print "\n\tExiting..."

exit()

n=input("\n\tEnter the number - ")

if choice==1:

for i in range(1,n,1):

if n%i==0:

print "\n\tNot a prime no."

break

else: print "\n\tPrime number"

elif choice==2:

if n%2==0:

print "\n\tEven number"

else: print "\n\tOdd number"

main()

main()

# 10 - menu driven pgm to print 'n' prime numbers & 'n' Armstrong nos.

while True:

print "\n\t\t\tMENU"

Page 12: Computer Practical File

print "\t\t1 --> Print 'n' prime numbers"

print "\t\t2 --> Print 'n' Armstrong numbers"

print "\t\t3 --> Exit"

choice=input("\nEnter your choice - ")

if choice==1:

n=input("\nEnter the number of prime nos. required - ")

print "\n",n,"prime numbers:"

no=0; i=1

while i<=n:

l=[]

for j in range(1,no+1):

if no%j==0:

l.append(j)

if len(l)==2:

print "\n#",i,"-",no

i+=1

no+=1

elif choice==2:

n=input("\nEnter the number of Armstrong nos. required - ")

print "\n",n,"Armstrong numbers:"

i=1; no=0

while i<=n:

no_=no; s=0

while no_!=0:

a=no_%10

s+=a**(len(str(no)))

no_=no_/10

if s==no:

print "\n#",i,"-",no

i+=1

no+=1

elif choice==3:

print "\nExiting..."

exit()

Page 13: Computer Practical File

else:

print "\nWrong choice! Try again!"

# 11 - menu driven pgm to check if no. is palindrome or armstrong

def main():

print "\n\t\tMENU - OPTIONS"

print "\n\t1 --> Palindrome check"

print "\t2 --> Armstrong no. check"

print "\t3 --> Exit"

choice=input("\n\tEnter your choice - ")

if choice==3:

print "\n\tExiting..."

exit()

n=input("\n\tEnter your number - ")

if choice==1:

if str(n)==str(n)[::-1]:

print "\n\tPalindrome"

else:

print "\n\tNot a palindrome"

elif choice==2:

s=0

for i in str(n):

s+=int(i)**len(str(n))

if s==n:

print "\n\tArmstrong Number"

else: print "\n\tNot an Armstrong number"

main()

main()

#12 - pgm to print Fibonacci series

x=0;y=1

n=input("\n\tEnter the number of terms - ")

print "\n\tTerms of Fibonacci series"

Page 14: Computer Practical File

print x

print y

for i in range(n-2):

s=x+y

print s

x=y

y=s

"""#13- pgm to print pattern:

*

**

***"""

n=input("Enter the maximum number of rows - ")

for i in range(1,n+1):

print "*"*i,

print

"""#14 - pgm to print pattern:

***

**

*"""

n=input("\n\tEnter the maximum number of rows - ")

for i in range(n,0,-1):

print "*"*i,

print

"""#15 - pgm to print pattern:

111

222

333"""

m=input("\n\tEnter the number of rows - ")

Page 15: Computer Practical File

n=input("\n\tEnter the number of columns - ")

for i in range(1,m+1):

for j in range(n):

print i,

print

"""#16 - pgm to print pattern:

123

123

123"""

m=input("\n\tEnter the number of rows - ")

n=input("\n\tEnter the number of columns - ")

for i in range(m):

for j in range(1,n+1):

print j,

print

"""#17 - pgm to print pattern:

1

12

123"""

m=input("\n\tEnter the number of rows - ")

for i in range(1,m+1):

for j in range(1,i+1):

print j,

print

"""#18 - pgm to print pattern:

321

Page 16: Computer Practical File

32

3"""

m=input("\n\tEnter the number of rows - ")

for i in range(1,m+1):

for j in range(m,i-1,-1):

print j,

print

"""#19 - pgm to print pattern:

1234

123

12

1"""

m=input("\n\tEnter the number of rows - ")

for i in range(m,0,-1):

for j in range(1,i+1):

print j,

print

"""#20 - pgm to print pattern:

*

* *

* * *"""

m=input("\n\tEnter the number of rows - ")

for i in range(1,m+1):

print " "*(m-i),

print "* "*i

print

Page 17: Computer Practical File

"""#21 - pgm to print pattern:

*

* *

* * *

* *

*"""

m=input("\n\tEnter the number of rows - ")

for i in range(1,m+1):

print " "*(m-i),

print "* "*i

for i in range(m-1,0,-1):

print " "*(m-i),

print "* "*i

"""#22 - pgm to print pattern:

1

121

12321"""

m=input("\n\tEnter the maximum number of rows - ")

for i in range(1,m+1):

print " "*(m-i),

for j in range(1,i+1):

print j,

for k in range(j-1,0,-1):

print k,

print

#23 - pgm to print sum of series: 1+5+9+13+..+'n' terms

n=input("\n\tEnter the number of terms - ")

s=0

Page 18: Computer Practical File

k=1

for i in range(n):

s+=k

k+=4

print "\n\tSum =",s

#24 - pgm to print the sum of series: 1+x+x^2+x^3+...+x^n

n=input("\n\tEnter the number of terms - ")

x=float(input("\n\tEnter the value of 'x' - "))

s=0

for i in range(n):

s+=x**(n-1)

print "\n\tSum =",s

#25 - pgm to print the sum of series: (1/3)+(2/5)+...+'n' terms

n=input("\n\tEnter the number of terms - ")

s=0.0

for i in range(1,n+1):

s+=(float(i)/(2*i+1))

print "Sum =",s

#26 - pgm to print the sum of series: (1/1!)+(2/2!)+...+(n/n!)

import math

n=input("\n\tEnter the number of terms - ")

s=0.0

for i in range(1,n+1):

s+=(float(i)/math.factorial(i))

print "\n\tSum =",s

#27 - pgm to print sum of series: 1+(1+2)+(1+2+3)+...

Page 19: Computer Practical File

n=input("\n\tEnter the number of terms - ")

s=0

for i in range(1,n+1):

s+=(i*(i+1))/2

print "\n\tSum =",s

#28 - pgm to check if alphabets, digits, and/or, alphanumeric chs. are in str

import string

str=raw_input("\n\tEnter the string - ")

if str.isalpha():

print "\n\tString contains only alphabets"

elif str.isdigit():

print "\n\tString contains only digits"

elif str.isalnum():

print "\n\tString contains alphanumeric characters"

else:

print "\n\tString does not contain only alphabets and/or digits"

#29 - pgm to find the number of alphabets, digits and spaces in a string

import string

str=raw_input("\n\tEnter the string - ")

str_a=str_d=str_s=0

for i in str:

if i.isalpha():

str_a+=1

elif i.isdigit():

str_d+=1

elif i.isspace():

str_s+=1

print "\n\tNumber of alphabets in string =",str_a

print "\n\tNumber of digits in string =",str_d

print "\n\tNumber of spaces in string =",str_s

#30 - pgm to find no. of vowels and consonants in string

Page 20: Computer Practical File

str=raw_input("\n\tEnter the string - ")

str_v=str_c=0

for i in str:

if i.isalpha():

if i in "aeiou":

str_v+=1

else: str_c+=1

print "\n\tNo. of vowels =",str_v

print "\n\tNo. of consonants -",str_c

#31 - pgm to check if string is palindrome

str=raw_input("\n\tEnter the string - ")

if str==str[::-1]:

print "\n\tString is a palindrome"

else: print "\n\tString is not a palindrome"

#32 - pgm to convert str to title case using title()

import string

str=raw_input("\n\tEnter the string - ")

print "\n\tString after title() - ",str.title()

#33 - pgm to swap case of string w/ & w/o built=in fn.

import string

str=raw_input("\n\tEnter the string - ")

print "\n\tString after swapcase() -", str.swapcase()

s=''

for i in str:

if i.isalpha():

if i.isupper():

s+=i.lower()

else:

s+=i.upper()

print "\n\tString after swapped cases w/o built-in function -",s

Page 21: Computer Practical File

#34 - pgm to enter 'n' values and print highest and lowest

n=input("\n\tEnter the number of values - ")

l=[]

for i in range(n):

j=float(input("\n\tEnter value #"+str(i+1)+" - "))

l.append(j)

print "\n\tHighest value =",max(l)

print "\n\tLowest value =",min(l)

#35 - pgm to enter 'n' values and print sum and average

n=input("\n\tEnter the number of values - ")

l=[]

for i in range(n):

j=float(input("\n\tEnter value #"+str(i+1)+" - "))

l.append(j)

print "\n\tSum =",sum(l)

print "\n\tAverage =",float(sum(l))/len(l)

#36 - pgm to swap the first and second halves of a list

n=input("\nEnter the number of elements to be inserted - ")

l=[input("\nEnter the element - ") for i in range(n)]

print "\nOriginal list...",l

l[int(round(float(n)/2)):],l[:int((n/2))]=l[:int((n/2))],l[int(round(float(n)/2)):]

print "\nAfter exchanging...",l

#37 - pgm to swap adjacent elements of a list

n=input("Enter the number of elements to be inserted - ")

l=[input("\nEnter the element - ") for i in range(n)]

m=list(l)

print "\nOriginal list...",l

for i in range(0,n-1,2):

Page 22: Computer Practical File

m[i],m[i+1]=m[i+1],m[i]

print "\nAfter swapping (without temporary variable)...",m

for i in range(0,n-1,2):

temp=l[i]

l[i]=l[i+1]

l[i+1]=temp

print "\nAfter swapping (with temporary variable)...",l

#38 - menu driven pgm to create a list, and edit, append, delete, and display elements

while True:

print "\n\t\t\t------MENU------"

print "\t\t1 --> Create a list"

print "\t\t2 --> Edit elements"

print "\t\t3 --> Append elements"

print "\t\t4 --> Delete elements"

print "\t\t5 --> Display elements"

print "\t\t6 --> Exit"

choice=input("\nEnter your choice - ")

if choice==1:

n=input("\nEnter the number of elements - ")

l=[raw_input("\nEnter the element - ") for i in range(n)]

elif choice==4:

print "\n\tINDEX\t\tELEMENT"

for i in range(n):

print "\t"+str(i)+"\t\t"+str(l[i])

no=input("\nEnter the number of elements to be deleted - ")

for i in range(no):

choice=input("\nEnter the index of the element to be deleted - ")

del l[choice]

elif choice==3:

a=input("\nEnter the number of elements to be appended - ")

Page 23: Computer Practical File

for i in a: l.append(raw_input("\nEnter the element - "))

elif choice==2:

print "\n\tINDEX\t\tELEMENT"

for i in range(n):

print "\t"+str(i)+"\t\t"+str(l[i])

no=input("\nEnter the number of elements to edit - ")

for i in range(no):

ind=input("\nEnter the index of the element to edit - ")

l[ind]=raw_input("\nEnter the element - ")

elif choice==5:

print "\n\tElements are:"

for i in l: print "\n\t",i

elif choice==6:

print "\nExiting..."

exit()

#39 - pgm to print elements above and below main and off diagonals of a matrix

def display(m,n,l):

for i in range(m):

for j in range(n):

print l[i][j],

print

def main():

m=input("Enter the number of rows - ")

n=input("Enter the number of columns - ")

l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]

display(m,n,l)

print "\nElements above the the main diagonal..."

for i in range(m):

for j in range(n):

if j>i:

print l[i][j],

else: print " ",

print

Page 24: Computer Practical File

print "\nElements below the the main diagonal..."

for i in range(m):

for j in range(n):

if j<i:

print l[i][j],

else: print " ",

print

print "\nElements above the the off diagonal..."

for i in range(m):

for j in range(n):

if j<m-1-i:

print l[i][j],

else: print " ",

print

print "\nElements below the the off diagonal..."

for i in range(m):

for j in range(n):

if j>m-1-i:

print l[i][j],

else: print " ",

print

main()

#40 - pgm to print the border and middle elements of a matrix

m=input("\nEnter the number of rows - ")

n=input("\nEnter the number of columns - ")

l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]

for i in range(m):

for j in range(n):

print l[i][j],

print

Page 25: Computer Practical File

print "\nBorder and middle elements..."

for i in range(m):

for j in range(n):

if (i==0 or i==m-1 or i==m/2) or (j==0 or j==n-1 or j==n/2):

print l[i][j],

else: print " ",

print

#41 - menu driven pgm to print main diagonal and off diagonal of a matrix using single loop or otherwise

m=input("\nEnter the number of rows - ")

n=input("\nEnter the number of columns - ")

l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]

print

for i in range(m):

for j in range(n):

print l[i][j],

print

while True:

print "\n\t\t---MENU---"

print "\n\t1 --> Main diagonal using nested loop"

print "\n\t2 --> Main diagonal using single loop"

print "\n\t3 --> Off diagonal using nested loop"

print "\n\t4 --> Off diagonal using single loop"

print "\n\t5 --> Exit"

choice=input("\nEnter your choice - ")

if choice==1:

print

for i in range(m):

for j in range(n):

Page 26: Computer Practical File

if j==i: print l[i][j],

else: print " ",

print

elif choice==2:

print

for i in range(m):

print " "*i,

print l[i][i]

elif choice==3:

print

for i in range(m):

for j in range(n):

if j==m-1-i: print l[i][j],

else: print " ",

print

elif choice==4:

print

for i in range(m):

print " "*(m-1-i),

print l[i][m-1-i]

elif choice==5:

print "\nExiting..."

exit()

#42 - pgm to print sum and difference of two matrices

def display(m,n,l):

print

for i in range(m):

for j in range(n):

print l[i][j],

print

def main():

m=input("\nEnter the number of rows - ")

n=input("\nEnter the number of columns - ")

Page 27: Computer Practical File

print "\n\tMATRIX - 1"

l1=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]

display(m,n,l1)

print "\n\tMATRIX - 2"

l2=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]

display(m,n,l2)

l_add=[[0 for j in range(n)]for i in range(m)]

l_sub=[[0 for j in range(n)]for i in range(m)]

for i in range(m):

for j in range(n):

l_add[i][j]=l1[i][j]+l2[i][j]

l_sub[i][j]=l1[i][j]-l2[i][j]

print "\nAddition of Matrices..."

display(m,n,l_add)

print "\nSubtraction of Matrices..."

display(m,n,l_sub)

main()

#43 - pgm to print sum of each row and column of a matrix

m=input("\nEnter the number of rows - ")

n=input("\nEnter the number of columns - ")

l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]

print

for i in range(m):

for j in range(n):

print l[i][j],

print

print "\n\tROW - WISE ADDITION"

for i in range(m):

s_row=0

Page 28: Computer Practical File

for j in range(n):

s_row+=l[i][j]

print "\nSum of Row",i+1,"=",s_row

print "\n\tCOLUMN - WISE ADDITION"

for j in range(n):

s_col=0

for i in range(m):

s_col+=l[i][j]

print "\nSum of Column",j+1,"=",s_col

#44 - pgm to print the sum of elements above and below the main & off diagonals

m=input("\nEnter the number of rows - ")

n=input("\nEnter the number of columns - ")

l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]

print

sum_a=sum_b=0

for i in range(m):

for j in range(n):

if j!=i:

sum_a+=l[i][j]

if j!=m-1-i:

sum_b+=l[i][j]

print l[i][j],

print

print "\nSum of elements above and below the main diagonal -",sum_a

print "\nSum of elements above and below the main diagonal -",sum_b

#45 - pgm to print the sum of main and off diagonals

m=input("\nEnter the number of rows - ")

n=input("\nEnter the number of columns - ")

l=[[input("\nEnter the element - ") for j in range(n)]for i in range(m)]

sum_m=sum_o=0

Page 29: Computer Practical File

for i in range(m):

for j in range(n):

print l[i][j],

if i==j: sum_m+=l[i][j]

elif j==m-1-i: sum_o+=l[i][j]

print

print "\nSum of main diagonals -",sum_m

print "\nSum of off diagonals -",sum_o

#46 - pgm to enter names and phone-numbers in a dictionary

n=input("\nEnter the number of names to be added - ")

phonebook={}

for i in range(n):

name=raw_input("\nEnter the name of the person - ")

number=raw_input("\nEnter the person's phone-number - ")

phonebook[name]=number

print "\n\tNAME\t\t PHONE-NUMBER"

for i in sorted(phonebook.keys()):

print "\n\t\t",i,"\t\t ",phonebook[i]

#47 - pgm to enter name and marks of students and search a student's mark

n=input("\nEnter the number of students to be added - ")

grades={}

for i in range(n):

name=raw_input("\nEnter the name of the student - ").lower().capitalize()

mark=raw_input("\nEnter the student's marks - ")

grades[name]=mark

print "\n\t\tSTUDENT\t\t\tMARKS"

for i in sorted(grades.keys()):

print "\n\t ",i,"\t\t\t",grades[i]

search=raw_input("\nEnter the student to search for his/her mark - ").lower().capitalize()

print "\nResult",grades.get(search, "Student not available")

Page 30: Computer Practical File