Solution of Practicals Class Xii Comp. Sci. 083 2022 23
Solution of Practicals Class Xii Comp. Sci. 083 2022 23
com/
CLASS-XII
SUBJECT – COMPUTER SCIENCE (083)
PRACTICAL FILE SOLUTION (Session 2022-23)
PRACTIC
OBJECTIVE & SOLUTION
AL NO.
1. Write a program in python to check a number whether it is prime or not.
num=int(input("Enter the number: "))
for i in range(2,num):
if num%i==0:
SOURCE print(num, "is not prime number")
CODE: break;
else:
print(num,"is prime number")
OUTPUT:
Page 1 of 30
CODE: n=num
res=0
while num>0:
rem=num%10
res=rem+res*10
num=num//10
if res==n:
print("Number is Palindrome")
else:
print("Number is not Palindrome")
OUTPUT:
Page 2 of 30
print(ord(ch))
elif choice==2:
val=int(input("Enter an integer value: "))
print(chr(val))
else:
print("You entered wrong choice")
Page 3 of 30
Press-1 to find the ordinal value of a character
Press-2 to find a character of a value
2
Enter an integer value: 65
A
Do you want to continue? Y/N
SOURCE
# main program
CODE:
Num1=int(input("Enter Number-1: "))
Num2=int(input("Enter Number-2: "))
print("Before Swap: ")
print("Num1: ", Num1)
print("Num2: ", Num2)
SwapNumbers(Num1, Num2)
Page 4 of 30
Enter Number-1: 10
Enter Number-2: 20
Before Swap:
Num1: 10
OUTPUT: Num2: 20
After Swap:
Num1: 20
Num2: 10
Page 5 of 30
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print(count)
Page 7 of 30
13
OUTPUT:
Write a program to write those lines which have the character 'p' from
9.
one text file to another text file.
fin=open("E:\\book.txt","r")
fout=open("E:\\story.txt","a")
s=fin.readlines( )
for j in s:
SOURCE if 'p' in j:
CODE: fout.write(j)
fin.close()
fout.close()
16
OUTPUT:
Page 9 of 30
pickle.dump(list, file)
file.close( )
file.close( )
Page 11 of 30
Enter roll number whose name you want to update in binary file :1202
Enter new name: Harish
OUTPUT:
Record Updated
14. Write a python program to delete student data from a binary file.
import pickle
roll = input('Enter roll number whose record you want to delete:')
file = open("student.dat", "rb+")
list = pickle.load(file)
found = 0
lst = []
for x in list:
if roll not in x['roll']:
SOURCE
lst.append(x)
CODE:
else:
found = 1
if found == 1:
file.seek(0)
pickle.dump(lst, file)
print("Record Deleted ")
else:
Page 12 of 30
print('Roll Number does not exist')
file.close( )
Enter roll number that you want to search in binary file :1202
OUTPUT:
Name of student is: Harish
Page 13 of 30
16. Write a program to perform read and write operation with .csv file.
import csv
def readcsv():
with open('C:\\ data.csv','rt')as f:
data = csv.reader(f)
for row in data:
print(row)
def writecsv( ):
with open('C:\\data.csv', mode='a', newline='') as file:
SOURCE writer = csv.writer(file, delimiter=',', quotechar='"')
CODE:
#write new record in file
writer.writerow(['4', 'Devansh', 'Arts', '404'])
Create a CSV file by entering user-id and password, read and search the
17.
password for given userid.
import csv
def searchcsv(id):
with open('E:\\data.csv','rt')as f:
SOURCE
data = csv.reader(f)
CODE:
for row in data:
if row[0]==id:
print(row[1])
Page 15 of 30
break
else:
print("Userid is not present")
def writecsv( ):
with open('E:\\data.csv','a', newline='') as f:
w = csv.writer(f)
writecsv()
userid=input("Enter userid for which you want to search password: ")
searchcsv(userid)
How many userid you want to enter: 3
OUTPUT: Enter userid 1
Saurav
Page 16 of 30
Enter Password: 123
Enter userid 2
Garima
Enter Password: 456
Enter userid 3
Anoop
Enter Password: 789
Enter userid for which you want to search password: Garima
456
18. Write a program for linear search.
L=eval(input("Enter the elements: "))
n=len(L)
item=eval(input("Enter the element that you want to search : "))
for i in range(n):
SOURCE if L[i]==item:
CODE: print("Element found at the position :", i+1)
break
else:
print("Element not Found")
Page 17 of 30
Enter the element that you want to search : 33
Element found at the position : 6
20. Write a menu based program to perform the operation on stack in python.
SOURCE def push(n):
CODE: L.append(n)
Page 18 of 30
print("Element inserted successfully")
def Pop( ):
if len(L)==0:
print("Stack is empty")
else:
print("Deleted element is: ", L.pop( ))
def Display( ):
print("The list is : ", L)
def size( ):
print("Size of list is: ", len(L))
def Top( ):
if len(L)==0:
print("Stack is empty")
else:
print("Value of top is: ", L[len(L)-1])
#main program
Page 19 of 30
L=[ ]
print("MENU BASED STACK")
cd=True
while cd:
print(" 1. Push ")
print(" 2. Pop ")
print(" 3. Display ")
print(" 4. Size of Stack ")
print(" 5. Value at Top ")
if choice==1:
val=input("Enter the element: ")
push(val)
elif choice==2:
Pop( )
elif choice==3:
Display( )
elif choice==4:
size( )
elif choice==5:
Page 20 of 30
Top( )
else:
print("You enetered wrong choice ")
Page 21 of 30
2. Pop
3. Display
4. Size of Stack
5. Value at Top
Enter your choice (1-5) : 3
['45']
Do you want to continue? Y/N
y
1. Push
2. Pop
3. Display
4. Size of Stack
5. Value at Top
Page 22 of 30
#Command to enter in a databse
mysql> USE RAILWAYS;
Database changed
Page 23 of 30
#Show all data/rows of the table
mysql> SELECT * FROM TRAINDETAILS;
Find the name and salary of those employees whose salary is between
B.
35000 and 40000.
SELECT Ename, salary
FROM EMPLOYEE
SOLUTION:
WHERE salary BETWEEN 35000 and 40000;
Find the name of those employees who live in guwahati, surat or jaipur
C.
city.
Page 24 of 30
SELECT Ename, city
FROM EMPLOYEE
SOLUTION:
WHERE city IN(‘Guwahati’,’Surat’,’Jaipur’);
D. Display the name of those employees whose name starts with ‘M’.
SELECT Ename
FROM EMPLOYEE
SOLUTION:
WHERE Ename LIKE ‘M%’;
SELECT Ename
FROM EMPLOYEE
SOLUTION:
WHERE Dept IS NULL;
Page 25 of 30
G. Find the average salary at each department.
SELECT Dept, avg(salary)
FROM EMPLOYEE
SOLUTION:
group by Dept;
Find maximum salary of each department and display the name of that
H. department which has maximum salary more than 39000.
Page 27 of 30
Write a program to connect Python with MySQL using database
25. connectivity and perform the following operation on data in database:
Insert record in the table
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
SOLUTION: democursor.execute("insert into student values (%s, %s, %s, %s, %s,
%s)", (1245, 'Arush', 'M', '2003-10-04', 'science', 67.34))
demodb.commit( )
FETCHMANY( )
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
SOLUTION: democursor=demodb.cursor()
democursor.execute("select * from student")
print(democursor.fetchmany(3))
Page 29 of 30
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
democursor=demodb.cursor( )
democursor.execute("update student set marks=55.68 where
SOLUTION:
admn_no=1356")
demodb.commit( )
import mysql.connector
demodb = mysql.connector.connect(host="localhost", user="root",
passwd="computer", database="EDUCATION")
SOLUTION: democursor=demodb.cursor( )
democursor.execute("delete from student where admn_no=1356")
demodb.commit( )
Page 30 of 30