0% found this document useful (0 votes)
46 views

Class 12 Practical File 2024-25

practial

Uploaded by

sakshay1871
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Class 12 Practical File 2024-25

practial

Uploaded by

sakshay1871
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Date : Experiment No: 6

Program 1: Program to read and display file content line by line with each
word separated by „#‟

#Program to read content of file line by line


#and display each word separated by '#'

f = open("file1.txt")

for line in f:
words = line.split()
for w in words:
print(w+'#',end='')
print()
f.close()

NOTE : if the original content of file is:


India is my country
I love python
Python learning is fun

OUTPUT
India#is#my#country#
I#love#python#
Python#learning#is#fun#

Page : 6
Date : Experiment No: 7

Program 1: Program to read the content of file and display the total number
of consonants, uppercase, vowels and lower case characters‟

#Program to read content of file


#and display total number of vowels, consonants, lowercase and uppercase characters

f = open("file1.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1
print("Total Vowels in file :",v)
print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters :",o)
f.close()
NOTE : if the original content of file is:
India is my country
I love python
Python learning is fun
123@

OUTPUT
Total Vowels in file : 16
Total Consonants in file n : 30
Total Capital letters in file :2
Total Small letters in file : 44
Total Other than letters :4

Page : 7
Date : Experiment No: 8

Program 1: Program to create binary file to store Rollno and Name, Search
any Rollno and display name if Rollno found otherwise “Rollno not found”

#Program to create a binary file to store Rollno and name


#Search for Rollno and display record if found
#otherwise "Roll no. not found"

import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
student.append([roll,name])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'

while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to search :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Search more ?(Y) :")
f.close()

Page : 8
OUTPUT
Enter Roll Number :1
Enter Name :Amit
Add More ?(Y)y

Enter Roll Number :2


Enter Name :Jasbir
Add More ?(Y)y

Enter Roll Number :3


Enter Name :Vikral
Add More ?(Y)n

Enter Roll number to search :2


## Name is : Jasbir ##
Search more ?(Y) :y

Enter Roll number to search :1


## Name is : Amit ##
Search more ?(Y) :y

Enter Roll number to search :4


####Sorry! Roll number not found ####
Search more ?(Y) :n

Page : 9
Date : Experiment No: 9

Program 1: Program to create binary file to store Rollno,Name and Marks


and update marks of entered Rollno

#Program to create a binary file to store Rollno and name


#Search for Rollno and display record if found
#otherwise "Roll no. not found"

import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
print("## Current Marks is :",s[2]," ##")
m = int(input("Enter new marks :"))
s[2]=m
print("## Record Updated ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y) :")
f.close()
Page : 10
OUTPUT
Enter Roll Number :1
Enter Name :Amit
Enter Marks :99
Add More ?(Y)y

Enter Roll Number :2


Enter Name :Vikrant
Enter Marks :88
Add More ?(Y)y

Enter Roll Number :3


Enter Name :Nitin
Enter Marks :66
Add More ?(Y)n

Enter Roll number to update :2


## Name is : Vikrant ##
## Current Marks is : 88 ##
Enter new marks :90
## Record Updated ##
Update more ?(Y) :y

Enter Roll number to update :2


## Name is : Vikrant ##
## Current Marks is : 90 ##
Enter new marks :95
## Record Updated ##
Update more ?(Y) :n

Page : 11
Date : Experiment No: 10

Program 1: Program to read the content of file line by line and write it to
another file except for the lines contains „a‟ letter in it.

#Program to read line from file and write it to another line


#Except for those line which contains letter 'a'

f1 = open("file2.txt")
f2 = open("file2copy.txt","w")

for line in f1:


if 'a' not in line:
f2.write(line)
print(“## File Copied Successfully! ##”)
f1.close()
f2.close()

NOTE: Content of file2.txt


a quick brown fox
one two three four
five six seven
India is my country
eight nine ten
bye!

OUTPUT

## File Copied Successfully! ##

NOTE: After copy content of file2copy.txt


one two three four
five six seven
eight nine ten
bye!

Page : 12
Date : Experiment No: 11

Program 1: Program to create CSV file and store empno,name,salary and


search any empno and display name,salary and if not found appropriate
message.

import csv
with open('myfile.csv',mode='a') as csvfile:
mywriter = csv.writer(csvfile,delimiter=',')
ans='y'
while ans.lower()=='y':
eno=int(input("Enter Employee Number "))
name=input("Enter Employee Name ")
salary=int(input("Enter Employee Salary :"))
mywriter.writerow([eno,name,salary])
print("## Data Saved... ##")
ans=input("Add More ?")
ans='y'
with open('myfile.csv',mode='r') as csvfile:
myreader = csv.reader(csvfile,delimiter=',')
while ans=='y':
found=False
e = int(input("Enter Employee Number to search :"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("============================")
print("NAME :",row[1])
print("SALARY :",row[2])
found=True
break
if not found:
print("==========================")
print(" EMPNO NOT FOUND")
print("==========================")
ans = input("Search More ? (Y)")

Page : 13
Enter Employee Number 1

Enter Employee Name Amit

Enter Employee Salary :90000

## Data Saved... ##

Add More ?y

Enter Employee Number 2

Enter Employee Name Sunil

Enter Employee Salary :80000

## Data Saved... ##

Add More ?y

Enter Employee Number 3

Enter Employee Name Satya

Enter Employee Salary :75000

## Data Saved... ##

Add More ?n

Enter Employee Number to search :2

============================

NAME : Sunil

SALARY : 80000

Search More ? (Y)y

Enter Employee Number to search :3

============================

NAME : Satya

SALARY : 75000

Search More ? (Y)y

Enter Employee Number to search :4

==========================

EMPNO NOT FOUND

==========================

Search More ? (Y)n

Page : 14
Date : Experiment No: 12

Program 1: Program to generate random number 1-6, simulating a dice

# Program to generate random number between 1 - 6


# To simulate the dice
import random
import time
print("Press CTRL+C to stop the dice ")
play='y'
while play=='y':
try:
while True:
for i in range(10):
print()
n = random.randint(1,6)
print(n,end='')
time.sleep(.00001)
except KeyboardInterrupt:
print("Your Number is :",n)
ans=input("Play More? (Y) :")
if ans.lower()!='y':
play='n'
break

OUTPUT

4Your Number is : 4
Play More? (Y) :y
Your Number is : 3
Play More? (Y) :y
Your Number is : 2
Play More? (Y) :n

Page : 15
Date : Experiment No: 13

Program 1: Program to implement Stack in Python using List

def isEmpty(S):
if len(S)==0:
return True
else:
return False

def Push(S,item):
S.append(item)
top=len(S)-1

def Pop(S):
if isEmpty(S):
return "Underflow"
else:
val = S.pop()
if len(S)==0:
top=None
else:
top=len(S)-1
return val

def Peek(S):
if isEmpty(S):
return "Underflow"
else:
top=len(S)-1
return S[top]

def Show(S):
if isEmpty(S):
print("Sorry No items in Stack ")
else:
t = len(S)-1
print("(Top)",end=' ')
while(t>=0):
print(S[t],"<==",end=' ')
t-=1
print()

Page : 16
# main begins here
S=[] #Stack
top=None
while True:
print("**** STACK DEMONSTRATION ******")
print("1. PUSH ")
print("2. POP")
print("3. PEEK")
print("4. SHOW STACK ")
print("0. EXIT")
ch = int(input("Enter your choice :"))
if ch==1:
val = int(input("Enter Item to Push :"))
Push(S,val)
elif ch==2:
val = Pop(S)
if val=="Underflow":
print("Stack is Empty")
else:
print("\nDeleted Item was :",val)
elif ch==3:
val = Peek(S)
if val=="Underflow":
print("Stack Empty")
else:
print("Top Item :",val)
elif ch==4:
Show(S)
elif ch==0:
print("Bye")
break

OUTPUT
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :10

Cont…
Page : 17
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :20

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :1
Enter Item to Push :30

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :4
(Top) 30 <== 20 <== 10 <==

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :3
Top Item : 30

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :2

Deleted Item was : 30

Page : 18
**** STACK DEMONSTRATION ******
1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :4
(Top) 20 <== 10 <==

**** STACK DEMONSTRATION ******


1. PUSH
2. POP
3. PEEK
4. SHOW STACK
0. EXIT
Enter your choice :0
Bye

Page : 19
SQL QUERIES

Tables used:
1. Emp table

2. Dept table
1. Write a query to display employee name, salary and department number who are not
getting commission.

SELECT ename, sal, deptno FROM emp


WHERE comm IS NOT null;

2. Write a query to display name and job of those employees who don’t have manager.

SELECT ename, job FROM emp


WHERE mgr IS null;

3. Write a query to display the number of employees working in different types of jobs.

SELECT job, COUNT(empno) as total_employees FROM emp


GROUP BY job;

4. Write a query to display the name, monthly salary and annual salary (sal *12) of all the
employees. The output should be in the following format:
e.g. SMITH earns 800 rupees in a month and 9600 rupees in a year.

SELECT ename ,"earns", sal ,"rupees in a month and ", sal*12 ,"rupees in a
year" FROM emp;
5. Write a query to display the names and salary of all the employees in descending order
of salary.

SELECT ename, sal FROM emp


ORDER BY sal desc;

6. Write a query to display the details of only those employees whose names have only 4
letters.

SELECT * FROM emp


WHERE ename LIKE ’_ _ _ _’ ;

7. List the count of employees in different departments (DeptNo).

SELECT deptno, count(ename) as total_employees FROM emp


GROUP BY deptno;

8. Write a query to display the sum of salaries of different employees working under
different jobs.

SELECT job, SUM(sal) AS "Total Salary"


FROM emp
GROUP BY job;
9. Write a query to display the average salaries of only those jobs under which more than
3 employees are working.

SELECT deptno, AVG(sal) as Avg_salary FROM emp


GROUP BY deptno HAVING COUNT(deptno) >3;

10. Write a query to display the department no, average salary and average commission
of different departments in emp table having average salary more than 2000 and
average commission more than 750.

SELECT deptno, AVG(sal), AVG(comm)


FROM emp
GROUP BY deptno
HAVING AVG(sal) > 2000 AND AVG(comm) > 750;

11. Write a query to change the salary of employee having empid as 7839 to 7000.

UPDATE emp
SET sal = 7000
WHERE empno = 7839;

12. Write a query to display the senior most employee.

SELECT ename, deptno, hiredate FROM emp


WHERE hiredate = (SELECT MAX(hiredate) FROM emp)
ORDER BY hiredate DESC;

13. Write a query to display employee names and department names they are working in
from emp and dept table.

select e.ename,e.DEPTNO,d.DNAME from emp e


join dept d on e.deptno=d.deptno;
14. Write a query to display the employee name and location they are posted at using emp
and dept tables.

SELECT emp.ename, dept.dname


FROM emp
INNER JOIN dept
ON emp.deptno = dept.deptno;

15. Write a query to display the name of employee having maximum commission along
with the department name.

SELECT emp.ename, dept.dname


FROM emp inner join dept on emp.deptno = dept.deptno
WHERE comm = (SELECT MAX(comm) FROM emp);

16. Write a query to create a table Employee based on the following table instance chart:

CREATE TABLE Employee (


Id INT(8),
First_Name VARCHAR(25),
Last_Name VARCHAR(25),
Dept_ID INT(8)
);

17. Insert 5 records in the table created in the above query.

INSERT INTO employee (ID, First_Name, Last_Name, Dept_ID) VALUES


(101, "Nivedita", "Chhokar", 2309),
(102, "Pranshu", "Battan", 5020),
(103, "Bhavya", "Kamboj", 1306),
(104, "Savita", "Rani", 2004),
(105, "Ashok", "Kumar", 7070);

18. Write a query to add one more column address in the table employee.

ALTER TABLE employee


ADD COLUMN address VARCHAR(255);
19. Write a query to change the length of First_Name field to 30.

ALTER TABLE employee


MODIFY COLUMN First_Name VARCHAR(30);

20. Write a query to add primary key constraint to make ID as primary key.

ALTER TABLE employee


ADD CONSTRAINT pk_employee_id PRIMARY KEY (ID);

You might also like