Geethanjali Vidyalaya Class Xii Sub: Computer Science Lab Programs (For Record) Programs 1 To 10
Geethanjali Vidyalaya Class Xii Sub: Computer Science Lab Programs (For Record) Programs 1 To 10
CLASS XII
SUB: COMPUTER SCIENCE
LAB PROGRAMS (FOR RECORD)
PROGRAMS 1 TO 10
Instructions:
➢ Write the question and Source code on the right hand side of the
record book.
➢ Output should be written in blank left hand side with pencil
➢ Submission date will be intimated shortly
1. Write a python program to perform the basic arithmetic operations in a menu-driven program
with different functions. The output should be like this:
Select an operator to perform the task:
‘+’ for Addition
‘-‘ for Subtraction
‘*’ for Multiplication
‘/’ for Division
def main():
print('+ for Addition')
print('- for Subtraction')
print('* for Multiplication')
print('/ for Division')
ch = input("Enter your choice:")
if ch=='+':
x=int(input("Enter value of a:"))
y=int(input("Enter value of b:"))
print("Addition:",add(x,y))
elif ch=='-':
x=int(input("Enter value of a:"))
y=int(input("Enter value of b:"))
print("Subtraction:",sub(x,y))
elif ch=='*':
x=int(input("Enter value of a:"))
y=int(input("Enter value of b:"))
print("Multiplication",mul(x,y))
elif ch=='/':
x=int(input("Enter value of a:"))
y=int(input("Enter value of b:"))
print("Division",div(x,y))
else:
print("Invalid character")
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
main()
2. Write a python program to enter a temperature in Celsius into Fahrenheit by using function.
def tempConvert():
cels = float(input("Enter temperature in celsius: "))
fh = (cels * 9/5) + 32
print('%.2f Celsius is: %0.2f Fahrenheit' %(cels, fh))
tempConvert()
5. Write a python program to accept username “Admin” as default argument and password 123
entered by user to allow login into the system.
def user_pass(password,username="Admin"):
if password=='123':
print("You have logged into system")
else:
print("Password is incorrect!!!!!!")
password=input("Enter the password:")
user_pass(password)
6. Write menu-driven python program using different functions for the following menu: 1 Check no.
is Palindrome or not
2 Check no. is Armstrong or not
3 Exit
def checkPalin(n):
temp=n
rem=0
rev=0
while(n>0):
rem=n%10
rev=rev*10+rem
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number is not a palindrome!")
def checkArmstrong(n):
temp=n
rem=0
arm=0
while(n>0):
rem=n%10
arm+=rem**3
n=n//10
if(temp==arm):
print("The number is an armstrong!")
else:
print("The number is not an armstrong!")
def menu():
print("1.Check no. is Palindrome:")
print("2.Check no. is Armstrong:")
print("3.Exit")
opt=int(input("Enter option:"))
no=int(input("Enter number to check:"))
if opt==1:
checkPalin(no)
elif opt==2:
checkArmstrong(no)
elif opt==3:
sys.exit()
else:
print("Invalid option")
menu()
7. Write a python program using a function to print prime numbers between 11 to 200.
start =11
end =200
print("Prime numbers between", start, "and", end, "are:")
for n in range(start, end + 1):
if n > 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print(n,",",end=" ")
8. Write a python program to print the following patterns using functions: 1. Diamond Pattern with *
2. Butterfly Pattern with *
3. Triangle Pattern with *
def pattern_diamond(n):
no = 0
for i in range(1, n + 1):
for j in range (1, (n - i) + 1):
print(end = " ")
while no != (2 * i - 1):
print("*", end = "")
no = no + 1
no = 0
print()
k=1
no = 1
for i in range(1, n):
for j in range (1, k + 1):
print(end = " ")
k=k+1
while no <= (2 * (n - i) - 1):
print("*", end = "")
no = no + 1
no = 1
print()
num=int(input("Enter no or lines to print:"))
pattern_diamond(num)
def pattern_butterfly(n):
for i in range(1, n + 1):
for j in range(1, 2 * n + 1):
if (i < j):
print("", end = " ");
else:
print("*", end = "");
if (i <= ((2 * n) - j)):
print("", end = " ");
else:
print("*", end = "");
print("");
for i in range(1, n + 1):
for j in range(1, 2 * n + 1):
if (i > (n - j + 1)):
print("", end = " ");
else:
print("*", end = "");
if ((i + n) > j):
print("", end = " ");
else:
print("*", end = "");
print("");
num=int(input("Enter no or lines to print:"))
pattern_butterfly(num);
def pattern_triangle(n):
for i in range(1, n+1):
for j in range(1, i+1):
print("* ",end="")
print("r")
num=int(input("Enter no or lines to print:"))
pattern_triangle(num)
9. Write a function to write data into binary file marks.dat and display the records of students who
scored more than 95 marks.
import pickle
def search_95plus():
f = open("marks.dat","ab")
while True:
rec=[]
data=[rn,sname,marks]
rec.append(data)
pickle.dump(rec,f)
break
f.close()
f = open("marks.dat","rb")
cnt=0
try:
while True:
data = pickle.load(f)
for s in data:
if s[2]>95:
cnt+=1
print("Record:",cnt)
print("RollNO:",s[0])
print("Name:",s[1])
print("Marks:",s[2])
except Exception:
f.close()
search_95plus()
10. Read a CSV file top5.csv and print the contents in a proper format. The data for top5.csv file are
as following:
SNo Batsman Team Runs Highest