Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

NAME – Deepak

CLASS – 12TH – A
ROLL NO. – 11
SCHOOL – kendriya
Vidyalaya sainik vihar
COMPUTER SCIENCE PRACTICAL LIST FOR CLASS XII

1. Write a python program to search an element in a list and display the frequency of element
present in list and their location using Linear search by using user defined function. [List and
search element should be entered by user]

2. Write a python program to search an element in a list and display the frequency of element
present in list and their location using binary search by using user defined function. [List and
search element should be entered by user]

3. Write a python program to pass list to a function and double the odd values and half even
values of a list and display list element after changing.

4. Write a Python program input n numbers in tuple and pass it to function to count how many
even and odd numbers are entered.

5. Write a Python program to function with key and value, and update value at that key in
dictionary entered by user.

6. Write a Python program to pass a string to a function and count how many vowels present in
the string.

7. Write a Python program to generator(Random Number) that generates random numbers


between 1 and 6 (simulates a dice) using user defined function.

8. Write a python program to implement python mathematical functions.

9. Write a python program to implement python string functions.

10. Write a python program to read and display file content line by line with each word separated
by #.

11. Write a python program to remove all the lines that contain the character ‘a’ in a file and write
it to another file.

12. Write a python program to read characters from keyboard one by one, all lower case letters
gets stored inside a file “LOWER”, all uppercase letters gets stored inside a file “UPPER”, and all
other characters get stored inside “OTHERS”
13. Write a python program to create a binary file with name and roll number. Search for a given
roll number and display name, if not found display appropriate message.

14. Write a python program to create a binary file with roll number, name and marks, input a roll
number and update the marks.

15. Write a python program to create a CSV file with empid, name and mobile no. and search
empid, update the record and display the records.

16. Write a Python program to create Lpush( ) and Lpop( ) function to do push and pop operation
on a stack using a list e.g. take a student information and push and pop the details.

17. Create a student table and insert data. Implement the following SQL commands on the student
table:
ALTER table to add new attributes / modify data type / drop attribute
UPDATE table to modify data ORDER By to display data in ascending / descending order
DELETE to remove tuple(s) GROUP BY and find the min, max, sum, count and average

18. Integrate SQL with Python by importing the MySQL module record of employee and display the
record.

19. Integrate SQL with Python by importing the MySQL module to search an employee using
empno and if present in table display the record, if not display appropriate method.

20. Integrate SQL with Python by importing the MySQL module to search a student using rollno,
update the record.

21. Integrate SQL with Python by importing the MySQL module to search a student using rollno,
delete the record.

22. Take a sample of ten phishing e-mails (or any text file) and find most commonly occurring
word(s)
PRACTICAL- 1

AIM- Write a program to search an element in a list and display the


frequency of element present in list and their location using
Linear search
SOFTWARE USED- IDLE (PYTHON 3.10 64-bit)

INPUT

OUTPUT
PRACTICAL- 2

AIM- Write a to search an element in a list and display the frequency


of element present in list and their location using Binary Search
SOFTWARE USED- IDLE (PYTHON 3.10 64-bit)

INPUT

OUTPUT

========= RESTART: C:/Python310/deepak.py ==


