HAPPY SCHOOL/COMPUTER SCIENCE
HAPPY SCHOOL
Computer Science
Practical File
(Code - 083)
Name: Sarthak Gupta Roll
number: 12441
Submitted to: Ms. Ritu Pandey
TERM-2 Teacher’s Signature
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
INDEX
S.NO TOPIC REMARKS
1. WAP to find sum of first n natural numbers.
2. WAP to check whether the year is a leap year or not.
3. WAP to calculate commission.
4. WAP to calculate Simple Interest.
5. WAP to bubble sort (ascending order).
6. WAP to sort using insertion sort (ascending order).
7. WAP to implement linear search.
8. WAP to binary search (ascending order).
9. WAP to calculate area of a circle.
10. WAP to create a user defined function which gives even numbers in the list.
11. Write a user defined function to swap two numbers.
12. WAP which takes a list and returns a list containing squares of each number.
13. WAP to display 2nd and 4th line from file “test.txt”.
14. WAP to enter student records.
15. WAP to count number of lines starting with letter "s".
16. WAP to create a file, store values and read it.
17. WAP to update records in a binary file.
18. WAP to create CSV file for student records.
19. WAP to pop using user defined function.
20. WAP to show stack implementation.
21. WSC to create table “STUDENT”.
22. WSC to add records in table STUDENT.
23. WSC to display records of table STUDENT.
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
24. WSC add gender column.
25. Update Gender column.
26. WSC to display the record of students whose marks is b/w 80-100.
27. WAP to check whether the database has been created or not.
28. WAP to insert 5 records in student table through python
29.
WAP to create a table students using python script mode.
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
1. WAP to find sum of first n natural numbers.
CODE
def sum(n) :
x=0
for i in range(1,n+1) :
x += i
return(x)
n = int(input("enter the number : "))
print("sum : ",sum(n))
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
2. WAP to check whether the year is a leap year or not.
CODE
a = int(input("enter the year : "))
if a%100 == 0 :
if a%400 == 0 :
print("this is a leap year")
else :
print("not a leap year")
elif a%4 == 0 :
print("this is a leap year")
else :
print("not a leap year")
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
3. WAP to calculate commission.
CODE
s = float(input("enter your salary : "))
a = float(input("enter your sales : "))
if 15000>a>=10000 :
c = a*0.1
elif a>=15000 :
c = a*0.2
else :
c=0
t = s+c
print("commission is :" , c)
print("salary is : " , t)
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
4. WAP to calculate Simple Interest.
CODE
def s(p,r,t) :
k = (p*r*t)/100
return k
p = int(input('enter the principal amount : '))
r = int(input('enter the rate of interest : '))
t = int(input('enter the time (in years) : '))
print("simple interest is : ",s(p,r,t))
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
5. WAP to bubble sort (ascending order).
CODE
def sort(l) :
for i in range(len(l)-1,0,-1) :
for j in range(i) :
if l[j]>l[j+1] :
t = l[j]
l[j] = l[j+1]
l[j+1] = t
return(l)
l = eval(input("enter the list : "))
print(“sorted list is : ” , sort(l))
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
6. WAP to sort using insertion sort (ascending order).
CODE
def sort(l) :
for i in range(1,len(l)) :
temp = l[i]
j=i-1
while l[j] > temp and j >= 0 :
l[j+1] = l[j]
j = j-1
l[j+1] = temp
return(l)
a = eval(input("enter the list : "))
b = sort(a)
print(“sorted list is : ” , b)
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
7. WAP to implement linear search.
CODE
def linear(l,k) :
r=0
for i in range(len(l)) :
if l[i] == k :
r=i+1
break
return(r)
p = eval(input("enter the list : "))
s = int(input("enter the element to be searched : "))
h = linear(p,s)
if h == 0 :
print("element not found")
else :
print("element found at position : ",h)
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
8. WAP to binary search (ascending order).
CODE
def binary(l,n) :
pos = 0
first = 0
last = len(l)-1
while first <= last :
mid = (first+last)//2
if l[mid] == n :
pos = mid + 1
break
elif l[mid] < n :
first = mid + 1
else :
last = mid - 1
return(pos)
l = eval(input("enter the list in ascending order : "))
n = int(input("enter the number to be searched : "))
k = binary(l,n)
if k == 0 :
print("search unsuccessful")
else :
print("number found at position : ",k)
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
9. WAP to calculate area of a circle.
CODE
def a(n) :
p = 3.14
k = p*(n**2)
return k
d = float(input("enter the radius (in meters) : "))
print("area is : ", a(d))
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
10. WAP to create a user defined function which gives even
numbers in the list.
CODE
def even(l) :
d = []
for i in l :
if i%2 == 0 :
d.append(i)
else :
continue
return(d)
l = eval(input("enter the list : "))
k = even(l)
print("even numbers are : " , k)
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
11. Write a user defined function to swap two numbers.
CODE
def w(a,b) :
a,b = b,a
print("swapped values are a : ",a,"b : ",b)
a = int(input("enter a : "))
b = int(input("enter b : "))
print("original values are a : ",a,"b : ",b)
w(a,b)
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
12. WAP which takes a list and returns a list containing
squares of each number.
CODE
def square(l) :
l1 = []
for i in l :
l1.append(i**2)
return(l1)
a = eval(input("enter the list : "))
p = square(a)
print("original list : ",a)
print("updated list : ",p)
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
13. WAP to display 2nd and 4th line from file “test.txt”.
CODE
f = open("test.txt",'r')
data = f.readlines()
print("second line : ",data[1])
print("fourth line : ",data[3])
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
14. WAP to enter student records.
CODE
f = open('stud.txt','a')
n = int(input("enter the number of records to be entered : "))
i=1
while i <= n :
R = input("enter roll number : ")
N = input("enter name : ")
M = input("enter marks : ")
L = [R,N,M]
f.writelines(L)
i+=1
f.close()
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
15. WAP to count number of lines starting with letter "s".
CODE
f = open("stud.txt",'r')
k = f.readlines()
c=0
for i in k :
if i.startswith("s") :
c+=1
print(c)
f.close()
TEXT FILE
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
16. WAP to create a file, store values and read it.
CODE
import pickle
l = eval(input("enter a list : "))
f = open("list.dat",'wb')
pickle.dump(l,f)
print("list added")
f.close()
q = open("list.dat",'rb')
k = pickle.load(q)
print("content of file : ", k)
f.close()
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
17. WAP to update records in a binary file.
CODE
import pickle
f = open("student.dat",'rb+')
rec = pickle.load(f)
found = 0
r = int(input("enter roll number to be updated : "))
for i in rec :
if i[0] == r :
print("current marks are : ",i[2])
i[2] = int(input("enter new marks : "))
found = 1
break
if found == 1 :
f.seek(0)
pickle.dump(rec,f)
print("marks updated")
else :
print("record not found")
f.close()
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
18. WAP to create CSV file for student records.
CODE
import csv
f = open("student.csv",'w', newline= "")
ob = csv.writer(f,delimiter=',')
heading = ['roll-number','name','marks','DOB']
data = []
while True :
r = int(input("enter roll number : "))
n = input("enter name : ")
m = int(input("enter marks : "))
d = input("enter DOB : ")
g = [r,n,m,d]
data.append(g)
ans = input("do you want to enter more records ? (Y/N) : ")
if ans == 'N' or ans == 'n' :
break
ob.writerow(heading)
for row in data :
ob.writerow(row)
f.close()
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
OUTPUT
CSV FILE
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
19. WAP to pop using user defined function.
CODE
def pop(arr,n) :
if arr == [] :
print("stack underflow")
else :
x = arr.pop(n)
return(x)
a = eval(input("enter the list : "))
n = int(input("enter the index : "))
z = pop(a,n)
print("element popped is : ", z)
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
20. WAP to show stack implementation.
CODE
s = [1,2,34,56,76]
c = 'y'
while c == 'y' :
print("\n1.PUSH \n2.POP \n3.DISPLAY \n4.EXIT")
k = int(input("enter your choice : "))
if k == 1 :
a = input("enter element : ")
s.append(a)
elif k == 2 :
if s == [] :
print("empty stack")
else :
print("deleted item : ",s.pop())
elif k == 3 :
l = len(s)
for i in range(l-1,-1,-1) :
print(s[i])
elif k == 4 :
break
else :
print("wrong input")
print(s)
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
21 . Write Sql command to create a table student.
CODE
CREATE TABLE
student (
Roll_No Integer Not Null Primary
key,Name Varchar(20) NOT NULL,
Marks
Integer(10),
DOB date
);
OUTPUT:
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
22. WSC to insert record in student table.
CODE
INSERT INTO student
VALUES(1,’ANU’,89,’2009-11-09’);
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
23. WSC to display records of students.
CODE
SELECT*
FROM student;
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
24. WSC add gender column.
CODE
ALTER TABLE student
ADD(GenderVARCHAR(0)
);
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
25. WSC add gender column.
CODE
UPDATE student
SET
Gender=’Female’
WHERE
Name=’Anu’;
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
26. WSC to display the record of students whose marks is b/w 80-100.
CODE
select*
FROM student
WHERE Marks Between 80 and 100;
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
27. WAP to check whether the database has been created or not.
CODE
import mysql.connector
con=mysql.connector.connect(host="localhost",user="root",password
="3268")
cur=con.cursor()
cur.execute("SHOW
DATABASES")for x in cur:
print(x)
OUTPUT
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
28. WAP to insert 5 records in student table through python
interface.
CODE
import mysql.connector as s
con=s.connect(host="localhost",user="root",password="3268",databa
se="python")
cur=con.cursor()
cur.execute("INSERT INTO
student(Rollno,age,Name,DOB,Marks)
VALUES(1,16,'Amit','2006-01-02',89)")
cur.execute("INSERT INTO
student(Rollno,age,Name,DOB,Marks)
VALUES(2,16,'Raghav','2006-05-23',67)")
cur.execute("INSERT INTO
student(Rollno,age,Name,DOB,Marks)
VALUES(3,17,'Khsuhi','2005-09-09',89)")
print('Data added')
SARTHAK GUPTA XII-S 12441
HAPPY SCHOOL/COMPUTER SCIENCE
29. WAP to create a table students using python script mode.
CODE
import mysql.connector as s
con=s.connect(host="localhost",user="root",password="3268",databa
se="python")
cur=con.cursor()
cur.execute("CREATE TABLE students(Rollno integer(12),age
integer(5),Name varchar(30),DOB Date)")
OUTPUT
SARTHAK GUPTA XII-S 12441