Computer Science Practical File Khyati Kedia
Computer Science Practical File Khyati Kedia
SCHOOL
KANAKAPURA, KARNATAKA
PRACTICAL FILE
For
SUBMITTED BY
KHYATI KEDIA
JAIN INTERNATIONAL RESIDENTIAL SCHOOL
KANAKAPURA, KARNATAKA
Examination 2024.
………………………… ……………………………..
Signature of External Examiner Signature of Internal Examiner
1 10 – 6 – 24 MATH OPERATIONS 1
PROGRAM CODING:
a=int(input('enter number='))
b=int(input('enter number='))
c=a+b
d=a-b
e=a*b
f=a%b
output:
enter number=15
enter number=23
the sum is 8
the difference is 2
the product is 15
the quotient is 2
PROGRAM -2
Aim:
#Write a program to accept a number and print if the number is an
Armstrong number or not.
PROGRAM CODING:
p=int(input('enter the number='))
l=len(str(a))
k=0
t=p
while t>0:
j=t%10
k+=j**l
t=t//10
if k==p:
else:
OUTPUT:
It is not an Armstrong no
PROGRAM -3
Aim:
#Write a program to accept a number and print if the number is a prime
number or not.
PROGRAM CODING:
output: a=int(input("enter a number="))
y=0
for i in range(2,(a//2+1)):
if (a%i==0):
y=1
break
if(y==0):
else:
OUTPUT:
enter a number=5
it is a prime number
PROGRAM -4
Aim:
#write a program to create a function factorial to accept a number and
print the factorial of the number.
PROGRAM CODING:
num =int(input("Enter a number: "))
f=1
if num<0:
elif num==0:
else:
f=f*i
OUTPUT:
Enter a number: 54
The factorial of 54 is
23084369733924138047209274268302758108327856
4571807941132288000000000000
PROGRAM -5
Aim:
#print the number of words in the string, function palindrome to print if
the string is palindrome and function count to count the occurrence of the
letter ‘T’ in the string.
PROGRAM CODING:
def words():
a=input("enter str:")
b=a.split()
print(len(b))
def palindrome():
a=input("enter word:")
if (a[::-1]==a):
print("string is palindrome")
else:
def count():
b=0
for i in a:
if(i=="t"):
b+=1
print(b)
words()
palindrome()
count()
OUTPUT:
enter str:i love studying computer science
enter word:mom
string is palindrome
2
PROGRAM -6
Aim:
# A Program to enter a list of elements and pass the list to a function and
display the elements and index of the non-zero elements.
PROGRAM CODING:
def elements(a):
for i in range(0,len(a)):
if(a[i]!=0):
print(i)
l=eval(input("values??"))
elements(l)
OUTPUT:
values??[1,4,7,0,70,0,5,8]
7
PROGRAM -7
Aim:
# A program to find area of different shapes using user defined functions
PROGRAM CODING:
choice='yes'
def circle(r):
def rectangle(l,b):
def square(s):
def triangle(b,h):
while choice=='yes':
print('1.circle')
print('2.rectangle')
print('3.square')
print('4.triangle')
if choice2==1:
elif choice2==2:
rectangle(l,b)
elif choice2==3:
square(s)
else:
triangle(b,h)
OUTPUT:
1.circle
2.rectangle
3.square
4.triangle
2.rectangle
3.square
4.triangle
PROGRAM CODING:
a=open(r"C:\Users\students\Desktop/kesar.txt")
b=a.read()
c=b.split()
for i in c:
print(i,"#")
OUTPUT:
prime #minister #of #india #is #mr #modi #
PROGRAM -9
Aim:
# Read a text file and display the number of
PROGRAM CODING:
y=open(r'C:\Users\students\Desktop\kesar.txt',"r")
x=y.read()
v=0
c=0
l=0
u=0
for i in x :
l=l+1
u=u+1
for i in x :
if i in ['a','e','i','o','u']:
v=v+1
else:
c=c+1
print(l)
print(v)
print(u)
print(c)
OUTPUT:
36
15
28
PROGRAM -10
Aim:
# A program to remove all the lines that contain the character ‘a’ in a file
and write it to another file.
PROGRAM CODING:
y=open(r'C:\Users\students\Desktop\kesar.txt',"r")
z=open(r"C:\Users\students\Desktop\cs.txt","w")
x=y.readlines()
k=[]
for i in x :
if 'a' not in i :
k.append(i)
z.writelines(k)
z.close()
OUTPUT:
PROGRAM -11
Aim:
# A Program to read a text file and count the number of occurrence of a
word in the text file.
PROGRAM CODING:
a=open(r"C:\Users\students\Desktop/kesar.txt")
f=0
b=a.read()
c=b.split()
for i in c:
if (i==k):
f=f+1
print(f)
OUTPUT:
enter the word?prime
1
PROGRAM -12
Aim:
# Create a binary file with roll number and name. Input a roll number and
search the student with a specific roll number and display the details.
PROGRAM CODING:
import pickle
l=[]
def writing():
y=open(“stu.dat”,”wb”)
for i in range(1,3):
n=int(input”roll no”))
na=input(“name”)
l.append([n,na])
pickle.dump(l,y)
y.close()
def reading():
y=open(“stu.dat”,”rb+”)
n=int(input(“roll no”))
p=y.tell()
a=pickle.load(y)
na=input(“name”)
for i in a:
if(i[0]==n):
print i[0]
print i[1]
y.close()
def display():
y=open(“stu.dat”,”rb+”)
a1=pickle.load(y)
print(a1)
writing()
reading()
display()
OUTPUT:
roll no1234
namekhyati
roll no2345
namearyan
roll no1235
nameashka
PROGRAM CODING:
import pickle
l=[]
def wr():
y=open(“binary13.dat”,”wb”)
for i in range(0,2):
l.append([na,roll,ma])
pickle.dump(l,y)
y.close()
def read():
y=open(“binary13.dat”,”rb+”)
edited:"))
a=pickle.load(y)
for i in a:
if i[0]==roll:
i[2]=ma
y.close()
def display():
y=open(“binary13.dat”,”rb+”)
a1=pickle.load(y)
print(a1)
wr()
read()
display()
OUTPUT:
enter the name:pri
PROGRAM CODING:
import csv
def addrec():
with open(“cs.csv”,”w”,newline=” “) as f:
b=csv.writer(f)
b.writerow([“UserID”,”Password”])
ans=”y”
b.writerow([uID,p])
print(“record stored”)
def searchrec():
with open(“cs.csv”,”r”) as f:
b=csv.reader(f)
ans=”y”
found=False
for r in b:
if str(uID)==str(r[0]):
found=True
break
if found==False:
addrec()
searchrec()
OUTPUT:
Enter Login Details:
record stored
PROGRAM CODING:
import csv
found=0
a=open(r'C:\Users\students\Desktop\kesar.csv',"w",newline="")
b=csv.writer(a)
c=int(input("number of people"))
l=[]
for i in range(0,c):
d=int(input("user id?"))
e=input("pass?")
l.append([d,e])
b.writerows(l)
a.close()
a=open(r'C:\Users\students\Desktop\kesar.csv',"r")
k=csv.reader(a)
a.seek(0)
y=a.tell()
if(str(i[0])==str(h)):
print(i[1])
found=1
pa=i[1]
break
if(found==1):
print("password=",pa)
else:
print("not found")
OUTPUT:
number of people2
user id?1
pass?gg
user id?2
pass?hh
enter user id 1
gg
password= gg
PROGRAM -16
Aim:
# A program to implement the concept of stack.
PROGRAM CODING:
l=[ ]
def push():
k=int(input("enter a number:"))
l.append(K)
print(l)
def pop1():
if l==[]or len(l)==0:
print("stack is empty")
else:
l.pop()
def disp():
if l==[]:
print("stack empty")
else:
print(l[::-1])
def peak():
if l==[]:
print("stack empty")
else:
print(l[-1])
choice="yes"
while choice=="yes":
print("1.push")
print("2.pop")
print("3.display")
print("4.peak")
if choice1 == “1”:
push()
pop()
disp()
peak()
OUTPUT:
1.push
2.pop
3.display
4.peak
enter the choice1
enter11
[11]
do you want to continueno
PROGRAM -17
Aim:
# A Program to create a product table and insert and display data.
PROGRAM CODING:
import mysql.connector
con=mysql.connector.connect(host="localhost" , user="root" ,
password="jirs" ,charset="utf8", database="productdetails");
cur=con.cursor()
def adddata():
for i in range(0,3):
productid=int(input("enter pid="))
productname=input("enter pname=")
price=int(input("price=$"))
quantity=int(input("qty="))
dop=input("date of purchase=")
t=(productid,productname,price,quantity,dop)
cur.execute(q,t)
con.commit()
adddata()
def display():
for i in cur:
print(i)
display()
OUTPUT:
id of the product 12
name of productpen
price100
id of the product 11
name of productpencil
price50
id of the product 13
name of productbook
price300
PROGRAM -18
Aim:
# A Program to update and display the data after modifying Table.
PROGRAM CODING:
import mysql.connector
con=mysql.connector.connect(host="localhost" , user="root" ,
password="jirs" ,charset="utf8", database="productdetails");
cur=con.cursor()
def update():
productid=int(input("enter pid="))
productname=input("enter pname=")
price=int(input("price=$"))
quantity=int(input("qty="))
dop=input("date of purchase=")
t=(productname,price,quantity,dop,productid)
cur.execute(q,t)
con.commit()
def display():
cur.execute(q)
for i in cur:
print(i)
display()
update()
display()
OUTPUT:
PROGRAM CODING:
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",pa
ssword="jirs",charset="utf8",database="students")
cur=con.cursor()
def add():
for i in range(0,3):
rollno=int(input("rollno="))
name=input("name=")
class2=input("class=")
t=(rollno,name,class2)
cur.execute(q,t)
con.commit()
def update():
rollno=int(input("rollno="))
name=input("name=")
class2=input("class")
q="update school set name=%s,class1=%s where rollno=
%s;"
t=(name,class2,rollno)
cur.execute(q,t)
con.commit()
def delete():
rollno=int(input("enter"))
t=(rollno,)
cur.execute(p,t)
con.commit()
add()
update()
delete()
OUTPUT:
rollno=1
name=aryan
class=12
rollno=2
name=tej
class=12
rollno=3
name=heet
class=12
rollno=4
name=aayush
class12
enter4
PROGRAM -20
Aim:
# A Program to display the records of the products if the price is more
than 1000.
PROGRAM CODING:
import mysql.connector
con=mysql.connector.connect(host="localhost" , user="root" ,
password="jirs" ,charset="utf8", database="productdetails");
cur=con.cursor()
def adddata():
for i in range(0,3):
productid=int(input("enter pid="))
productname=input("enter pname=")
price=int(input("price=$"))
quantity=int(input("qty="))
dop=input("date of purchase=")
t=(productid,productname,price,quantity,dop)
cur.execute(q,t)
con.commit()
def display():
cur.execute(q)
for i in cur:
print(i)
adddata()
display()
OUTPUT:
(9855, 'masala lays', 10.0, 7, datetime.date(2024, 5, 3))
(874984, 'dark green lays', 10.0, 4, datetime.date(2024, 2, 4))
(8230990, 'yelllow lays ', 10.0, 2, datetime.date(2024, 5, 7))
(78045, 'dehg', 8.0, 7, datetime.date(2024, 8, 9))
(843670, '740', 9.0, 74, datetime.date(2024, 9, 8))
(6343, '874', 7.0, 8, datetime.date(2024, 5, 8))
(436, 'nutella', 300.0, 6, datetime.date(2024, 7, 12))
(346749, 'kinderjoy', 76.0, 9, datetime.date(2024, 9, 3))
(7480879, 'mousse', 150.0, 4, datetime.date(2024, 7, 5))
(163493, 'oreo silk', 150.0, 5, datetime.date(2024, 6, 4))
(763492, 'oreo silk', 150.0, 3, datetime.date(2024, 7, 5))
(68432, 'oreo silk', 150.0, 5, datetime.date(2024, 8, 9))
(4267803, 'oreo silk', 150.0, 3, datetime.date(2024, 7, 8))
(7303, 'silk', 24.0, 9, datetime.date(2024, 5, 6))
(238648, 'ferero rocher', 500.0, 1, datetime.date(2024, 7, 7))
(630, 'blue lays', 10.0, 6, datetime.date(2024, 9, 13))
(872, 'kinder joy', 36.0, 4, datetime.date(2024, 3, 2))
(730, 'scezwan', 150.0, 2, datetime.date(2024, 7, 26))
MYSQL-1:
Considering the table bank given below write queries for the
following:
questions:
1.To display the details about all the
Output:
2. To display the Account number and
FD amount of all the Fixed Deposits that
are started before
01-04-2018.
Ans: . Select Accno, FD_amount from Fixed_Deposits where FD_Date < “01-04-2018”;
Output:
Output:
4. To display the customer name and FD
Amount of all the Fixed deposits for
which the FD amount is less than
500000 and int_rate is more than 7.
Output:
Output:
6.To display the details of all the Fixed Deposits whose FD Amount is in the range 40000-
50000.
Ans: Select * from Fixed_Deposits where FD_Amount between 40000 and 50000;
Output:
7.To display the Customer name and FD amount for all the
loans for which the number of months is 24,36 or 48(using in
operator).
Ans: . Select CUST_Name, FFD_Amount from Fixed_Deposits where Months in (24,36,48);
Output:
8. To display the Account number, customer name and
fd_amount for all the FDs for which the customer name ends
with “Sharma”;
Ans: 8. Select ACCNo, CUST_Name, FD_amount from Fixed_Deposits where CUST_Name like
“%Sharma”;
Output:
Output:
12.To display the details about all the FDs whose customer
name doesn’t contain the character “a”.
Ans: . Select * from Fixed_Deposits where CUST_Name not like “%a%”;
Output:
13.To update the Interest rate of all the bank Customers from
6.75 to 6.80
Ans: Update Fixed_Deposits set Int_Rate 6.80 where Int_Rate=6.75;
Output:
MYSQL-2:
AIM :
OUTPUT:
OUTPUT:
OUTPUT:
6. To display the coach names, sports , pay and date_of_app of all the coaches whosename ends with
“n”.
ANS: Select coachname, sports, pay, date_of_app from club where coachname like '%n';
OUTPUT:
7. To display the coach name, sports , age and pay of all the coaches whose pay is in
ANS: Select coachname, sports, age, pay from club where pay between 2000 and 2500;
OUTPUT:
8. To display the details about all the coaches who are above 30 in age and coach a
ANS: Select * from club where age > 30 and sports like 's%';
OUTPUT:
OUTPUT:
10. To display the average Pay given for a coach for every sports activity.
OUTPUT:
11. To display the details about all the male coaches who joined after 15th February
ANS: Select * from club where sex = "m" and date_of_app > '2022-02-15';
Output:
12. To display the coach id , names and age of all the coaches who coach neither
Ans: Select coach_id, coachname, age from club where sports not in ("swimming", "basketball");
Output:
13. To display the names of all the sports in the club which have more than 2 coaches.
Ans: Select sports from club group by sports having count(*) > 2;
Output:
14. To display the total salary given for male and female coaches in the club.
Output:
MYSQL-3:
1. To show all the information about the patients of the cardiology department.
OUTPUT:
2. To list the names of female patients who are either in the orthopaedic or surgery
department.
ANS: Select name from patients where sex = 'f' and (department = 'orthopaedic' or department =
'surgery');
OUTPUT:
ANS:
OUTPUT:
4. To display the number of patients in each department.
OUTPUT:
5. To display details of all the patients whose name’s second character is “a”.
OUTPUT:
6. To display the details of all the patients who was admitted in the year 2019.
OUTPUT:
7. To display the details about all the patients in the reverse alphabetical order of their names.
Output:
Output.
Output:
10. To display the names of the patients who are charged in the range 300 and 400 (both inclusive).
Ans: SELECT name FROM patients WHERE charges BETWEEN 300 AND 400;
Output:
11. To display the number of patients who are aged above 30.
Ans: SELECT COUNT(*) AS num_patients FROM patients WHERE age > 30;
Output:
12. To display the names of the department and the number of patients in the department that
have more than one patient.
Output
13. To delete the record of the patient "Kush".
Output:
14. To decrease the charges by 5% of all the patients admitted to the ENT department.
Ans: UPDATE patients SET charges = charges * 0.95 WHERE department = 'ent';
Output:
15. To add another column DOCNAME of datatype varchar(20) into the table hospital.
Output:
MYSQL-4:
1. Query:
To display the natural join of the tables items and traders.
ANS: Select * from items natural join traders;
OUTPUT:
2. Query:
To display the number of items traded by every Trader.
ANS: Select tname, count(*) as num_items from items natural join
traders group by tname;
OUTPUT:
3. Query:
To display the Item name, company, trader name, and city of all the
items.
ANS: Select iname, company, tname, city from items natural join
traders;
OUTPUT:
4. Query:
To display the Item name and Trader name of all the items that are
either from Delhi or Mumbai.
ANS: Select iname, tname from items natural join traders where city in
('delhi', 'mumbai');
OUTPUT:
5. Query:
To display the minimum and maximum price of the item traded by
every trader.
ANS: Select tname, min(price) as min_price, max(price) as max_price
from items natural join traders group by tname;
OUTPUT:
6. Query:
To display the average price of an item traded by every trader.
ANS: Select tname, avg(price) as avg_price from items natural join
traders group by tname;
OUTPUT:
7. Query:
To display the Item name and trader name of all the items in the
alphabetical order of the item names.
ANS: Select iname, tname from items natural join traders order by
iname asc;
OUTPUT:
8. Query:
To display the names of the traders whose average price of an item is
not more than 20000.
ANS: Select tname from traders natural join items where price <
20000;
OUTPUT:
9. Query:
To display the details about all the items whose price is in the range
between 10000 and 30000.
ANS: Select * from items natural join traders where price between
10000 and 30000;
OUTPUT:
10. Query:
To display the total number of quantity of items available for every
trader.
ANS: Select tname, sum(qty) as total_qty from items natural join
traders group by tname;
OUTPUT:
MYSQL-5:
TABLE:DEPT
Query 2:
Q2. To display the details about all the workers whose Wno is either
1003, 1005, or 1007.
ANS: Select * from workers natural join dept where wno in (1003,
1005, 1007);
OUTPUT:
Query 3:
Q3. To display the names and date of joining of all the workers in
Kolkata.
ANS: Select name, doj from workers natural join dept where city like
"Kolkata";
OUTPUT:
Query 4:
Q4. To display the number of male and female workers.
ANS: Select count(gender) "no.of workers", gender from workers
group by gender;
OUTPUT:
Query 5:
Q5. To display Wno, name, gender, department in descending order of
Wno.
ANS: Select wno, name, gender, department from workers natural join
dept order by wno desc;
OUTPUT:
Query 6:
Q6. To display the names of all the departments where more than one
worker is available.
ANS: Select department from workers natural join dept group by
dcode having count(wno) > 1;
OUTPUT:
Query 7:
Q7. To display Wno, name, department of all the workers who are born
between “1987-01-01” and “1991-12-01”.
ANS: Select wno, name, department from workers natural join dept
where dob >= "1987-01-01" and dob <= "1991-12-01";
OUTPUT:
Query 8:
Q8. To display Wno, name, department, city of all the workers whose
name contains the letter “s” and who are not in Mumbai and Delhi.
ANS: Select wno, name, department, city from workers natural join
dept where name like "%s" and city not in ("Mumbai", "Delhi");
OUTPUT:
Query 9:
Q9. To count and display the number of male workers who have joined
after “1986-01-01”.
ANS: Select count(wno) from workers where doj >= "1986-01-01" and
gender like "male";
OUTPUT:
Query 10:
Q10. To display the details of all the male workers in the finance
department.
ANS: Select * from workers natural join dept where gender like "male"
and department like "finance";
OUTPUT: