0% found this document useful (0 votes)
343 views26 pages

XII CS PRATICALS 1 To16 PDF

The document contains an index of programming tasks completed by a student named Onkar A Shahane in class XII-A. The index includes 17 tasks completed between July 26 and August 8, with tasks involving searching and manipulating lists, tuples, dictionaries, strings and files in Python. It also includes tasks integrating Python with SQL and analyzing text files. Each task is labeled with the date and aim/topic and includes the code and output for the task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
343 views26 pages

XII CS PRATICALS 1 To16 PDF

The document contains an index of programming tasks completed by a student named Onkar A Shahane in class XII-A. The index includes 17 tasks completed between July 26 and August 8, with tasks involving searching and manipulating lists, tuples, dictionaries, strings and files in Python. It also includes tasks integrating Python with SQL and analyzing text files. Each task is labeled with the date and aim/topic and includes the code and output for the task.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

1

NAME : ONKAR A SHAHANE


ROLL No : 34
CLASS : XII – A
2

INDEX
SL. DATE TOPIC TEACHER
NO. SIGN
1. 26/07/2020 WAP 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
2. 27/07/2020 WAP 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.
3. 28/07/2020 WAP 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. 29/07/2020 WAP input n numbers in tuple and pass it to function to count how many
even and odd numbers are entered.
5. 30/07/2020 WAP to function with key and value, and update value at that key in
dictionary entered by user
6. 31/07/2020 WAP to pass a string to a function and count how many vowels present in
the string
7. 01/08/2020 WAP to generator that generates random numbers between 1 and 6 using
user defined function
8. 02/08/2020 WAP to implement python mathematical functions.

9. 03/08/2020 WAP implement python string functions

10. 04/08/2020 WAP to make user define module and import same in another module or
program.

11. 05/08/2020 WAP to read and display file content line by line with each word separated
by #
12. 06/08/2020 WAP to remove all the lines that contain the character ‘a’ in a file and write
it to another file.

13. 07/08/2020 WAP 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”
14. 08/08/2020 WAP 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.

15 09/08/2020 WAP to create a binary file with roll number, name and marks, input a roll
number and update the marks.
16. 10/08/2020 WAP to create a CSV file with empid, name and mobile no. and search
empid, update the record and display the records.
17. 11/08/2020

18. 12/08/2020
3

19.

20.

21.

22.

23.
4

KENDRIYA VIDYALAYA SECTOR-8, R K PURAM, NEW DELHI-110022


(Shift-1) SESSION 2020-21
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 user1.
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 make user define module and import same in another module or
program.
11. Write a python program to read and display file content line by line with each word separated by
#.
12. Write a python program to remove all the lines that contain the character ‘a’ in a file and write it
to another file.
13. 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”
14. 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.
15. Write a python program to create a binary file with roll number, name and marks, input a roll
number and update the marks.
16. 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.
17. 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.
5

18. 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
19. Integrate SQL with Python by importing the MySQL module record of employee and display the
record.
20. 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.
21. Integrate SQL with Python by importing the MySQL module to search a student using rollno,
update the record.
22. Integrate SQL with Python by importing the MySQL module to search a student using rollno,
delete the record.
23. Take a sample of ten phishing e-mails (or any text file) and find most commonly occurring
word(s)
6

26/07/2020
AIM

WAP 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.7 32-bit)

#TO LINEAR SEARCH AN ELEMENT IN A LIST AND DISPLAY THE FREQUENCY AND LOCATION
def fun(l,x):
count=0
for i in range(0,len(l)):
if l[i]==x:
print("Found location =",i)
count+=1
print("Frequency of element",count)
a=eval(input("Enter a list ="))
b=eval(input("Element to be searched ="))
fun(a,b)
>>>
=======================RESTART:E:/SOFTWARE/ pritical1.py===================
Enter a list =[1,4,2,3,4,5,4,4]
Element to be searched =4
Found location = 1
Found location = 4
Found location = 6
Found location = 7
Frequency of element 4
7

27/07/2020
AIM

WAP 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.7 32-bit)

#TO BINARY SEARCH AN ELEMENT IN A LIST AND DISPLAY THE FREQUENCY AND LOCATION
def fun(l,a):
count=0
low=0
high=len(l)-1
while low<=high:
mid=int((low+high)/2)
if a==l[mid]:
count+=1
return ("Element found!,index:",mid,"Frequency:",count)
elif a<l[mid]:
high=mid-1
else:
low=mid+1
x=eval(input("Enter a list ="))
y=eval(input("Enter the element to search ="))
print(fun(x,y))>>>
========================RESTART:E:/SOFTWARE/pritical2.py ====================
Enter a list =[3,4,5,6,7]
Enter the element to search =7
('Element found!,index:', 4, 'Frequency:', 1)
8

28/07/2020
AIM

WAP 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.7 32-bit)

#TO PASS LIST & DOUBLE THE ODD VALUES AND HALF EVEN VALUES & DISPLAY THE CHANGED
LIST
def f(l):
l2=[]
for i in range(0,len(l)):
if l[i]%2==0:
e=l[i]/2
l2.append(e)
else:
e=l[i]*2
l2.append(e)
print(l2)
a=eval(input("Enter a list ="))
f(a)
>>>
======================== RESTART: E:/SOFTWARE/pratical3.py===================

Enter a list =[1,2,3,4,5,6,7,8]


[2, 1.0, 6, 2.0, 10, 3.0, 14, 4.0]
9

29/07/2020
AIM

WAP 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.7 32-bit)

#TO INPUT N NUMBERS IN TUPLE & COUNT HOW MANY EVEN & ODD NUMBERS ARE ENTERED
def fun(l):
e=0
o=0
for i in range(0,len(l)):
if l[i]%2==0:
e+=1
else:
o+=1
print("Number of even numbers = ",e,"Number of odd numbers = ",o)
x=eval(input("Enter a tuple = "))
fun(x)
>>>
======================== RESTART: E:/SOFTWARE/pratical4.py===================

Enter a tuple = (1,2,3,4,5)


Number of even numbers = 2 Number of odd numbers = 3
10

30/07/2020
AIM
WAP to function with key and value, and update value at that key in dictionary entered by user

SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

#TO UPDATE THE KEY AND VALUE OF EXISTING DICT ENTERED BY USER
def fun(d,k):
value2=eval(input("enter the value"))
d[k]=value2
print("updated dictionary:",d)
x=int(input("number of pairs in dictoinary:"))
dic={}
for i in range(x):
key=eval(input("enter the key"))
value=eval(input("enter the value"))
dic[key]=value
print("original dictionary:",dic)
a=(eval(input("enter the key whose value you wants to change")))
fun(dic,a)
>>>
======================== RESTART: E:/SOFTWARE/pratical5.py===================
Number of pairs in dictoinary = 3
Enter the key = "Name"
Enter the value = "Bhuvan Bam"
Enter the key = "Age"
Enter the value = 23
Enter the key = "Profession"
Enter the value = "Youtuber"
Original dictionary = {'Name': 'Bhuvan Bam', 'Age': 23, 'Profession': 'Youtuber'}
Enter the key whose value you wants to change = "Age"
Enter the value = 26
Updated dictionary = {'Name': 'Bhuvan Bam', 'Age': 26, 'Profession': 'Youtuber'}
11

31/07/2020
AIM

WAP to pass a string to a function and count how many vowels present in the string
SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

#TO COUNT HOW MANY VOWELS PRESENT IN THE STRING


def fun(l):
count=0
for i in range(0,len(l)):
if l[i]=="a" or l[i]=="A" :
count+=1
if l[i]=="e" or l[i]=="E":
count+=1
if l[i]=="i" or l[i]=="I":
count+=1
if l[i]=="o" or l[i]=="O":
count+=1
if l[i]=="u" or l[i]=="U":
count+=1
print("Number of vowels = ",count)
a=eval(input("Enter a string = "))
fun(a) >>>
======================== RESTART: E:/SOFTWARE/pratical6.py===================

Enter a string = RaSHtIPATi bHaVaN


Number of vowels = 6
12

01/08/2020
AIM
WAP to generator that generates random numbers between 1 and 6 using user defined function

SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

