Cs File
Cs File
Cs File
30
CS Practical File Session 2023-24 CLASS12
Table of Content:
S.No. PracticalName PageNo. Teacher’s
Sign
PYTHONPRACTICALS
1. WAP in Python to find the factorial of a number using
function.
2. WAP in Python to implement default and positional
parameters.
3. Write a program in Python to input the value of x and n and
print the sum of the following series
1+x+x^2+x^3+----------------x^n
4. WAP in Python to read a text file and print the number of
vowels and consonants in the file.
5. WAP in Python to read a text file and print the line or
Paragraph starting with the letter‘ S’
6. WAP in Python to read a text file and print the number of
Uppercase and lowercase letters in the file.
7. WAP in Python to create a binary file with name and roll
number of the students .Search for a given roll number and
Display the name of student.
8. Create a binary file withroll _no, name and marks of some
Students and update the marks of specific student.
9. Create a binary file with eid ,ename and salary and update
The salary of the employee.
10. Create a text file and remove the lines from the file which
Contains letter ‘K’
11. Createabinaryfilewith10randomnumbersfrom1to40 and print
those numbers.
12. Write a program in Python to create a CSV file with the
detailsof5students.
13. WAP in Python to read a CSV file.
14. Write a menu driven program which insert ,delete and display
the details of an employee such as eid ,ename and
Salary using Stack.
15. Write a menu driven program which insert, delete and
Display the details of a book such as book_id,book_name and
price using Stack.
16. Write a menu driven program which insert,delete and
Display the details of a student such as roll_no, sname and
MYSQL PRACTICALS
1. Write a SQL query to create a database.
14. Write a SQL query to display the student_id, name, dob, marks,
email of male students in ascending order of their name
15. Write a SQL query to display the student_id, gender, name, dob,
marks, email of students in descending order of their marks
16. Write a SQL query to display the unique section name from the
student table
17. Create a student table with student id, name and marks
as attribute, where the student id is the primary key
20. Use the Select command to get the details of the students with
marks more than 30
21. Shiva, a student of class XII, created a table “CLASS”. Grade is one
of the columns of this table. Write the SQL query to find the details
of students whose grade have not been entered.
22. Write the SQL query to find out the square root of 26
35. Write any one similarity and one difference between Primary key
and unique key constraint
36. Write the difference between char() and varchar()
def fact(n):
f=1
while(n>0):
f=f*n
n=n-1
return f
if(n<0):
elif(n==0):
print("Factorial of 0 is 1")
else:
factorial=fact(n)
OUTPUT:
Enter a number to find the factorial: 5
Factorial of 5 is = 120
>>>
Factorial of 0 is 1
>>>
>>>
#Default Parameter
def show(a,b,c=8):
print("Sum=",(a+b+c))
show(5,6)
show(5,6,10)
def show1(a,b=9,c=8):
print("Sum1=",(a+b+c))
show1(5)
show1(5,6)
show1(5,6,10)
#Positional Parameter
def show2(a,b):
print("Sub=",(a-b))
show2(15,6)
show2(6,15)
OUTPUT:
Sum= 19
Sum= 21
Sum1= 22
Sum1= 19
Sum1= 21
Sub= 9
Sub= -9
>>>
1+x+x^2+x^3+----------------x^n
print("Sum of series=",sum)
OUTPUT:
Enter the value of x: 4
Sum of series= 85
Practical No - 4: WAP in Python to read a text file and print the number of
vowels and consonants in the file.
f=open("Vowel.txt","r")
data=f.read()
V=['A','E','I','O','U','a','e','i','o','u']
C=['B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X', \
'Y','Z','b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w', \
'x','y','z']
cv=0
cc=0
for i in data:
if i in V:
cv=cv+1
elif i in C:
cc=cc+1
ct=0
for i in data:
ct=ct+1
print("Number of Vowels in the file=",cv)
print("Number of Consonants in the file=",cc)
print("Number of Total Chars in the file=",ct)
f.close()
OUTPUT:
Number of Vowels in the file= 184
Practical No - 5: WAP in Python to read a text file and print the line or paragraph
starting with the letter ‘S’
f=open("abc.txt","r")
line=f.readline()
lc=0
while line:
if line[0]=='s' or line[0]=='S':
print(line,end="")
lc=lc+1
line=f.readline()
f.close()
OUTPUT:
Sam
Sameer
Sanjay
Sunil
Practical No - 6: WAP in Python to read a text file and print the number of
uppercase and lowercase letters in the file.
f=open("Vowel.txt","r")
data=f.read()
U=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', \
'S','T','U','V','W','X','Y','Z',]
L=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r', \
's','t','u','v','w','x','y','z']
cu=0
cl=0
for i in
data: if i
in U:
cu=cu+1
elif i in L:
cl=cl+1
ct=0
for i in
data:
ct=ct+1
print("Number of Uppercase letters in the file=",cu)
print("Number of Lowercase Letters in the file=",cl)
print("Number of Total Chars in the file=",ct)
f.close()
'''
Practical No. 6: WAP in Python to read a text file and print the number
of uppercase and lowercase letters in the file
'''
f=open("Vowel.txt","r")
data=f.read()
cu=0
cl=0
for i in data:
if i.isupper():
cu=cu+1
elif i.islower():
cl=cl+1
ct=0
for i in
data:
ct=ct+1
print("Number of Uppercase letters in the file=",cu)
print("Number of Lowercase Letters in the file=",cl)
7 Ayush Jha GSBV BURARI
CS Practical File Session 2023-24 CLASS 12
f.close()
OUTPUT:
Number of Uppercase letters in the file= 139
Practical No - 7: WAP in Python to create a binary file with name and roll
number of the students. Search for a given roll number and display the name of
student.
import pickle
S={}
f=open('stud.dat','wb')
c='y'
S['RollNo']=rno
S['Name']=name
pickle.dump(S,f)
f.close()
f=open('stud.dat','rb')
K={}
m=0
try:
while True:
K=pickle.load(f)
if K["RollNo"] == rno:
print(K)
m=m+1
except EOFError:
f.close()
if m==0:
OUTPUT:
Enter the roll no. of the student : 1
Practical No - 8: Create a binary file with roll_no, name and marks of some
students and update the marks of specific student.
import pickle
S={}
f=open('stud.dat','wb')
c='y'
S['RollNo']=rno
S['Name']=name
S['Marks']=marks
pickle.dump(S,f)
f.close()
f=open('stud.dat','rb+')
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
S=pickle.load(f)
if S["RollNo"] == rno:
f.seek(pos)
S["Marks"]=marks
pickle.dump(S,f)
m=m+1
except EOFError:
f.close()
if m==0:
else:
f=open('stud.dat','rb')
try:
while True:
S=pickle.load(f)
print(S)
except EOFError:
f.close()
OUTPUT:
Practical No - 9: Create a binary file with eid, ename and salary and update the
salary of the employee.
import pickle
E={}
f=open('emp.dat','wb')
c='y'
E['Emp_Id']=eid
E['Emp_Name']=ename
E['Salary']=salary
pickle.dump(E,f)
f.close()
f=open('emp.dat','rb+')
f.seek(0,0)
m=0
try:
while True:
pos=f.tell()
E=pickle.load(f)
if E["Emp_Id"] == eid:
f.seek(pos)
E["Salary"]=salary
pickle.dump(E,f)
m=m+1
except EOFError:
f.close()
if m==0:
else:
f=open('emp.dat','rb')
try:
while True:
E=pickle.load(f)
print(E)
except EOFError:
f.close()
OUTPUT:
>>>
Practical No - 10: Create a text file and remove the lines from the file which
contains letter ‘K’
import sys
f=open("sps.txt","w+")
data = sys.stdin.readlines()
for i in data:
f.write(i)
f.close()
print("**********")
print("Content of File:
") f=open("sps.txt","r")
data=f.read()
print(data)
f.close()
f=open("sps.txt","r+")
data=f.readlines()
f.seek(0)
for i in data:
if "K" not in
i: f.write(i)
f.truncate()
f.close()
print("**********")
f=open("sps.txt","r")
data=f.read()
print(data)
OUTPUT:
HELLO INDIA
KARAN
SAM
RAM
KASHMIR
DELHI
**********
Content of File:
S P SHARMA CLASSES
HELLO INDIA
KARAN
SAM
RAM
KASHMIR
DELHI
**********
S P SHARMA CLASSES
HELLO INDIA
SAM
RAM
DELHI
>>>
Practical No - 11: Create a binary file with 10 random numbers from 1 to 40 and
print those numbers.
import pickle,random
N=[]
f=open("sps.txt","wb")
for i in range(10):
N.append(random.randint(1,40))
pickle.dump(N,f)
f.close()
print("File Created:")
print("Content of File:")
f=open("sps.txt","rb")
data=pickle.load(f)
for i in data:
print(i)
f.close()
OUTPUT:
File Created:
Content of File:
24
14
18
14
26
33
10
33
Practical No - 12: Write a program in Python to create a CSV file with the details
of 5 students.
import csv
f=open("student.csv","w",newline='')
cw=csv.writer(f)
cw.writerow(['Rollno','Name','Marks'])
for i in range(5):
sr=[rollno,name,marks]
cw.writerow(sr)
f.close()
OUTPUT:
Student Record of 1
Enter Marks: 34
Student Record of 2
Enter Marks: 37
Student Record of 3
Enter Marks: 24
Student Record of 4
Enter Marks: 40
Student Record of 5
Enter Marks: 37
>>>
import csv
f=open("student.csv","r")
cr=csv.reader(f)
for r in cr:
print(r)
f.close()
OUTPUT:
Content of CSV File:
>>>
Practical No - 14: Write a menu driven program which insert, delete and display
the details of an employee such as eid, ename and salary using Stack.
Employee=[]
c='y'
while(c=="y" or c=="Y"):
print("1: Add Employee Detail: ")
print("2: Delete Employee Detail: ")
print("3: Display Employee Detail: ")
choice=int(input("Enter your choice: "))
if(choice==1):
eid=int(input("Enter Employee Id: "))
ename=input("Enter Employee Name: ")
salary=float(input("Enter Employee Salary: "))
emp=(eid,ename,salary)
Employee.append(emp)
elif(choice==2):
if(Employee==[]):
print("Stack Empty")
else:
print("Deleted element is: ",Employee.pop())
elif(choice==3):
L=len(Employee)
while(L>0):
print(Employee[L-1])
L=L-1
else:
print("Wrong Input")
c=input("Do you want to continue? Press 'y' to Continue: ")
OUTPUT:
3: Display Employee
>>>
Practical No - 15: Write a menu driven program which insert, delete and display
the details of a book such as book_id, book_name and price using Stack.
Book=[]
c='y'
while(c=='y' or c=='Y'):
")
if(choice==1):
B=(book_id,book_name,price)
Book.append(B)
elif(choice==2):
if(Book==[]):
print("Stack Empty")
else:
",Book.pop()) elif(choice==3):
L=len(Book)
while(L>0):
print(Book [L-1])
L=L-1
else:
print("Wrong Input")
OUTPUT:
1: Add Book Details:
>>>
Student=[]
c='y'
while(c=='y' or c=='Y'):
if(choice==1):
Practical No - 17: Write a menu driven program which insert, delete and display
the details of a movie such as movie_id, mname and rating using Stack.
movie=[]
c='y'
while(c=='y' or c=='Y'):
if(Choice==1):
mov= (mid,mname,rating)
movie.append(mov)
elif(Choice==2):
if(movie==[]):
print("stack empty")
else:
elif(Choice==3):
L=len(movie)
while(L>0):
print(movie[L-1])
L=L-1
else:
print("wrong input")
OUTPUT:
1:Add Movie Details:
Practical No - 18: Write a menu driven program which insert, delete and display
the details of a product such as pid, pname and price using Stack.
Product=[ ]
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
Prd=(pid,pname,price)
Product.append(Prd)
elif(choice==2):
if(Product==[ ]):
print("Stack Empty")
else:
elif(choice==3):
L=len(Product)
while(L>0):
print(Product[L-1])
L=L-1
else:
print("Wrong input")
OUTPUT:
1: Insert Product Details:
3: Display Product
Details:
>>>
Practical No - 19: Write a menu driven program which insert, delete and display
the details of a club such as club_id, cname and city using Stack.
Club= []
c='y'
if (choice == 1):
clu = (cid,cname,city)
Club.append(clu)
elif (choice==2):
if(Club == []):
else:
L = len(Club)
while (L>0):
print(Club[L-1])
L=L-1
else:
print("Wrong Input")
OUTPUT:
39 Ayush Jha GSBV BURARI
CS Practical File Session 2023-24 CLASS 12
Enter choice: 1
A1
Enter choice: 1
3: Display Club
Enter choice: 3
Enter choice: 2
Enter choice: 3
>>>
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("use spsharmag")
mycursor.execute("create table if not exists book (bid int primary key,bname varchar(20),bprice
float(5,2))")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
con.commit()
elif(choice==2):
mycursor.execute("select * from
book") mybooks=mycursor.fetchall()
for x in
mybooks:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
44 Ayush Jha GSBV BURARI
CS Practical File Session 2023-24 CLASS 12
>>>
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("use spsharmag")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
(pid,pname,pprice)) con.commit()
elif(choice==2):
myproducts=mycursor.fetchall()
for x in
myproducts:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
600
>>>
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("use spsharmag")
mycursor.execute("create table if not exists club (cid int primary key, cname
varchar(20),city varchar(20))")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
con.commit()
elif(choice==2):
myclubs=mycursor.fetchall()
for x in myclubs:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
>>>
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("use spsharmag")
mycursor.execute("create table if not exists Student (sid int primary key, sname varchar(20),
course varchar(20))")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
")
con.commit()
elif(choice==2):
mycursor.execute("select * from
Student") mystudents=mycursor.fetchall()
for x in
mystudents:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
B.Tech.
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
(mid,mname,rating)) con.commit()
elif(choice==2):
mymovies=mycursor.fetchall()
for x in
mymovies:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
4.8
>>>
import mysql.connector
con=mysql.connector.connect(host="localhost",username="root",passwd="root")
mycursor=con.cursor()
mycursor.execute("create table if not exists Employee (eid int primary key, ename
varchar(20), salary float(8,2))")
c="y"
while(c=="y" or c=="Y"):
if(choice==1):
")
(eid,ename,salary)) con.commit()
elif(choice==2):
myemp=mycursor.fetchall()
for x in
myemp:
print(x)
elif(choice==3):
con.commit()
elif(choice==4):
con.commit()
elif(choice==5):
break
else:
print("Wrong Choice")
OUTPUT:
Salary: 123456
134567
>>>
Q – 1: Create a database.
Syntax:
Show databases;
Q – 2: To create a student table with the student id, class, section, gender,
name, dob, and marks as attributes where the student id is the primary key.
Ans:
Primary Key:
Primary key is a constraint of an attribute which cannot be NULL or duplicate.
Only one primary key is allowed in a table.
Syntax:
To delete the specific record from the table use delete command/query.
Syntax:
Note: If we don’t use where clause It delete all the records one by one from the
table.
Q – 5: To increase the marks by 5% for those students who are scoring marks
more than 30.
Update:
Update is a SQL query used to update/change/modify the record of a table.
Syntax
Q – 7: To display student_id, name and marks of those students who are scoring
marks more than 30.
Aggregate Function:
Q – 10: To add a new column email in the student table with appropriate data
type.
Q – 11: To add the email id’s of each student in the previously created email
column.
Q – 12: To display the information of all the students, whose name starts with
‘S’
Q – 13: To display the student_id, name, dob of those students who are born
between ‘2005-01-01’ and ‘2005-12-31’.
Q – 14: To display the student_id, name, dob, marks, email of male students in
ascending order of their name.
Q – 15: To display the student_id, gender, name, dob, marks, email of students
in descending order of their marks.
Q – 16: To display the unique section name from the student table.
Q – 17: Create a student table with student id, name and marks as attribute,
where the student id is the primary key.
Q –20: Use the Select command to get the details of the students with marks more than 30.
Q-21: Shiva, a student of class XII, created a table “CLASS”. Grade is one of the columns of
this table. Write the SQL query to find the details of students whose grade have not been
entered.
Q-22: Write the SQL query to find out the square root of 26
Q-23:Shiva is using a table with the following details: Students(Name, Class, Stream _id,
Stream _Name) Write the SQL query to display the names of students who have not been
assigned any stream or have been assigned Stream _Name that end with “computers”
Q-24: Write the difference between drop, delete and truncate command with example.
Drop:
Drop Is used to remove an object completely just like remove database ,table ,view ,index
etc.
Syntax:
Examples:
DELETE:
Delete is used to remove one or more rows from a table , view ,index etc. We can use where
clause with delete command .Delete remove one row at a time i.e. it remove rows one by
one . it will not delete columns or structure of table.
Syntax:
condition; Examples:
TRUNCATE:
Truncate is used to remove all the rowsn from a table,view,index, etc. we xan not use where
clauses with truncate command. Truncate remove all the rows at once,but it will not delete
structure of the table or any object.
Syntax:
condition; Examples:
Truncate table_name;
Q:25 Shiva is using a table employee. It has following details: Employee (Code, Name, Salary,
DeptCode). Write the SQL query to display maximum salary department wise.
Q-26: Write the SQL Query to display the difference of highest and lowest salary of each department
having maximum salary greater than 4000
Q-27: Write the SQL Query to increase 20% salary ofthe employee whose experience is more than 5
year of the table Emp(id, name, salary, exp)
Q-28: Write the SQL Query for inner join of two tables Emp(eid, ename, salary, dept_id) and
Dept(dept_id, dname)
Q-29: Write the SQL Query for full outer join of two tables Emp(eid, ename,salary,dept_id) and
Dept(dept_id, dname)
Q-30: Write a SQL to Enter 5 Employee data in a single query in the table Emp (eid, ename, salary, city,
dob).
Q-31: Display 4 characters extracted from 5th right character onwards from string ‘ABCDEFG’.
Q-32: Write a query to create a string from the ASCII values 65, 67.3, ‘68.3’
Q-33: Display names ‘MR. MODI’ and ‘MR. Sharma’ into lowercase
Q-35: Write any one similarity and one difference between Primary key and
unique key constraint
Similarity: Column with both the constraints will only take unique
values.
Difference: Column with Primary key constraints will not be able
to hold NULL values while Column with Unique constraints will
be able to hold NULL values.