CS File
CS File
HAPPY SCHOOL
Computer Science
Practical File
(Code - 083)
Name: Sarthak Gupta Roll
number: 12441
Submitted to: Ms. Ritu Pandey
INDEX
S.NO TOPIC REMARKS
1. WAP to find sum of first n natural numbers.
10. WAP to create a user defined function which gives even numbers in the list.
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”.
27. WAP to check whether the database has been created or not.
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
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
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
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
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)
OUTPUT
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
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
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)
OUTPUT
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
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
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
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
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
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
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
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
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()
OUTPUT
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()
OUTPUT
CSV FILE
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
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)
OUTPUT
OUTPUT:
OUTPUT
CODE
SELECT*
FROM student;
OUTPUT
CODE
ALTER TABLE student
ADD(GenderVARCHAR(0)
);
OUTPUT
CODE
UPDATE student
SET
Gender=’Female’
WHERE
Name=’Anu’;
OUTPUT
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
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
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')
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