0% found this document useful (0 votes)
38 views37 pages

Class 12 Practical File 23-24

Uploaded by

At Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views37 pages

Class 12 Practical File 23-24

Uploaded by

At Gamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

PRACTICAL FILE

COMPUTER SCIENCE
CODE:083
ACADEMIC YEAR: 2023-24

ROLL NO :

NAME :

CLASS : XII

SUBJECT : COMPUTER SCIENCE

SUBMITTED TO : Mr. SYED ZISHAN ABBAS

PGT (CS)

1
VARDAAN INTERNATIONAL ACADEMY

CERTIFICATE

This is to certify that ________________________________ student of class XII

Roll No: __________ has successfully completed the practical work in Computer

Science (083) under guidance of Mr. Syed Zishan Abbas during the academic year

2023-24

Number of practicals certified 25 out of 25 in the subject of Computer Science.

Signature of Principal’s Signature of

Internal Examiner Signature External Examiner

(Institution Rubber Stamp)

2
INDEX
Page
S. No. Name of the Practical Date Signature
No.
Input any number from user and calculate factorial of
01 06-04-23 05
a number.
Input any number from user and check, it is prime
02 number or not. 12-04-23 05-06

Write a program to find sum of elements of List


03 recursively. 19-04-23 06

Write a program to print Fibonacci series up to nth


04 24-04-23 07
term.
WAP to accept a string and display whether it is a
05 palindrome. 03-05-23 08
WAP for calculating simple interest.
06 13-05-23 09
WAP to accept percentage of a student and display its
07 grade accordingly. 20-05-23 10

WAP to accept a number and find out whether it is a


08 perfect number or not. 22-05-23 11

Read a text file line by line and display each word


09 separated by a #. 03-07-23 11

Read a text file and display the number of


10 vowels/consonants/ uppercase/lowercase characters 08-07-23 12
in the file.
Remove all the lines that contain the character 'a' in a
11 file and write it to another file. 12-07-23 13

Create a binary file with name and roll number. Search


12 for a given roll number and display the name, if not 15-07-23 14
found display appropriate message.
Create a binary file with roll number, name and marks.
13 Input a roll number and update the marks. 20-07-23 15

Write a random number generator that generates


14 random numbers between 1 and 6 (simulates a dice). 22-07-23 16

Create a CSV file by entering user-id and password,


15 read and search the password for given user id. 25-07-23 17-18

Write a menu-driven python program to implement


16 stack operation. 27-07-23 18-19

Write a program to implement a stack for the


17 employee details (empno, name,salary). 31-07-23 20-21

3
18 Write a python program to check whether a string is a 03-08-23 21-22
palindrome or not using stack.

Write a menu driven program using function Qadd( ),


19 Qdel( ) and disp( ) to add, delete and display the record 09-08-23 22-23
of book using queue as data structure in python.

Queries Set 1 (Database Fetching records)


20 18-08-23 23-25
Queries Set 2 (Based on Functions)
21 22-08-23 25-26

22 Queries Set 3 (DDL Commands) 25-08-23 27-28


23 Queries set 4 (Based on Two Tables) 30-08-23 28-29
24 Queries Set 5 (Group by, Order By) 12-09-23 30-31

Set-1: Write a MySQL connectivity program in Python


to
• Create a database school
• Create a table student with the specifications –
ROLLNO integer, STNAME character (10) in MySQL
25 and perform the following operations: 27-09-23 32-37
>Insert two records in it
>Display the contents of the table

Set-2: Perform all the operations with reference to


table ‘students’ through MySQL-Python connectivity.

4
Program 1: Input any number from user and calculate factorial of a number.

# Program to calculate factorial of entered number

num=int(input("Enter any number :"))

fact=1

n=num

while num>1:

fact=fact*num

num=num-1

print("Factorial of",n,"is :",fact)

OUTPUT:

Program 2: Input any number from user and check it is Prime number or not.

# Program to input any number from user

# Check it is Prime number or not

import math

num=int(input("Enter any number :"))

isPrime=True

