Professional Documents
Culture Documents
CSC Lab 2023-2024
CSC Lab 2023-2024
XII-COMPUTER SCIENCE(083)
LIST OF RECORD PROGRAMS
Program-1
Aim:
Source Code:
def LINEAR(a,key):
for i in a:
if i==key:
print(key,"found at location",a.index(key))
break
else:
LINEAR(a,key)Sample Output:
Program-2
Aim:
A python program to swap first half of the list with second half in a list
Source Code:
def SWAP(a):
def SWAP(a):
if len(a)%2==0:
j=len(a)//2
else:
j=len(a)//2+1
i=0
while i<len(a)//2:
a[i],a[j]=a[j],a[i]
i+=1
j+=1
SWAP(a)Sample Output:
Program-3
Aim:
Source Code:
def Largest(a):
lar=a[0]
for i in a[1:]:
if i>lar:
lar=i
print("Largest is ",lar)
a=eval(input("Enter a tuple"))
Largest(a)
Sample Output:
Program-4
Aim:
Source Code:
def UPDATE(d):
d[k]=v
d={}
for i in range(n):
d[key]=value
UPDATE( d)
Sample Output:
Program-5
Aim:
Source Code:
def PALINDROME(n):
rev=0
i=n
while i>0:
rev=rev*10+(i%10)
i=i//10
if n==rev:
print(n,"is palindrome")
else:
n=int(input("Enter a number"))
PALINDROME(n)
Sample Output:
Program-6
Aim:
Source Code:
def COUNT(a):
c=0
for i in a:
if i in'aeiouAEIOU':
c+=1
a=input("Enter a string")
COUNT(a)Sample Output:
Program-7
Aim:
Source Code:
f1=open("file1.txt",'r')
f2=open("file2.txt",'w')
found=0
data=f1.read()
for i in data:
found=1
f2.write(i)
if found==1:
print("Writing has done")
else:
f1.close()
f2.close()
Sample Output:
Program-8
Aim:
A python program to print all the five letter words from text file File1.txt
f=open("file1.txt",'r')
found=0
data=f.read()
data=data.split()
for i in data:
if len(i)==5:
found=1
print(i)
if found==1:
print("Above are the five letter word(s) present in the text file")
else:
print("No five letter words are available to display")
f.close()
Sample Output:
Program-9
Aim:
A python program to display all the lines starting with ‘I’ from text file File1.txt
Source Code:
f=open("file1.txt",'r')
data=f.readlines()
found=0
for i in data:
if i[0]=='I':
found=1
print(i,end='')
if found==1:
else:
f.close()
Sample Output:
Program-10
Aim:
Source Code:
import pickle
f=open("Student.dat",'wb+')
rec=[]
for i in range(n):
rec=[rno,name,marks]
pickle.dump(rec,f)
f.seek(0)
try:
while True:
rec=pickle.load(f)
print(rec)
except EOFError:
f.close()
Sample Output:
Program-11
Aim:
Source Code:
import pickle
f=open("Student.dat",'wb+')
rec=[]
for i in range(n):
rec=[rno,name,marks]
pickle.dump(rec,f)
f.seek(0)
try:
while True:
rec=pickle.load(f)
if r in rec:
print(rec)
except EOFError:
f.close()
Sample Output:
Program-12
Aim:
Source Code:
a=[]
def PUSH():
for i in range(n):
a.append(ele)
def POP():
if len(a)==0:
print("Underflow")
else:
def DISPLAY():
if len(a)==0:
print("Underflow")
else:
print("Elements are",a[::-1])
PUSH()
POP()
DISPLAY()
Sample Output:
Program-13
Aim:
Source Code:
for i in range(2,n):
if n%i==0:
break
else:
A python program to generate a random number between an upper limit and lower limit
Source Code:
import random
print(random.randint(l,u))
Sample Output:
Program-15
Aim:
Source Code:
import csv
student=[(1,'Lata',450),(2,'Anil',496),(3,'John',390)]
f=open('students.csv','a', newline='')
obj=csv.writer(f)
obj.writerow(stud)
f.close()
f=open('students.csv','r', newline='')
obj=csv.reader(f)
for i in obj:
print(i)
f.close()
f=open('students.csv','r', newline='')
obj=csv.reader(f)
if n in stud:
print (stud)
break
else:
f.close()Sample Output:
16. CREATE AND DISPLAY A TABLE
Aim:
To write a Python-SQL connectivity program to create a database 'EMPLOYEE' , table 'DATA'
using below information and display it.
EMPNO EMPNAME SALARY EXPERIENCE
1 ‘Ram’ 25000 5
2 'Rohith' 20000 4
3 'Ann' 35000 7
4 'Aman' 15000 2
5 'Ranjini' 26000 6
Source Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="admin")
mycursor=mydb.cursor()
mycursor.execute("CREATE DATABASE IF NOT EXISTS EMPLOYEE")
mycursor.execute("USE EMPLOYEE")
mycursor.execute("CREATE TABLE IF NOT EXISTS DATA (EMPNO INT,EMPNAME
CHAR(10),SALARY FLOAT,EXPERIENCE INT)")
mycursor.execute("INSERT INTO DATA VALUES(1,'Ram',25000,5)")
mycursor.execute("INSERT INTO DATA VALUES(2,'Rohith',20000,4)")
mycursor.execute("INSERT INTO DATA VALUES(3,'Ann',35000,7)")
mycursor.execute("INSERT INTO DATA VALUES(4,'Aman',15000,2)")
mycursor.execute("INSERT INTO DATA VALUES(5,'Ranjini',26000,6)")
mycursor.execute("SELECT * FROM DATA")
for i in mycursor:
print(i)
Sample Output:
1 ‘Ram’ 25000 5
2 'Rohith' 20000 4
3 'Ann' 35000 7
4 'Aman' 15000 2
5 'Ranjini' 26000 6
Source Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="admin")
mycursor=mydb.cursor()
mycursor.execute("CREATE DATABASE IF NOT EXISTS EMPLOYEE")
mycursor.execute("USE EMPLOYEE")
mycursor.execute("CREATE TABLE IF NOT EXISTS DATA (EMPNO INT,EMPNAME
CHAR(10),SALARY FLOAT,EXPERIENCE INT)")
mycursor.execute("INSERT INTO DATA VALUES(1,'Ram',25000,5)")
mycursor.execute("INSERT INTO DATA VALUES(2,'Rohith',20000,4)")
mycursor.execute("INSERT INTO DATA VALUES(3,'Ann',35000,7)")
mycursor.execute("INSERT INTO DATA VALUES(4,'Aman',15000,2)")
mycursor.execute("INSERT INTO DATA VALUES(5,'Ranjini',26000,6)")
mycursor.execute("SELECT * FROM DATA where EMPNO=2")
for i in mycursor:
print(i)
Sample Output:
1 ‘Ram’ 25000 5
2 'Rohith' 20000 4
3 'Ann' 35000 7
4 'Aman' 15000 2
5 'Ranjini' 26000 6
Source Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="admin")
mycursor=mydb.cursor()
mycursor.execute("CREATE DATABASE IF NOT EXISTS EMPLOYEE")
mycursor.execute("USE EMPLOYEE")
mycursor.execute("CREATE TABLE IF NOT EXISTS DATA (EMPNO INT,EMPNAME
CHAR(10),SALARY FLOAT,EXPERIENCE INT)")
mycursor.execute("INSERT INTO DATA VALUES(1,'Ram',25000,5)")
mycursor.execute("INSERT INTO DATA VALUES(2,'Rohith',20000,4)")
mycursor.execute("INSERT INTO DATA VALUES(3,'Ann',35000,7)")
mycursor.execute("INSERT INTO DATA VALUES(4,'Aman',15000,2)")
mycursor.execute("INSERT INTO DATA VALUES(5,'Ranjini',26000,6)")
mycursor.execute("SELECT * FROM DATA")
for i in mycursor:
print(i)
mycursor.execute("UPDATE DATA SET SALARY =SALARY+1000")
mycursor.execute("SELECT * FROM DATA")
for i in mycursor:
print(i)
Sample Output:
Before Updation:
After Updation:
1 ‘Ram’ 25000 5
2 'Rohith' 20000 4
3 'Ann' 35000 7
4 'Aman' 15000 2
5 'Ranjini' 26000 6
Source Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="admin")
mycursor=mydb.cursor()
mycursor.execute("CREATE DATABASE IF NOT EXISTS EMPLOYEE")
mycursor.execute("USE EMPLOYEE")
mycursor.execute("CREATE TABLE IF NOT EXISTS DATA (EMPNO
INT,EMPNAME CHAR(10),SALARY FLOAT,EXPERIENCE INT)")
mycursor.execute("INSERT INTO DATA VALUES(1,'Ram',25000,5)")
mycursor.execute("INSERT INTO DATA VALUES(2,'Rohith',20000,4)")
mycursor.execute("INSERT INTO DATA VALUES(3,'Ann',35000,7)")
TABLE: EMPLOYEE
NO NAME SALARY ZONE AGE GRADE DEPT
1 MUKUL 30000 WEST 28 A 10
2 KRITIKA 35000 CENTRE 30 A 10
3 NAVEEN 32000 WEST 40 NULL 20
4 UDAY 38000 NORTH 38 C 30
5 NUPUR 32000 EAST 26 NULL 20
6 MOKSH 37000 SOUTH 28 B 10
7 SHELLY 36000 NORTH 26 A 30
ANSWERS
a) SELECT NAME FROM EMPLOYEE WHERE ZONE=’NORTH’;
b) SELECT * FROM EMPLOYEE ORDER BY SALARY;
c) SELECT NAME,SALARY,AGE FROM EMPLOYEE WHERE NAME LIKE ‘%A%’;
d) SELECT MAX(SALARY),MIN(SALARY) FROM EMPLOYEE;
e) SELECT DISTINCT(ZONE) FROM EMPLOYEE;
SQL NO 21: ( Programm No.)
Write the SQL queries for the given statements from a to e based on the following table
EMPLOYEE
TABLE: EMPLOYEE
NO NAME SALARY ZONE AGE GRADE DEPT
1 MUKUL 30000 WEST 28 A 10
2 KRITIKA 35000 CENTRE 30 A 10
3 NAVEEN 32000 WEST 40 NULL 20
4 UDAY 38000 NORTH 38 C 30
5 NUPUR 32000 EAST 26 NULL 20
6 MOKSH 37000 SOUTH 28 B 10
7 SHELLY 36000 NORTH 26 A 30
a) Display the details of all the employees from the EMPLOYEE table
b) Display the names of all the employees in the descending order of their age
c) Display the name, salary, and age of all the employees having A grade from EMPLOYEE table
d) Display the total number of employees from each zone from the EMPLOYEE table
e) Display the names of all employees who are not assigned with grade from the table EMPLOYEE
ANSWERS
a) SELECT * FROM EMPLOYEE ;
b) SELECT NAME FROM EMPLOYEE ORDER BY AGE DESC;
c) SELECT NAME,SALARY,AGE FROM EMPLOYEE WHERE GRADE =’A’;
d) SELECT ZONE,COUNT(*) FROM EMPLOYEE GROUP BY ZONE;
e) SELECT NAME FROM EMPLOYEE WHERE GRADE IS NULL;
SQL NO 22: ( Programm No.)
Write the SQL queries for the given statements from a to e based on the following tables
EMPLOYEE AND DEPARTMENT
TABLE: EMPLOYEE
NO NAME SALARY ZONE AGE GRADE DEPT
1 MUKUL 30000 WEST 28 A 10
2 KRITIKA 35000 CENTRE 30 A 10
3 NAVEEN 32000 WEST 40 NULL 20
4 UDAY 38000 NORTH 38 C 30
5 NUPUR 32000 EAST 26 NULL 20
6 MOKSH 37000 SOUTH 28 B 10
7 SHELLY 36000 NORTH 26 A 30
TABLE: DEPARTMENT
DEPT DNAME MINSAL MAXSAL HOD
10 SALES 25000 32000 1
20 FINANCE 30000 50000 5
30 ADMIN 25000 40000 7
a) Display the name and department of all employees from the EMPLOYEE and DEPARTMENT
table.
b) Display the number of employees who are more than 30 years old from the EMPLOYEE table.
c) Display the name, salary, and age of all the employees whose name starts with ’M’.
d) Display the structure of the table EMPLOYEE
e) Increase the salary of all employees by 2000
ANSWERS
a) SELECT NAME,DNAME FROM EMPLOYEE E,DEPARTMENT D WHERE
E.DEPT=D.DEPT;
b) SELECT COUNT(*) FROM EMPLOYEE WHERE AGE>30;
c) SELECT NAME,SALARY,AGE FROM EMPLOYEE WHERE NAME LIKE ‘M%’;
d) DESC EMPLOYEE;
e) UPDATE EMPLOYEE SET SALARY =SALARY+2000 ;
SQL NO 23: ( Programm No.)
Write the SQL queries for the given statements from a to e based on the following tables
EMPLOYEE AND DEPARTMENT
TABL : EMPLO EE
NO NAME SALARY ZONE AGE GRADE DEPT
1 MUKUL 30000 WEST 28 A 10
2 KRITIKA 35000 CENTRE 30 A 10
3 NAVEEN 32000 WEST 40 NULL 20
4 UDAY 38000 NORTH 38 C 30
5 NUPUR 32000 EAST 26 NULL 20
6 MOKSH 37000 SOUTH 28 B 10
7 SHELLY 36000 NORTH 26 A 30
TABLE: DEPARTMENT
DEPT DNAME MINSAL MAXSAL HOD
10 SALES 25000 32000 1
20 FINANCE 30000 50000 5
30 ADMIN 25000 40000 7
a) Display the name ,minimum salary and maximum salary of all employees from the
EMPLOYEE and DEPARTMENT table.
b) Display the number of employees where the grade is assigned.
c) Display the name, salary, and age of all the employees who are from WEST zone.
d) Display the average of all salaries from EMPLOYEE table.
e) Insert the following row into the DEPARTMENT table
(40,’HR’,35000,45000,8)
ANSWERS
a) SELECT NAME,MINSAL,MAXSAL FROM EMPLOYEE E,DEPARTMENT DWHERE
E.DEPT=D.DEPT;
b) SELECT COUNT(*) FROM EMPLOYEE WHERE GRADE IS NOT NULL;;
c) SELECT NAME,SALARY,AGE FROM EMPLOYEE WHERE ZONE=’WEST’;
d) SELECT AVG(SALARY) FROM EMPLOYEE ;
e) INSERT INTO DEPARTMENT VALUES(40,’HR’,35000,45000,8);
SQL NO 24: ( Programm No.)
Write the SQL queries for the given statements from a to e based on the following tables
EMPLOYEE
TABLE: EMPLOYEE