Enter a list: [3,4,5,6,7]
Enter the element to search :7
('Element found! index:', 4, 'Frequency :, 1)
PRACTICAL- 3

AIM- Write a program to pass list to a function and double the odd
values and half even values of a list and display list element
after changing
SOFTWARE USED- IDLE (PYTHON 3.10 64-bit)

INPUT

OUTPUT

======== RESTART: C:/Python310/deepak.py ===


Enter a list: [1,2,3,4,5,6,7,8]
[2, 1.0, 6, 2.0, 10, 3.0, 14, 4.0]
PRACTICAL- 4

AIM- Write a program to input n numbers in tuple and pass it to


function to count how many even and odd numbers are entered
SOFTWARE USED- IDLE (PYTHON 3.10 64-bit)

INPUT

OUTPUT

=========== RESTART: C:/Python310/deepak.py ==


Enter a tuple: (1,2,3,4,5,6,7)
Number of even numbers: 3 Number of odd numbers: 4
PRACTICAL- 5

AIM- Write a program to function with key and value, and update value at that
key in dictionary entered by user

SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT

OUTPUT

========== RESTART: C:/Python310/deepak.py ==


Number of pairs in dictionary :3
Enter the key: "Name"
Enter the value: "Elon Musk"
Enter the key: "Age"
Enter the value :32
Enter the key: "Profession"
Enter the value: "Tech company owner"
Original dictionary: {'Name': 'Elon Musk', 'Age': 32, Profession': 'Tech company
owner'}
Enter the key whose value you want to change: "Age"
Enter the value :33
Updated dictionary: {'Name': 'Elon Musk', 'Age': 33, Profession': 'Tech company
owner'}
PRACTICAL- 6

AIM- Write a program to pass a string to a function and count how


many vowels present in the string
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT

OUTPUT

=========== RESTART: C:/Python310/deepak.py ==


Enter a string: "mIsSiSsIpPi State"
Number of vowels: 6
PRACTICAL- 7

AIM- Write a program to generator that generates random numbers


between 1 and 6 using user defined function
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT

OUTPUT(S)

=========== RESTART: C:/Python310/deepak.py ==


Random number generated between 1 to 6:6
>>>
=========== RESTART: C:/Python310/deepak.py ==
Random number generated between 1 to 6:1
>>>
=========== RESTART: C:/Python310/deepak.py ==
Random number generated between 1 to 6:2
>>>
=========== RESTART: C:/Python310/deepak.py ==
Random number generated between 1 to 6:6
>>>
=========== RESTART: C:/Python310/deepak.py ==
Random number generated between 1 to 6:6
>>>
=========== RESTART: C:/Python310/deepak.py ==
Random number generated between 1 to 6:3
PRACTICAL- 8

AIM- Write a program to implement python mathematical functions.


SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT
OUTPUT(S)

= RESTART: C:\Python310\deepak.py
Choose which function to use:
1. Square root (sqrt)
2. Factorial
3. Power
4. Log
Trigonometric functions:
5. sin
6. cos
7. tan
Value of Constants:
8. e
9. pi
10. tau
Enter operation number: 4
Enter a number: 20
Enter base: 10
1.301029995663981
= RESTART: C:\Python310\deepak.py
Choose which function to use:
1. Square root (sqrt)
2. Factorial
3. Power
4. Log
Trigonometric functions:
5. sin
6. cos
7. tan
Value of Constants:
8. e
9. pi
10. tau
Enter operation number: 3
Enter a number: 2
Enter power: 5
32.0
PRACTICAL- 9

AIM- Write a program to implement python string functions


SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT

OUTPUT

=========== RESTART: C:/Python310/deepak.Py ======


Enter sentence: "LOWER Case"
enter the spacing:
The string entered is a word: False
The string entered in lower case: "lower case"
The string entered is in lower case: False
The string entered in lower case: "LOWER CASE"
The string entered is in lower case: False
The string entered after removing the space from left side: "LOWER Case"
The string entered after removing the space from right side: "LOWER Case"
The string entered contains whitespace: False
The string entered is title cased: False
The string entered after joining with -: "-L-o-w-e-R- -C-a-s-e-"
The string entered after swiping case: "lower CASE"
PRACTICAL- 10

AIM- Write a program to read and display file content line by line
with each word separated by #.
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT

#TO READ AND DISPLAY FILE CONTENT LINE BY LINE WITH EACH WORD SEPARATED BY #
a= open ("TEXT. Txt ","r")
lines a. readlines ()
for line in lines:
x=line. Split ()
for y in x:
print (y+"#", end = “ ” )
print (" ")

OUTPUT

=========== RESTART: C:/Python310/deepak.py =


Hello # World #
Welcome # to # python #
Enjoy # programing #
PRACTICAL- 11

AIM- Write a program to remove all the lines that contain the
character ‘a’ in a file and write it to another file.
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT
file=open (“TEXT1. txt","r")
lines=file. readlines ()
file.close()
file=open ( TEXT1.txt", "W")
file=open ( MODTEXT1.txt", "W")
for line in lines:
if 'a' in line :
filel.write(line)
else:
file.write (line)
print ("Lines that contain a character are removed from TEXT1")
print ("Lines that contain a character are added in MODTEXT1 ")
file.close()
file1.close()

OUTPUT
PRACTICAL- 12

AIM- Write a program to read characters from keyboard one by one,


all lower case letters gets stored inside a file “LOWER”, all
uppercase letters gets stored inside a file “UPPER”, and all other
characters get stored inside “OTHERS”
SOFTWARE USED- IDLE (PYTHON 3.10 64-bit)

INPUT

f1=open ("LOWER.txt", "W")


f2=open ("UPPER.txt", "W")
f3=open ("OTHERS. txt","w")
print('Input "~" to stop execution.')
while True:
c=input("Enter a single character :")
if c=="~" :
break
elif c.islower():
fi.write(c)
elif c.isupper():
f2.write(c)
else:
f3.write(c)
f1.close()
f2.close()
f3.close()
OUTPUT
PRACTICAL- 13

AIM- Write a program to create a binary file with name and roll number.
search for a given roll number and display name, if not found display
appropriate

SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT

#CREATE A BINARY FILE WITH NAME & ROLL NUMBER. SEARCH FOR A GIVEN ROLL NUMBER AND DISPLAY NME, IF NOT
FOUND DISPLAY APPROPRIATE MESSAGE
import pickle
s data= {}
s list= []
totals =int (input ("Enter number of students:”))
for i in range (totals):
s data [ "Roll no."] =int (input ("Enter roll no. :))
s data ["Name"] = input ("Enter name:")
s list. Append (s data)
s data= {}
a= open ("TEXT2.dat", "b")
pickle. dump (s list, a)
a. Close ()

x= open (“TEXT2.dat", " rb")


s list = pickle. load(x)
b=int (input ("Enter the roll number of student to be searched :))
y=false
for TEXT2 in s list:
if (TEXT2("Roll no.") =b):
y=true
print (TEXT2("Name"), "Found in file")
if (y=false):
print ("Data of student not found.”)

OUTPUT

========= RESTART: C:/python310/Deepak.py ==


Enter number of students :3
Enter roll no. :06
Enter name: Ankit
Enter roll no. :14
Enter name: Harsh
Enter roll no. :11
Enter name: Deepak
Enter the roll number of student to be searched :11
Deepak Found in file
PRACTICAL- 14

AIM- Write a program to create a binary file with roll number, name
and marks, input a roll number and update the marks.
SOFTWARE USED- IDLE (PYTHON 3.10 64-bit)

INPUT

#TO CREATE A BINARY FILE WITH ROLL NUMBER, NAME & MARKS, INPUT A ROLL NUMBER &
UPDATE THE MARKS.
import pickle

sdata={}
totals=int (input("Enter number of students :"))
a=open (r"C:\Users\Dell\Documents\ankitverma\Python\TEXT3.dat", "wb")
for i in range (totals):
sdata["Roll no"]=int (input ("Enter roll no. :))
sdata["Name"]=input("Enter name :")
sdata["Marks"]=float(input ("Enter marks :"))
pickle. dump (sdata, a)
sdata={}
a.close()

#UPDATING MARKS
found=False
rollno=int (input ("Enter roll number :"))
a=open (r"C:\Users\Dell\Documents\ankitverma\Python\TEXT3.dat", "rb+")
try:
while True:
pos=a. tell()
sdata=pickle.load(a)
if (sdata("Roll no"]==rollno):
sdata("Marks"]=float (input("Enter new marks :"))

a. seek (pos)
pickle. dump (sdata, a)
found=true
except EOFError:
if found==False:
print("Roll number not found.")
else:
print("Students marks updated successfully.")
a.close()

OUTPUT

======== RESTART: C:/Python310/deepak.py ==


Enter number of students :3
Enter roll no. :25
Enter name: Ankita
Enter marks: 94
Enter roll no.: 41
Enter name: Hemant
Enter marks: 95
Enter roll no. :39
Enter name: Deepak
Enter marks: 93
Enter roll number: 39
Enter new marks: 96
Student’s marks updated successfully
PRACTICAL- 15

AIM- Write a program to create a CSV file with empid, name and
mobile no. and search empid, update the record and display the
records
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT

#Input Data
import csv
with open (employee. txt', 'a') as csv file:
my writer = csv. Writer (csv file, delimiter=',')
ans ='y'
while an. Lower () =='Y':
eno = int (input ("Enter Employee ID:"))
name= input ("Enter Employee Name:")
mobile no. =int (input ("Enter Employee Mobile No:"))
my writer. Write row ([e no, name, mobile no.])
answer=input ("Do You Want To Enter More Data? (y/n):")
answer='y'
#Search Empid And Display The Records
with open (employee. txt', 'r') as csv file:
my reader = csv. reader (csv file, delimiter=',')
answer='y'
while an. Lower () == 'y':
found=false
e = int (input ("Enter Employee ID to search :"))
for row in my reader:
if len (row) !=0:
if int (row [O]) ==e:
print ("Name:", row [1])
print ("Mobile no.: " row [2])
found=true
break
if not found:
print ("Employee ID not found.")
answer = input ("Do You Want To Search More? (y/n):")
answer ='y'
with open (‘employee. txt', 'a') as csv file:
x=csv. writer (csv file)
x. write row ([eno, name, mobile no.])
OUTPUT

Enter Employee ID: 1


Enter Employee Name: Deepak
Enter Employee Mobile No: 91986412
Do You Want To Enter More Data? (y/n) y
Enter Employee ID: 2
Enter Employee Name: Harsh
Enter Employee Mobile No: 941827498
Do You Want To Enter More Data? (y/n) :n
Enter Employee ID to search :1
Name : Ansh
Mobile no. : 919864124
Do You Want To Search More? (y/n):y
Enter Employee ID to search :2
Name : Harsh
Mobile no.: 941827498
Do You Want To Search More? (y/n):nOUTPUT
PRACTICAL- 16

AIM- Write a program to create Lpush() and Lpop() function to do


push and pop operation on a stack using a list e.g. take a student
information and push and pop the details
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT
OUTPUT

======= RESTART: C:\Python310\deepak.py ==


***STACK DEMONSTRATION***
1. PUSH
2. POP
0. EXIT
Enter your choice :1
Enter the item of Push :10
***STACK DEMONSTRATION***
1. PUSH
2. POP
0. EXIT
Enter your choice :2
Deleted Item was: 10
***STACK DEMONSTRATION***
1. PUSH
2. POP
0. EXIT
Enter your choice :0
THANK YOU
PRACTICAL- 17

AIM- Write a program to create a student table and insert data.


Implement the following SQL commands on the student table:
ALTER table to add new attributes / modify data type / drop attribute

UPDATE table to modify data ORDER By to display data in ascending / descending


order

DELETE to remove tuple(s) GROUP BY and find the min, max, sum, count and
average

SOFTWARE USED- MySQL Workbench 8.0 CE

INPUT

use studentdb;

Database changed

CREATING TABLE

create table student

(roll int not null,

studentname varchar(30) not null,

class char(5) not null,

section char(1) not null,

classstream char(20) not null);

desc student;

INSERTING VALUE

insert into student values

(1, "Akhil", "XII", "A", "Science"),

(2, "Satya", "XII", "C", "Science"),

(3, "Antony", "XII", "D", "Commerce"),

(4, "Vishal", "XII", "E", "Humanities"),

(5, "Deepak", "XII", "A", "Science"),


(6, "Brij", "XII", "B", "Science");

select * from student;

ADDING COLUMN IN TABLE USING ALTER COMMAND

alter table student

add(Substream char(20) not null),

(Percentage float not null);

desc student;

UPDATING VALUE

update student set Substream = "Computer Science" where roll = 1;

update student set Substream = "Biology" where roll = 2;

update student set Substream = "Maths" where roll = 3;

update student set Substream = "NA" where roll = 4;

update student set Substream = "Computer Science" where roll = 5;

update student set Substream = "Computer Science" where roll = 6;

select * from student;

update student set Percentage = 81 where roll = 6;

update student set Percentage = 69 where roll = 6;

update student set Percentage = 92 where roll = 6;


update student set Percentage = 55 where roll = 6;

update student set Percentage = 85 where roll = 6;

update student set Percentage = 72 where roll = 6;

select * from student;

alter table student

add(

Adhaar_card bigint not null);

desc student;

alter table student

drop column Adhaar_card;

desc student;

ORDER BY COMMAND

select studentname,section from student order by Percentage;


AVERAGE COMMAND

select avg(Percentage) from student;

COUNT COMMAND

select count(*) from student;

MIN, MAX AND SUM COMMAND

select min(Percentage) from student;

select max(Percentage) from student;

select sum(Percentage)/count(*) from student where substream = "Computer Science";

DELETE A TUPLE FROM TABLE

delete from student where classstream = "Science";

select * from student;


PRACTICAL- 18

AIM- Write a program to Integrate SQL with Python by importing the


MySQL module record of employee and display the record.
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

MySQL Workbench 8.0 CE

INPUT

OUTPUT

Python 3.8. 6rel (tags/v3.8. 6rcl: 08bd63d, Sep 7 2020, 23:10:23) [MSC v.1927 64 bit (AMD64)] on win32 Type
"help","copyright", "credits" or "license ()" for more information.
>>>>>>>>
== RESTART: C:/Users/Dell/Documents/deepak.py===

(9100727,‘Deepak’,datetime.date(1989,3,24),'F',916457.0,'Security','Mumbai',datetime.date (2011,1,20))
(9100906,'Shubham',datetime.date(1985,12,7),'M',843749.0,'Sales','Hyderabad',datetime.date(2007,6, 18))
(9200172,”Adhiraj”,datetime.date(1986,11,21),'M',1470420.0,'Security','Hyderabad',datetime.date(2008,5, 3))
(9200593, 'Ashish', datetime.date (1988, 6, 17), 'M', 1237820.0, 'Legal', 'Delhi', datetime.date (2010, 11, 24))
(9300866, 'Anjali', datetime.date (1986, 1, 13), 'P', 1937590.0, 'Finance', 'Mumbai', datetime date (2008, 9, 28))
(9700356, 'Kartik', datetime. date (1987, 9, 30), 'M', 2163510.0, 'Sales', 'Delhi', datetime.date (2009, 2, 12))
PRACTICAL- 19

AIM- Write a program to Integrate SQL with Python by importing the


MySQL module to search an employee using empno and if present in
table display the record, if not display appropriate method.
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

MySQL Workbench 8.0 CE

INPUT

OUTPUT
PRACTICAL- 20

AIM- Write a program to Integrate SQL with Python by importing the


MySQL module to search a student using roll no, update the record
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

MySQL Workbench 8.0 CE

INPUT

OUTPUT
PRACTICAL- 21

AIM- Write a program to Integrate SQL with Python by importing the


MySQL module to search a student using roll no, delete the record.
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

MySQL Workbench 8.0 CE

INPUT

OUTPUT
PRACTICAL- 22

AIM- Write a program to Take a sample of ten phishing e-mails (or


any text file) and find most commonly occurring word(s)
SOFTWARE USED- IDLE (PYTHON 3.10 64-BIT)

INPUT

file= open(r"C:\Python310/email.txt", "r")


content=file.read()
max=0
max_occuring_word = "
occurrences_dict = {}
words = content.split()
for word in words:
count = content.count (word)
occurrences_dict.update({word: count})
if (count>max):
max = count
max_occurring word = word
print("Most occurring word is :", max_occurring_word)
print("No. of timess it is occurring :", max)
print("Frequency of other words is :")
print (occurrences dict)

OUTPUT

Most occurring word is : win


No. of timess it is occurring : 9
Frequency of other words is :
('100': 4, "crore': 4, 'jackpot': 6, 'gain': 4, "69': 1, "@myluck.com': 3, 77777': 1,
"@awesomedeal.com': 2, 79456': 1,’@winwin.com: 3, "claim': 2, "the": 4, 'deal': 5, '420': 1, "you':
2, "are': 2, 456': 3, "9999': 1, 2221': 1, "lottow inwin.com': 1, "your': 1, 222': 2, "win': 9,
'spin': 1, 'wheel': 1, "123456': 1, "xyz.com': 1)

You might also like