for i in range(2, int(math.sqrt(num)+1)):

if num%i==0:

isPrime=False

if isPrime:

print("*Number is Prime*")

else:

print("*Number is not Prime*")

5
OUTPUT:

Program 3: Write a program to find sum of elements of List recursively.

# Program to find sum of elements of list recursively

def findSum(lst,num):
if num==0:
return 0
else:
return lst[num-1]+findSum(lst,num-1)
mylist=[] #Empty List
# Loop toinput in list
num=int(input("Enter how many number :"))
for i in range(num):
n=int(input("Enter Element"+str(i+1)+":"))
mylist.append(n) #Adding number to list
sum=findSum(mylist,len(mylist))
print("Sum of the List items",mylist,"is :",sum)
OUTPUT:

6
Program 4: Write a program to print Fibonacci series up to nth term.

# Python program to generate Fibonacci series until 'n' value

n = int(input("Enter the value of terms 'n': "))

a=0

b=1

sum = 0

count = 1

print("Fibonacci Series: ", end = " ")

while(count <= n):

print(sum, end = " ")

count += 1

a=b

b = sum

sum = a + b

OUTPUT:

7
Program 5: WAP to accept a string and display whether it is a Palindrome or not.

#Program to accept a string and display whether it is palindrome or not.

str=input ("Enter the string:")

l=len(str)

p=l-1

index=0

while (index<=p):

if(str[index]==str[p]):

index=index+1

p=p-1

else:

print ("String is not a palindrome!!")

break

else:

print ("String is a palindrome!!")

OUTPUT:

8
Program 6: WAP for calculating simple interest.

#Program to calculate simple interest.

principal = float (input ('Enter the principle amount: '))

time = float (input ('Enter the time: '))

rate = float (input ('Enter the rate: '))

simple_interest = (principal*time*rate)/100

print ("Simple interest is:", simple_interest)

OUTPUT:

9
Program 7: WAP to accept percentage of a student and display its grade accordingly.

#Program to accept percentage of a student and display its grade accordingly

a=float(input("Enter Marks in English :"))

b=float(input("Enter Marks in Maths:"))

c=float(input("Enter Marks in Physics:"))

d=float(input("Enter Marks in Chemistry:"))

e=float(input("Enter Marks in Computer Science:"))

avg=((a+b+c+d+e)/500)*100

if(avg>=80):

print ("Congratulations! You have secured first Class and your % is ", avg)

elif(avg>=60 and avg<80):

print ("Congratulations! You have secured Second Class and your % is ", avg )

elif (avg>=40 and avg<60):

print ("Congratulations! You have secured Third Class and your % is ", avg)

elif (avg<40):

print ("You are Fail")

OUTPUT:

10
Program 8: WAP to accept a number and find out whether it is a perfect number or not.

#Program to accept a number and find out whether it is a perfect number or not.
i=1
s=0
num=int (input ("Enter number:"))
while i<num:
if num%i==0:
s=s+i
i=i+1
if s==num:
print ("It is a perfect no.")
else:
print ("It is not a perfect no.")

OUTPUT:

Program 9: Read a text file line by line and display each word separated by a #.

#Program to read content of file line by line and display each word separated by '#'

f=open("file1.txt")
for line in f:
words=line.split()
for w in words:
print(w+'#',end='')
print()
f.close()

OUTPUT:

11
Program10: Read a text file and display the number of vowels/consonants/
uppercase/lowercase characters in the file.

#Program to read content of file and display


#Total no. of vowels, consonants, lower case and upper-case characters
f=open("file1.txt")
v=0
c=0
u=0
l=0
o=0
data=f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v=v+1
else:
c=c+1
if ch.isupper():
u=u+1
elif ch.islower():
l=l+1
elif ch!='' and ch!='\n':
o=o+1
print ("Toal Vowels in file :",v)
print ("Toal Consonants in file :",c)
print ("Toal Capital letters in file :",u)
print ("Toal Small letters in file :",l)
print ("Toal Other than letters in file :",o)
f.close()