#TO GENERATE RANDOM NUMBERS BETWEEN 1 AND 6 USING USER DEFINED FUNCTION
def fun():
import random
r=random.randint(1,6)
print("Random number generated between 1 to 6 = ",r)
fun()
>>>
======================== RESTART: E:/SOFTWARE/pratical7.py===================
Random number generated between 1 to 6 = = 3
>>>
======================== RESTART: E:/SOFTWARE/pratical7.py===================
Random number generated between 1 to 6 = = 1
>>>
======================== RESTART: E:/SOFTWARE/pratical7.py===================
Random number generated between 1 to 6 = = 4
>>>
======================== RESTART: E:/SOFTWARE/pratical7.py===================
Random number generated between 1 to 6 = = 2
13

02/08/2020
AIM

WAP to implement python mathematical functions.


SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

#TO IMPLEMENT PYTHON MATHEMATICAL FUNCTIONS


import math as m
a=eval(input("Enter a number for ceil value= "))
print(" Ceil value",a,"=",m.ceil(a))
a=eval(input("Enter a number for factorial = "))
print("Factorial",a," =",m.factorial(a))
a=eval(input("Enter a number for floor value = "))
print(" Floor value",a,"=",m.floor(a))
a=eval(input("Enter a number for fmod= "))
n=int (input("Enter value of n = "))
print("The value fmod",a,"=",m.fmod(a,n))
a=eval(input("Enter a number for exponential value = "))
print("Exponential value",a," =",m.exp(a))
a=eval(input("Enter a number for value of base2 log= "))
print("Value of log base2 ",a,"=",m.log2(a))
a=eval(input("Enter a number for value of base10 log= "))
print("Value of log base10 =",m.log10(a))
a=eval(input("Enter a number for power function = "))
n=int (input("enter the power = "))
print(n," power",a," =",m.pow(a,n))
a=eval(input("Enter a number for square root = "))
print(" The sqrt value of ",a,"=",m.sqrt(a))
a=eval(input("Enter a number = "))
print(" cos",a,"=",m.cos(a))
a=eval(input("Enter a number = "))
print("sin",a," =",m.sin(a))
a=eval(input("Enter a number = "))
print(" tan",a,"=",m.tan(a))
print("value of pi =",m.pi)
print("value of e =",m.e)
>>>
14

======================== RESTART: E:/SOFTWARE/pratical8.py=====================


Enter a number for ceil value= 4.35
Ceil value 4.35 = 5
Enter a number for factorial = 4
Factorial 4 = 24
Enter a number for floor value = 35.94
Floor value 35.94 = 35
Enter a number for fmod= 6
Enter value of n = 4
The value fmod 6 = 2.0
Enter a number for exponential value = 1
Exponential value 1 = 2.718281828459045
Enter a number for value of base2 log= 10
Value of log base2 10 = 3.321928094887362
Enter a number for value of base10 log= 2
Value of log base10 = 0.3010299956639812
Enter a number for power function = 2
enter the power = 4
4 power 2 = 16.0
Enter a number for square root = 25
The sqrt value of 25 = 5.0
Enter a number = 100
cos 100 = 0.8623188722876839
Enter a number = 45
sin 45 = 0.8509035245341184
Enter a number = 45
tan 45 = 1.6197751905438615
value of pi = 3.141592653589793
value of e = 2.718281828459045
15

03/08/2020
AIM

WAP to implement python string functions


SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

#TO IMPLEMENT PYTHON STRING FUNCTIONS


c=str(input("Enter sentence = "))
a=input("enter the spacing = ")
print("The string entered is a word =",c.isalpha())
print("The string entered in lower case =",c.lower())
print("The string entered is in lower case =",c.islower())
print("The string entered in lower case =",c.upper())
print("The string entered is in lower case =",c.isupper())
print("The string entered after removing the spave from left side =",c.lstrip())
print("The string entered after removing the spave from right side =",c.rstrip())
print("The string entered contains whitespace =",c.isspace())
print("The string entered is titlecased =",c.istitle())
print("The string entered after joining with ",a," =",a.join(c))
print("The string entered after swaping case =",c.swapcase())