OUTPUT:

12
Program 11: WAP to remove all the lines that contain the character 'a' in a file
and write it to another file.

# Program to remove all the lines that contain


# the character 'a' in a file and write it to another file.

f=open('file1.txt','r')

lines=f.readlines()

f.close()

f=open('file1.txt','w')
f1=open("file2.txt","w")

for line in lines:


if 'a' in line or 'A' in line:
f1.write(line)
else:
f.write(line)

print("All lines that contains a character has been removed from file1.txt")
print("All lines that contains a character has been saved in file2.txt")

f.close()

f1.close()

OUTPUT:

13
Program 12: Create a binary file with name and roll number. Search for a given
roll number and display the name, if not found display appropriate message.

import pickle
stud_data={}
list_of_students=[]
no_of_students=int(input("Enter no.of students:"))
for i in range(no_of_students):
stud_data["roll_no"]=int(input("Enter roll no.:"))
stud_data["name"]=input("Enter name:")
list_of_students.append(stud_data)
stud_data={}
file=open("stud_data.dat","wb")
pickle.dump(list_of_students,file)
file.close()

def search():
file=open("stud_data.dat","rb")
list_of_students=pickle.load(file)
roll_no=int(input("Enter roll no. to search:"))
found=False
for stud_data in list_of_students:
if(stud_data['roll_no']==roll_no):
found=True
print(stud_data['name'],"name, Found in file.")
if(found==False):
print("No student data found. please try again")
search()
file.close()

OUTPUT:

14
Program 13: Create a binary file with roll number, name and marks. Input a roll number
and update the marks.
import pickle
student=[]
f=open("student.dat","wb")
ans='y'
while ans.lower()=='y':
roll=int(input("Enter Roll Number:"))
name=input("Enter Name:")
marks=int(input("Enter Marks:"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open("student.dat","rb+")
student=[]
while True:
try:
student=pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r=int(input("Enter roll number to update:"))
for s in student:
if s[0]==r:
print("Name is:",s[1])
print("Current Marks is:",s[2])
m=int(input("Enter new Marks:"))
s[2]=m
print("Record Updated!!!")
found=True
break
if not found:
print("Sorry! Roll Number not found!!!")
ans=input("Update more?(Y):")
f.close()
OUTPUT:

15
Program14: Write a random number generator that generates random numbers
between 1 and 6 (simulates a dice).

#Program to generate random number 1-6, simulating a dice

import random
while True:
print("="*50)
print(" *****ROLLING THE DICE *****")
print("="*50)
num=random.randint(1,6)
if num==6:
print("Hey...You got ",num,"Congrats!!!")
elif num==1:
print("Well tried but you got",num)
else:
print("You got:",num)
ch=input("Roll Again?(Y/N)")
if ch in 'Nn':
break
print("Thanks for playing!!!!")

OUTPUT:

16
Program15: Create a CSV file by entering user-id and password, read and search the
password for given user id.

# CSV file by entering user-id and password, write, read and search

import csv
def write():

f=open("details.csv","w",newline='')
wo=csv.writer(f)
wo.writerow(["User Name","Password"])
while True:
u_id=input("Enter User ID:")
pswd=input("Enter Password:")
data=[u_id,pswd]
wo.writerow(data)
ch=input("Do you wanr to enter more record(Y/N)")
if ch in 'Nn':
break
f.close()

def read():

f=open("details.csv","r")
ro=csv.reader(f)
next(ro) #for ignoring heading of records
for i in ro:
print(i)
f.close()

def search():

f=open("details.csv","r")
found=0
u=input("Enter User ID to be search:")
ro=csv.reader(f)
next(ro)
for i in ro:
if i[0]==u:
print("Password is:",i[1])
found=1
if found==0:
print("Sorry...No Record Found!!!!")
f.close()

write()
read()
search()

17
OUTPUT:

Program 16: Write a menu driven program to add, delete and display the record of
Students, hostel using list as stack data structure in python.

Record of hostel contains the fields: Hostel number, Total Students and Total Rooms.

host=[ ]
ch='y'
def push(host):
hn=int(input("Enter hostel number:"))
ts=int(input("Enter Total students:"))
tr=int(input("Enter total rooms:"))
temp=[hn,ts,tr]
host.append(temp)
def pop(host):
if(host==[]):
print("No Record")
else:
val=host.pop()
print("Deleted Record is :",val)
def display(host):
l=len(host)
print("Hostel Number\tTotal Students\tTotal Rooms")
for i in range(l-1,-1,-1):
print(host[i][0],"\t\t",host[i][1],"\t\t",host[i][2])
while(ch=='y' or ch=='Y'):
print("1. Add Record\n")
print("2. Delete Record\n")
print("3. Display Record\n")

18
print("4. Exit")

op=int(input("Enter the Choice:"))

if(op==1):

push(host)

elif(op==2):

pop(host)

elif(op==3):

display(host)

elif(op==4):

break

ch=input("Do you want to enter more(Y/N):")

OUTPUT:

19
Program 17: Write a program to implement a stack for the employee details (empno,
name,sal).

#stack implementation using functions

employee=[]
def push():
empno=input("Enter empno ")
name=input("Enter name ")
sal=input("Enter sal ")
emp=(empno,name,sal)
employee.append(emp)
def pop():
if(employee==[]):
print("Underflow / Employee Stack in empty")
else:
empno,name,sal=employee.pop()
print("poped element is ")
print("empno ",empno," name ",name," salary ",sal)
def traverse():
if not (employee==[]):
n=len(employee)
for i in range(n-1,-1,-1):
print(employee[i])
else:
print("Empty , No employee to display")
while True:
print("1. Push")
print("2. Pop")
print("3. Traversal")
print("4. Exit")
ch=int(input("Enter your choice "))
if(ch==1):
push()
elif(ch==2):
pop()
elif(ch==3):
traverse()
elif(ch==4):
print("End")
break
else:
print("Invalid choice")

20
OUTPUT:

Program 18: Write a python program to check whether a string is a palindrome or not using
stack.

class Stack:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []

def push(self, data):


self.items.append(data)

def pop(self):
return self.items.pop()
s = Stack()
text = input('Please enter the string: ')

for character in text:


s.push(character)

reversed_text = ''
while not s.is_empty():
reversed_text = reversed_text + s.pop()

if text == reversed_text:
print('The string is a palindrome.')
else:

print('The string is not a palindrome.')

21
OUTPUT:

Program 19: Write a menu driven program using function Qadd( ), Qdel( ) and disp( ) to
add, delete and display the record of book using queue as data structure in python.

book=[ ]
ch='y'
def Qadd(book):
bn=input("Enter book name:")
bnum=int(input("Enter book number:"))
bp=int(input("Enter book price:"))
temp=[bn,bnum,bp]
book.append(temp)

def Qdel(book):
if(book==[ ]):
print("No Record!!!")
else:
print("Deleted Record is :",book.pop(0))

def disp(book):
l=len(book)
print("Book Name\tBook Number\tBook Price:")
for i in range(0,l):
print(book[i][0],"\t\t",book[i][1],"\t\t",book[i][2])

while(ch=='y' or ch=='Y'):
print("1. Add Record\n")
print("2. Delete Record\n")
print("3. Display Record\n")
print("4. Exit")
op=int(input("Enter the Choice:"))
if(op==1):
Qadd(book)
elif(op==2):
Qdel(book)
elif(op==3):
disp(book)
elif(op==4):
break
ch=input("Do you want to enter more(Y/N):")

22
OUTPUT:

20) Queries Set 1 (Database Fetching records):

Consider the following MOVIE table and write the SQL queries based on it.

23
24
21. Queries Set 2 (Based on Functions):

25
26
22) Queries Set 3 (DDL Commands)

27
23) Queries set 4 (Based on Two Tables)

Creating another table:

28
29
24) Queries Set 5 (Group by , Order By):

Consider the following table and write the queries:

30
31
32
33
34
35
36
37

You might also like