>>>
======================== RESTART: E:/SOFTWARE/pratical9.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 spave from left side = LoWeR CaSe
The string entered after removing the spave from right side = LoWeR CaSe
The string entered contains whitespace = False
The string entered is titlecased = False
The string entered after joining with - = L-o-W-e-R- -C-a-S-e
The string entered after swaping case = lOwEr cAsE
16

04/08/2020
AIM

WAP to make user define module and import same in another module or program
SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

#CALCULATOR MODULE
def add(a,b):
return a+b
def sub(a,b):
return a-b
def multi (a,b):
return a*b
def div (a,b):
return a/b

#TO MAKE USER DEFINE MODULE AND IMPORT SAME IN ANOTHER


MODULE OR PROGRAM
from Calc import *
a=int(input("Enter a number = "))
b=int(input("Enter a number = "))
c=add(a,b)
d=sub(a,b)
e=multi(a,b)
f=div(a,b)
print("Addition of ",a," and ",b," = ",c)
print("Subtraction of ",a," and ",b," = ",d)
print("Multiplication of ",a," and ",b," = ",e)
print("Division of ",a," and ",b," = ",f)

>>>
17

======================= RESTART: E:/SOFTWARE/pratical10.py=====================

Enter a number = 6
Enter a number = 2
Addition of 6 and 2 = 8
Subtraction of 6 and 2 = 4
Multiplication of 6 and 2 = 12
Division of 6 and 2 = 3.0

__________________________________________________________________________
18

05/08/2020
AIM

WAP to read and display file content line by line with each word separated by #.
SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

#TO READ AND DISPLAY FILE CONTENT LINE BY LINE WITH EACH WORD SEPARATED BY #
a= open ("E:\\SOFTWARE\\TEXT.txt","r")
lines=a.readlines()
for line in lines:
x=line.split()
for y in x:
print(y+" # " , end=" ")
print(" ")

>>>
======================= RESTART: E:/SOFTWARE/pratical11.py=====================

Hello # World #
Welcome # to # python #
Enjoy # programing #
19

06/08/2020
AIM
WAP to remove all the lines that contain the character ‘a’ in a file and write it to another file.
SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)
#TO REMOVE ALL THE LINES THAT CONTAIN THE CHARACTER ‘A’ IN A FILE & WRITE IT TO
ANOTHER FILE
file=open("E:\\SOFTWARE\\TEXT1.txt","r")
lines=file.readlines()
file.close()
file=open("E:\\SOFTWARE\\TEXT1.txt","w")
file1=open("E:\\SOFTWARE\\MODTEXT1.txt","w")
for line in lines:
if 'a' in line :
file1.write(line)
else:
file.write(line)
print("Lines that contain a character is removed from TEXT1")
print("Lines that contain a character is added in MODTEXT1 ")
file.close()
file1.close()
>>>
======================= RESTART: E:/SOFTWARE/pratical12.py=====================
20

07/08/2020
AIM
WAP 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.7 32-bit)


#TO READ CHARACTERS FROM KEYBOARD ONE BY ONE, ALL LOWER CASE LETTERS STORED
INSIDE A FILE “LOWER”, ALL UPPERCASE LETTERS STORED INSIDE A FILE “UPPER”,& ALL
OTHER CHARACTERS STORED INSIDE “OTHERS”
f1=open("E:\\SOFTWARE\\LOWER.txt","w")
f2=open("E:\\SOFTWARE\\UPPER.txt","w")
f3=open("E:\\SOFTWARE\\OTHERS.txt","w")
while True:
c=input("Enter a single character = ")
if c=="~":
break
elif c.islower():
f1.write(c)
elif c.isupper():
f2.write(c)
else:
f3.write(c)
f1.close()
f2.close()
f3.close()
>>>
======================= RESTART: E:/SOFTWARE/pratical13.py=====================
21
22

08/08/2020
AIM
WAP 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.
SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)
23

09/08/2020
AIM
WAP to create a binary file with roll number, name and marks, input a roll number and update the
marks.
SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

__________________________________________________________________________
24

10/08/2020
AIM
WAP 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.7 32-bit)
25

11/08/2020
AIM
SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)
26

12/08/2020
AIM
WAP
SOFTWARE USED
IDLE (PYTHON 3.7 32-bit)

You might also like