G12CSPractical Manual
G12CSPractical Manual
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
EX.NO :1
METHODOLOGY :
The values for x and n are read as inputs and the sum of the 2 series are
computed. The program makes use of one user defined function to compute the factorial and
CODING:
def ntr(a,v):
return a**v
def denum(u):
fact=1
for i in range(1,u+1):
fact*=i
return fact
while True:
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t1.(x/3!)+(x**3/5!)+.....(x**(2n-1))/(2n+1)!')
print('\t\t\t2.(x/2!)-(x**3/4!)+.....(x**(2n-1)/(2n)!')
print('\t\t\t3.Exit')
if ch==1:
s=0
for i in range(1,2*n,2):
t=ntr(x,i)/denum(i+2)
s+=t
if i!=2*n-1:
print(x,'**',i,'/',i+2,'! + ',end='')
else:
print(x,'**',i,'/',i+2,'!')
elif ch==2:
s=0
mul=1
for i in range(1,2*n+1,2):
t=ntr(x,i)/denum(i+1)
s+=(t*mul)
mul*=-1
print(x,'**',i,'/',i+1,'! - ',end='')
elif i!=2*n-1:
print(x,'**',i,'/',i+1,'! + ',end='')
else:
print(x,'**',i,'/',i+1,'!')
else:
if c=='yes':
continue
else:
break
OUTPUT:
MENU
****
1.(x/3!)+(x**3/5!)+.....(x**(2n-1))/(2n+1)!
2.(x/2!)-(x**3/4!)+.....(x**(2n-1)/(2n)!
3.Exit
MENU
****
1.(x/3!)+(x**3/5!)+.....(x**(2n-1))/(2n+1)!
2.(x/2!)-(x**3/4!)+.....(x**(2n-1)/(2n)!
3.Exit
RESULT:
Sum of series has computed successfully
AIM :
METHODOLOGY:
A list of integers are read as input and passes as argument to a user defined function which
computes the mean, mode, median and returns all the three valueswhich is printed in the calling
function.
CODING:
#Statistics function
def mn(t):
return mean(t)
def med(t):
return median(t)
def md(t):
return mode(t)
while True:
m=[]
for i in range(n):
print('\t\t\t\tSTATISTICS FUNCTION')
print('\t\t\t\t************** ************')
a,b,c=mn(m),med(m),md(m)
if ch=='no':
break
OUTPUT:
STATISTICS FUNCTION
************** ************
The list is: [56, 45, 34, 89, 89, 89, 90]
RESULT:
A list of integers are read as input and passes as argument to a user defined function which computes
the mean, mode, median and returns all the three valueswhich is printed in the calling function has executed
successfully
To write user defined functions and test them with a menu driven code METHODOLOGY: The factorial
computation and generation of Fibonacci series is performedusing 2 user defined functions. A menu is displayed
and the user’s choice is accepted. Basedon the choice the appropriate function is executed.
METHODOLOGY:
MENU
1. Factorial of a number
3. Exit
CODING:
def factorial(n):
a=1
for i in range(1,n+1):
a*=i
def fibonacci(n):
if n>2:
a,b=0,1
print('\t\t\t',a,b,end=',')
for i in range(n-2):
c=a+b
a,b=b,c
print(c,end=',')
print()
elif n==1:
print('\t\t\t',0)
else:
print('\t\t\t',1)
while True:
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t1.Factorial of a number')
print('\t\t\t2.Fibonacci series')
print('\t\t\t3.Exit')
if ch==1:
factorial(num)
elif ch==2:
fibonacci(num)
else:
break
if d=='no':
print('\t\t\tMenu is no longer available!')
break
OUTPUT:
USER DEFINED FUNCTION-1
**** ******* **********
MENU
****
1.Factorial of a number
2.Fibonacci series
3.Exit
2.Fibonacci series
3.Exit
0 1,1,2,3,5,8,
RESULT:
The factorial computation and generation of Fibonacci series is performed using 2 user defined functions. A
menu is displayed and the user’s choice is accepted. Based on the choice the appropriate function has executed
successfully
METHODOLOGY:
The Binary Search operation on a list of integers in ascending order and
Reversing a String operation is performed using 2 user defined functions. A menu is displayed
and the user’s choice is accepted. Based on the choice the appropriate function is executed.
CODING:
def search(L2,b):
print('\t\t\tThe list is:',L2)
count=0
if b in L2:
for i in L2:
count+=1
if i==b:
break
else:
print('\t\t\tThe number does not exist in the list')
def reverse(temp):
s2=''
for i in range(-1,-len(temp)-1,-1):
s2+=temp[i]
print('\t\t\tOriginal string:',temp)
while True:
print('\t\t\tUSER DEFINED FUNCTION-2')
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t1.Linear search')
print('\t\t\t2.Reversing string')
print('\t\t\t3.Exit')
if ch==1:
L1=[]
for i in range(n):
search(L1,a)
elif ch==2:
reverse(s1)
else:
print('\t\t\tMenu is no longer available!')
break
if c=='no':
print('\t\t\tMenu is no longer available!')
break
OUTPUT:
USER DEFINED FUNCTION-2
**** ******* **********
MENU
****
1.Linear search
2.Reversing string
3.Exit
Enter your choice:1
2.Reversing string
3.Exit
Enter your choice:2
Enter the string:computer
RESULT:
The Binary Search operation on a list of integers in ascending order and Reversing a String operation is
performed using 2 user defined functions. A menu is displayedand the user’s choice is accepted. Based on the
choice the appropriate function has executed successfully
DATE :
AIM:
To find the largest, second largest and the prime numbers from a list of randomly
generated integers.
METHODOLOGY:
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
The lower and upper limits are read as inputs. Three user defined functions
are written to do the required operations namely i)to generate the random numbers
between the given limits ii) to find the largest, second largest number in the generated list
and iii) to store the prime numbers in a local list and print them else print a message stating
CODING:
def largest(t):
high=t[0]
for i in (t):
if i>=high:
high=i
return high
def slarge(t):
high=t[0]
shigh=t[1]
if shigh>=high:
high,shigh=shigh,high
for i in (t):
if i>high:
shigh=high
high=i
elif i==high:
high=i
elif i>shigh:
shigh=i
return shigh
def prime(t):
print('\t\t\t\tThe prime numbers are:',end='')
s=[]
for i in t:
c=0
if i!=1:
for j in range(2,i):
if i%j==0:
c+=1
if c==0:
s.append(i)
return s
m=[]
for i in range(n):
m.append(randint(x,y))
print('\t\t\t\tRANDOM GENERATOR')
print('\t\t\t\t********** ***************')
while True:
print('\t\t\t\t\tMENU')
print('\t\t\t\t\t*******')
print('\t\t\t\t1.Largest number')
print('\t\t\t\t3.Prime numbers')
print('\t\t\t\t4.Exit')
if ch==1:
print('\t\t\t\tThe largest number is:',largest(m))
elif ch==2:
print('\t\t\t\tThe 2nd largest number is:',slarge(m))
elif ch==3:
print(prime(m))
else:
print('\t\t\t\tMenu is no longer available!')
if c=='no':
print('\t\t\t\tMenu is no longer available!')
break
OUTPUT:
Enter the lower limit:3
Enter the upper limit:10
Enter the number of inputs:7
RANDOM GENERATOR
********** ***************
The list of integers from 3 to 10 are: [4, 10, 3, 3, 10, 9, 3]
MENU
*****
1.Largest number
2.2nd largest number
3.Prime numbers
4.Exit
Enter your choice:1
The largest number is: 10
Would you like to continue?(yes/no):yes
MENU
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
*******
1.Largest number
2.2nd largest number
3.Prime numbers
4.Exit
Enter your choice:2
The 2nd largest number is: 9
Would you like to continue?(yes/no):yes
MENU
*******
1.Largest number
3.Prime numbers
4.Exit
>>>
RESULT:
The prime numbers from a list of randomly generated and the largest, second largest number has
found successfully
AIM :
METHODOLOGY:
The menu options are displayed. As per the user’s choice, within each of the menu options, a tuple is
read as input and the appropriate functions are called. The first menu option, reads a tuple of ‘n’ words, calls a
function that takes the tuple as parameter which checks if each word has all the 5 vowels in any case and
returns a tuple of such words. The second menu option reads a nested tuple having height, weight of ‘n’
persons, calls a function that takes as parameter, each element of the nested tuple, which calculates the BMI
and returns both the BMI and the result as OBESE /OVERWEIGHT/NORMAL/UNDERWEIGHT. The returned
values are displayed in the main function for each person.
MENU
iii) Exit
>25 Overweight
18.5 – 25 Normal.
<18.5 – Underweight
CODING:
def tup(b):
for i in b:
if 'a' in i and 'e' in i and 'i' in i and 'o' in i and 'u' in i:
print(i,end=',')
def bmi(c,d):
for i in range(d):
print('\n\t',c[i][0],end='')
print('\t\t',c[i][1],'kg',end='')
print('\t\t',c[i][2],'m',end='')
print('\t\t',(c[i][1])/((c[i][2])**2),end='')
while True:
print('\t\t\t\tMENU')
print('\t\t\t\t*******')
print('\t\t\t\t1.Vowels')
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
print('\t\t\t\t2.BMI Calculation')
print('\t\t\t\t3.Exit')
ch=int(input('Enter the serial number:'))
if ch==1:
t=()
n=int(input('Enter the number of words:'))
for i in range(n):
a=input('Enter the word:')
t+=(a,)
print('\t\t\tThe tuple is:',t)
print('\t\t\tThe words with all 5 vowels are:',end=' ' )
tup(t)
elif ch==2:
n=int(input('Enter the number of people:'))
t2=()
for i in range(n):
t1=()
name=input('Enter the name:')
w=float(input('Enter the weight in kg:'))
h=float(input('Enter the height in metres:'))
t1+=(name,w,h)
t2+=(t1,)
print('\tNAME\t\tWEIGHT\t\tHEIGHT\t\tBMI')
print('\t*******\t\t**********\t\t*********\t\t******')
bmi(t2,n)
else:
print('\t\t\t\tMenu is no longer available!')
z=input('\nWould you like to continue?(yes/no):')
if z=='no':
print('\t\t\t\tMenu is no longer available!')
break
OUTPUT:
MENU
*******
1.Vowels
2.BMI Calculation
3.Exit
Enter the serial number:1
Enter the number of words:4
Enter the word:raji
Enter the word:vino
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
MENU
*****
1.Vowels
2.BMI Calculation
3.Exit
Enter the serial number:2
Enter the number of people:2
Enter the name:sasi
Enter the weight in kg:50
Enter the height in metres:1.7
Enter the name:sangee
Enter the weight in kg:65
Enter the height in metres:1.8
RESULT:
perform the menu options on a tuple of values read as input has executed successfully
AIM:
To create a dictionary to store details of ‘n’ subscribers and perform the menu operations.
METHODOLOGY:
The details of ‘n’ subscribers namely name and phone number are read and stored in a dictionary.
Using a menu and user’s choice, the operations like Add a subscriber detail, View all subscribers, Modify name
of a given subscriber, Delete an existing subscriber are done using user defined functions.
Note: The menu to be display should be:
MENU
1. ADD
2. VIEW
3. MODIFY NAME
4. DELETE
5. EXIT
Creating the dictionary should be done only by calling option ADD repeatedly,
Modify and Delete, the input taken is the phone number. If the number does not exist in the
List , appropriate message should be shown
CODING:
import json
def add(a):
for i in range(n):
a[ph]=name
def display(a):
n=int(input('Enter the phone number:'))
if n in a:
print('\t\t\tDisplay successful')
else:
def modify(a):
if ph in a:
det=input('Modified detail:')
a[ph]=det
else:
def delete(a):
if ph in a:
print('\t\t\tDeletion successful!')
del a[ph]
else:
while True:
d={}
for i in range(n):
d[ph]=name
print(json.dumps(d,indent=65))
while True:
print('\t\t\t\tMENU\n\t\t\t\t****')
print('\t\t\t\t2.View subscriber')
print('\t\t\t\t3.Modify')
print('\t\t\t\t4.Delete')
print('\t\t\t\t5.Exit')
if ch==1:
add(d)
print(json.dumps(d,indent=65))
elif ch==2:
display(d)
elif ch==3:
modify(d)
print(json.dumps(d,indent=65))
elif ch==4:
delete(d)
print(json.dumps(d,indent=65))
else:
break
if t=='no':
break
if tt=='no':
break
OUTPUT:
MENU
****
1.Add subscriber details
2.View subscriber
3.Modify
4.Delete
5.Exit
MENU
****
1.Add subscriber details
2.View subscriber
3.Modify
4.Delete
5.Exit
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
RESULT:
create a dictionary to store details of ‘n’ subscribers and perform the menu operations has executed
successfully
AIM:
To create a nested dictionary and manipulate the same.
METHODOLOGY:
A nested dictionary is created having the main keys to be 3 categories namely SENIORS, JUNIORS,
SUBJUNIORS. For each of these keys, an inner dictionary is created with keys as BHARATHI, TAGORE, SHIVAJI,
PRATAP and values as the score for that house. The code makes use of an user defined function MAX_SCORE()
that takes the dictionary as parameter and displays the house having maximum score for each category.
Note: Strictly follow the uppercase for all the keys as mentioned above
CODING:
#User defined function-nested dictionary
def high(d):
for i in range(3):
b=t[i]
high=d[b]['Nalanda']
for i in range(4):
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
if d[b][h[i]]>=high:
high=d[b][h[i]]
a=h[i]
print('\t\t\t\tThe winner of ',b,' category is', a)
md={}
h=('Nalanda','Thakshashila','Shantiniketan','Kalakshetra')
t=('Sub juniors','Juniors','Seniors')
for i in range(3):
td={}
print(t[i],'Category')
td['Nalanda']=int(input('Enter Nalanda score :'))
td['Thakshashila']=int(input('Enter Thakshashila score:'))
td['Shantiniketan']=int(input('Enter Shantiniketan score :'))
td['Kalakshetra']=int(input('Enter Kalakshetra score:'))
md[t[i]]=td
print('\t\t\t\tUSER DEFINED FUNCTION-NESTED DICTIONARY')
print('\t\t\t\t******* *********** ************* **************************')
print('\t\t\t\tThe Original dictionary is:\n',md)
high(md)
OUTPUT:
Juniors Category
Seniors Category
{'Sub juniors': {'Nalanda': 67, 'Thakshashila': 78, 'Shantiniketan': 90, 'Kalakshetra': 60}, 'Juniors': {'Nalanda': 75,
'Thakshashila': 68, 'Shantiniketan': 70, 'Kalakshetra': 98}, 'Seniors': {'Nalanda': 80, 'Thakshashila': 87,
'Shantiniketan': 81, 'Kalakshetra': 72}}
RESULT:
Create a nested dictionary makes use of an user defined function MAX_SCORE() that takes the
dictionary as parameter and displays the house having maximum score for each category has executed
successfully
AIM:
To create an user defined module NUMBER to include 2 user defined functions PALINDROME(),
SPECIAL() and import this module in another python code and execute the functions
METHODOLOGY:
A module NUMBER is created to include the 2 functions namely PALINDROME() which takes a number
as parameter and returns 1 if it’s a palindrome and -1 otherwise; SPECIAL() which takes a number as parameter
and returns 1 if it’s a special number and -1 otherwise.This module is imported in another python code and
both the functions are executed. For the PALINDROME() function, a tuple of integers are read and the code
displays all the palindrome numbers in the tuple. If there weren’t any palindrome numbers appropriate
message is shown. For the SPECIAL() function 2 limits are read and all the special numbers between these limits
are generated. If there were no special numbers, appropriate message is shown.
Note: Create a module NUMBER to include the functions, with each function to include docstrings to describe
the function. Also write a python code to import this module and use the two functions for inputs from the
user.
i) palindrome() to take as parameter a number and returns 1 if the number is a palindrome and –1 otherwise.
This function to be tested with a tuple of integers
ii) special() – takes as parameter a number and returns 1 if it’s a special number and -1 otherwise. [ A special
number is a number equal to the sum of the factorial of the individual digits of the number] This function to be
tested to generate list of special numbers between two limits accepted from the user.
CODING:
import Number as N
while True:
n=int(input('Enter the number of elements:'))
t=()
for i in range(n):
t+=(int(input('Enter the number:')),)
while True:
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t 3.Exit')
if ch==1:
for i in t:
N.palindrome(i)
elif ch==2:
for i in t:
N.special(i)
else:
break
if a=='no':
break
if b=='no':
break
OUTPUT:
Enter the number of elements:2
MENU
****
1.Palindrome check
2.Special Number
3.Exit
Reverse of it is = 54
45 is Not palindrome
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
Reverse of it is = 676
676 is a Palindrome
MENU
****
1.Palindrome check
2.Special Number
3.Exit
it is not special no
it is not special no
>>>
RESULT:
create an user defined module NUMBER to include 2 user defined functions PALINDROME(), SPECIAL()
and import this module in another python code and execute the functions successfully
To create an user defined module MATRIX to include 2 user defined functions SWAP (), SORTROWCOL()
and import this module in another python code and execute the functions
METHODOLOGY:
A module MATRIX is created to include the 2 functions namely SWAP() which takes a nested list of
integers as parameter and swaps the main and secondary diagonal elements; SORTROWCOL() which takes a
nested list of integers as parameter and sorts each row in ascending order using Bubble sort followed by each
column in ascending order using Insertion sort. This module is imported in another python code and both the
functions are executed with the outputs displayed in the main function.
Note: Create a module MATRIX to include the functions, with each function to include
docstrings to describe the function. Also write a python code to import this module and use the
two functions for inputs from the user.
i) swap() to take as parameter a nested list and swaps the left and right diagonal elements. This
function to be tested with a nested list of integers
ii) sortrowcol() – takes as parameter a nested list of integers and sorts the rows using bubble sort in ascending
order and sorts columns using Insertion sort in ascending order. This function to be tested with a nested list of
integers.Both the outputs to be shown in the main function
CODING:
import Matrixfunction as mf
while True:
m1=[]
for i in range(r):
m2=[]
for j in range(c):
m1.append(m2)
for i in range(r):
print('\t\t\t',end='')
for j in range(c):
print(m1[i][j],end='\t')
print()
print('\t\t\t\tMENU\n\t\t\t\t****')
print('\t\t\t\t1.Swap')
print('\t\t\t\t2.Row sort')
print('\t\t\t\t3.Column sort')
print('\t\t\t\t4.Exit')
if ch==1:
mf.swap(m1)
elif ch==2:
mf.rowsort(m1)
elif ch==3:
mf.columnsort(m1)
else:
break
for i in range(r):
print('\t\t\t',end='')
for j in range(c):
print(m1[i][j],end='\t')
print()
if a=='no':
break
OUTPUT:
34 45
56 67
MENU
****
1.Swap
2.Row sort
3.Column sort
4.Exit
45 34
67 56
12 15
10 11
MENU
****
1.Swap
2.Row sort
3.Column sort
4.Exit
11 11
12 15
10 32
12 20
MENU
****
1.Swap
2.Row sort
3.Column sort
4.Exit
10 10
20 20
>>>
RESULT:
Create an user defined module MATRIX to include 2 user defined functions SWAP (), SORTROWCOL() and
import this module in another python code and execute the functions successfully
2. Copy the words containing ‘U’ into another file and display the new file
3. Convert the case of the letters(lower to upper and vice versa) in the original text file and
display the contents
4. Exit
CODING:
#text file-1
def lines():
f=open('Text.txt')
L2=f.readlines()
try:
except:
f.close()
def copy():
f=open('Text.txt')
f2=open('Copytext.txt','w')
try:
for i in f.readlines():
s=i.split(' ')
for j in s:
f2.write(j+'\t')
f2.write(j[:-1:]+'\t')
f2.close()
except:
f.close()
def convert():
f=open('Text.txt','r+')
try:
s=''
for i in f.read():
if i.isupper()==True:
s+=i.lower()
elif i.islower()==True:
s+=i.upper()
else:
s+=i
f.seek(0)
f.write(s)
except:
f.close()
f=open('Text.txt','w')
L1=s.split('.')
for i in L1:
a=i+'\n'
f.write(a)
f.close()
while True:
print('\t\t\t\tTEXT FILE-1')
print('\t\t\t\t**** ******')
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t4.Exit')
if ch==1:
lines()
elif ch==2:
copy()
elif ch==3:
convert()
else:
break
if t=='no':
break
OUTPUT:
TEXT FILE-1
**** ******
MENU
****
4.Exit
TEXT FILE-1
**** ******
MENU
****
4.Exit
['YOU\t']
TEXT FILE-1
**** ******
MENU
****
4.Exit
21
TEXT FILE-1
**** ******
MENU
****
4.Exit
>>>
RESULT:
Create a text file with contents entered by the user as long as the user wishes to and
perform the operations in the menu according to the user’s choice has executed successfully
AIM:
To create a text file with ‘N’ lines of text and perform the operations in the menu
METHODOLOGY:
A text file is created by ‘N’ lines of text. A menu is then displayed and the
related operations are performed on the text file, depending on user’s choice.
MENU
4. Exit
CODING:
def count():
f=open('Text-2')
try:
count=0
while True:
a=f.readline()
if not a:
break
else:
L=a.split()
for i in L:
if i.isalnum()==True:
count+=1
f.close()
except:
print('\t\t\tError in opening file')
def palindrome():
f=open('Text-2')
try:
print('\t\t\tPalindromes:',end='')
c=0
while True:
a=f.readline()
if not a:
break
else:
L=a.split()
for i in L:
if i==i[-1::-1] and i.isalnum()==True:
print(i,end=',')
c+=1
if c==0:
print('None')
f.close()
except:
print('\t\t\tError in opening file')
def vowel():
f=open('Text-2')
try:
print('\t\tWords starting and ending with vowels:',end='')
c=0
while True:
a=f.readline()
if not a:
break
else:
L=a.split()
for i in L:
print(i,end=',')
c+=1
if c==0:
print('None')
f.close()
except:
print('\t\t\tError in opening file')
f=open('Text-2','w')
for i in range(n):
s=input('Enter the text:')
f.write(s+'\n')
f.close()
while True:
print('\n\t\t\t\tTEXT FILE-2')
print('\t\t\t\t**** ******')
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t2.Display palindromes')
print('\t\t\t4.Exit')
if ch==1:
count()
elif ch==2:
palindrome()
elif ch==3:
vowel()
else:
print('\t\t\tMenu is no longer available!')
break
if b=='no':
print('\t\t\tMenu is no longer available!')
break
OUTPUT:
TEXT FILE-2
**** ******
MENU
****
2.Display palindromes
4.Exit
Enter your choice:1
TEXT FILE-2
**** ******
MENU
****
2.Display palindromes
4.Exit
Enter your choice:2
Palindromes:None
None
Would you like to continue?(yes/no):yes
TEXT FILE-2
**** ******
MENU
****
2.Display palindromes
4.Exit
Enter your choice:3
None
Would you like to continue?(yes/no):no
Menu is no longer available!
>>>
RESULT:
Create a text file with ‘N’ lines of text and perform the operations in the menu according to the user’s
choice has executed successfully
AIM :
roll number, name, stream, total and perform the operations using a menu driven code.
METHODOLOGY:
The binary file is created to store details of ‘N’ students, with input read in the form of a list. The menu
is displayed and the related operations are performed on the binary file depending on user’s choice.
Note: The menu options to be as follows:
MENU
2. Increment the total by 3 marks for students in the biology stream and decrement by 2for
students in EG stream.
3. Exit
CODING:
#Binary file-1
import pickle
def display():
f=open('STUDENT.DAT','rb')
try:
high=pickle.load(f)['Total']
while True:
temp=pickle.load(f)
if not temp:
break
else:
if temp['Total']>high:
high=temp['Total']
a=temp
except EOFError:
f.close()
print('\t\tTopper details:',a)
def mark_edit():
f=open('STUDENT.DAT','rb+')
try:
while True:
pos=f.tell()
temp=pickle.load(f)
if not temp:
break
else:
if temp['Stream']=='Computer Science':
temp['Total']+=3
elif temp['Stream']=='Biology':
temp['Total']-=2
f.seek(pos)
pickle.dump(temp,f)
except EOFError:
f.close()
f=open('STUDENT.DAT','wb+')
for i in range(n):
d={}
d['Name']=input('Enter name:')
d['Stream']=input('Enter stream:')
d['Total']=int(input('Enter total:'))
pickle.dump(d,f)
f.close()
while True:
print('\t\t\t\tBINARY FILE-1')
print('\t\t\t\t****** ******')
print('\t\t\t\t MENU')
print('\t\t\t\t ****')
print('\t\t\t3.Exit')
ch=int(input('Enter choice:'))
if ch==1:
display()
elif ch==2:
mark_edit()
else:
print('\t\t\tMenu is no longer available!')
break
if s=='no':
break
OUTPUT:
Enter name:HARI
Enter total:478
Enter name:LATHA
Enter stream:Biology
Enter total:490
BINARY FILE-1
****** ******
MENU
****
3.Exit
Enter choice:1
Topper details: {'Roll': 1202, 'Name': 'LATHA', 'Stream': 'Biology', 'Total': 490}
BINARY FILE-1
****** ******
MENU
****
3.Exit
Enter choice:2
BINARY FILE-1
****** ******
MENU
****
3.Exit
Enter choice:1
Topper details: {'Roll': 1202, 'Name': 'LATHA', 'Stream': 'Biology', 'Total': 488}
RESULT:
Create a Binary file with each record having details of roll number, name, stream, total and perform the
operations using a menu driven code has executed successfully
AIM :
To create a Binary file EMPLOYEE.DAT with each record having fields Name, Age,
Department, Designation, Salary and perform the operations using a menu driven code.
METHODOLOGY:
The binary file is created to store details of ‘N’ employees, with input read
in the form of a dictionary. The menu is displayed and the related operations are performed
on the binary file depending on user’s choice.
MENU
1. Display details of Managers earning more than 50000 in Finance and in Admin Dept.
2. Delete the employee details who have reached retirement age (58 years)
3. Exit
CODING:
#Binary file-2
import pickle
f=open('EMPLOYEE.DAT',wb)
def display():
f=open('EMPLOYEE.DAT',rb)
try:
L2=[]
while True:
L1=pickle.load(f)
if not L:
break
else:
if L1[2]=='Finance' or L1[2]=='Admin':
if L1[-1]>50000:
L2.append(L1)
except EOFError:
f.close()
print('
for i in range n:
L=[]
L.append(input('Enter name:'))
L.append(int(input('Enter age:')))
L.append(input('Enter department:'))
L.append(input('Enter designation:'))
L.append(int(input('Enter salary:')))
pickle.load(L,f)
while True:
print('\t\t\tBINARY FILE-2')
print('\t\t\t****** ******')
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t1.Display details')
print('\t\t\t2.Delete details')
print('\t\t\t3.Exit')
if ch==1:
display()
elif ch==2:
delete()
else:
break
if t=='no':
break
output:
Menu
1. Add Record
2. Display Records
3. delete a Record
4. Exit
Enter choice(1-4): 3
Menu
1. Add Record
2. Display Records
3. delete a Record
4. Exit
Enter choice(1-4): 2
BEFORE DELETION
***************
AFTER DELETION
***************
Menu
1. Add Record
2. Display Records
3. delete a Record
4. Exit
>>>
RESULT:
The binary file is created to store details of ‘N’ employees, with input read in the form of a dictionary. The
menu is displayed and the related operations are performed on the binary file depending on user’s choice has
executed successfully
AIM:
METHODOLOGY:
CATEGORY GST_PERCENTAGE
Automobiles 25
Stationary 12
Chocolates 10
Dairy 3
A second file ITEMS.CSV is created to have fields ItemID, Name, Category, Unitprice, Finalprice. The input for ‘N’
items are read from the user for the first 4 fields. The Finalprice is computed using the GST.CSV file retrieving
the GST% as per the category. The contents of the completed ITEMS.CSV is then displayed
Note: Make sure the Category you input for ITEMS.CSV is one of the Category in GST.CSV
CODING:
import csv
def gst():
f=open('GST.csv','w',newline='')
w=csv.writer(f)
w.writerows([['CATEGORY','GST'],['********','***']])
for i in range(n):
L=[]
L.append(int(input('Enter GST:')))
w.writerow(L)
f.close()
items(n)
def items(n):
f=open('ITEMS.csv','w',newline='')
w=csv.writer(f)
for i in range(n):
L=[]
w.writerow(L)
f.close()
Final()
def Final():
f = open('Final.csv','w',newline = '')
f1 = open('GST.csv')
f2 = open('ITEMS.csv')
w = csv.writer(f)
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
r1 = csv.reader (f1)
r2 = csv.reader(f2)
c=0
for i in r2:
f1.seek(0)
c+=1
if c>2:
for j in r1:
if i[2] in j:
L=[]
L.extend(i)
L.append(j[-1])
t=int(L[3])+int(L[3])*int(L[4])/100
L.append(t)
w.writerow(L)
f.close()
f1.close()
f2.close()
display()
def display():
f=open('GST.csv')
r=csv.reader(f)
for i in r:
for j in i:
print(j,end='\t')
print('\n\t\t')
f.close()
f=open('ITEMS.csv')
r=csv.reader(f)
for i in r:
for j in i:
print(j,end='\t')
print('\n\t\t')
f.close()
f=open('FINAL.csv')
r=csv.reader(f)
for i in r:
for j in i:
print(j,end='\t')
print('\n\t\t')
f.close()
gst()
OUTPUT:
Enter GST:12
Enter GST:18
Enter GST:21
CATEGORY GST
****** ***
soap 12
mobile 18
laptop 21
1 soap soap 56
2 HP laptop 18000
>>>
RESULT:
Create CSV files and manipulate its contents has executed successfully
AIM :
To implement a Stack using a list of integers and performing the related operations using user defined
functions.
METHODOLOGY:
The Stack operations namely Push, Pop, Peek, Display Stack Status are performed on a list of integers
using user defined functions
Note: The menu options to be displayed:
MENU
1. Push
2. Pop
3. Display Stack
4. Peek
5. Exit
CODING:
#stack-1
stack=[]
def push(stack):
ch='yes'
while ch=='yes':
if len(stack)>=n:
print('\t\t\t\tStack overflow!\n\t\t\tCannot push anymore elements!')
break
else:
stack.append(int(input('Enter the element:')))
return stack
def pop(stack):
ch='yes'
while ch=='yes':
if len(stack)==0:
print('\t\t\t\tStack underflow!\n\t\t\tCannot pop anymore elements!')
break
else:
stack.pop()
return stack
def peek(stack):
if len(stack)==0:
print('\t\t\tStack is empty!')
else:
print('\t\t\tThe topmost element in the stack is',stack[-1])
def display(stack):
if len(stack)==0:
print('\t\t\tStack is empty!')
else:
L=[]
while True:
if len(stack)==0:
break
else:
L.append(stack.pop())
print('\t\t\t',end='')
for i in L:
print(i,end=',')
print()
return L
while True:
print('\t\t\tSTACK OPERATION-1')
print('\t\t\t***** ***********')
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t\t1.Push')
print('\t\t\t\t2.Pop')
print('\t\t\t\t3.Peek')
print('\t\t\t\t4.Display')
print('\t\t\t\t5.Exit')
if ch==1:
n=int(input('Enter stack capacity:'))
stack=push(stack)
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
elif ch==2:
stack=pop(stack)
elif ch==3:
peek(stack)
elif ch==4:
stack=display(stack)
else:
print('\t\t\tMenu is no longer available!')
break
if s=='no':
print('\t\t\tMenu is no longer available!')
break
output:
STACK OPERATION-1
***** ***********
MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
STACK OPERATION-1
***** ***********
MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
STACK OPERATION-1
***** ***********
MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice:3
STACK OPERATION-1
***** ***********
MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
890,678,456,
>>>
RESULT:
Implement a Stack using a list of integers and performing the related operations using user defined
functions has executed successfully
AIM :
To implement a stack using a list where each element in the list has Country name and
Capital. The related operations are performed using user defined functions.
METHODOLOGY:
The stack operations namely Insert, Delete, Peek, Display Stack Status are
MENU
1. Insert
2. Delete
3. Display Stack
4. Peek
5. Exit
CODING:
#stack-2
stack=[]
def push(stack):
ch='yes'
while ch=='yes':
if len(stack)>=n:
break
else:
country=input('Enter country:')
capital=input('Enter capital:')
stack.append([country,capital])
return stack
def pop(stack):
ch='yes'
while ch=='yes':
if len(stack)==0:
break
else:
stack.pop()
return stack
def peek(stack):
if len(stack)==0:
print('\t\t\tStack is empty!')
else:
def display(stack):
if len(stack)==0:
print('\t\t\tStack is empty!')
else:
L=[]
r=stack
while True:
if len(stack)==0:
break
else:
L.append(stack.pop())
print('\t\t\t',end='')
for i in L:
print(i,end=',')
print()
return r
while True:
print('\t\t\tSTACK OPERATION-2')
print('\t\t\t***** ***********')
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t\t\t1.Push')
print('\t\t\t\t2.Pop')
print('\t\t\t\t3.Peek')
print('\t\t\t\t4.Display')
print('\t\t\t\t5.Exit')
if ch==1:
stack=push(stack)
elif ch==2:
stack=pop(stack)
elif ch==3:
peek(stack)
elif ch==4:
stack=display(stack)
else:
break
if s=='no':
break
OUTPUT:
STACK OPERATION-2
***** ***********
MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice:1
Enter country:INDIA
Enter country:ENGLAND
Enter capital:LONDON
Enter country:USA
Enter capital:WASHINGTON
STACK OPERATION-2
***** ***********
MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
STACK OPERATION-2
***** ***********
MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
STACK OPERATION-2
***** ***********
MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
>>>
RESULT:
Implement a stack using a list where each element in the list has Country name and Capital. The related
operations are performed using user defined functions has executed successfully
METHODOLOGY:
The mysql.connector module is imported and the interface between Python and MySQL is created.
Using the DDL command CREATE TABLE, a student table structure is created. Using DML commands INSERT,
SELECT, UPDATE the operations are performed to obtain the desired outputs.
Note:
Create a table STUDENT using python interface having atleast 5 records with the following attributes:
ROLL, NAME, TOTAL, COURSE, DOB. Also apply constraints PRIMARY KEY for ROLL and CHECK for COURSE to
take values only from ‘CS’, ‘BIOLOGY’, ‘COMMERCE’, ‘EG’. Also write queries to perform the following
operations:
A. Find maximum total marks, minimum total marks and number of students in each course where there at
least 2 students.
B. Display details of students born in the month of may 2003 who have scored total between 100 to 200.
D. Increase total marks for CS students by 5 %, for those, whose total is less than 180.
CODING:
import mysql.connector as mc
def course():
cr.execute('SELECT course,max(total),min(total),count(course) FROM Student GROUP BY course HAVING
count(course)>1')
print('\t\tCourse\t\tMax\t\tMin\t\tNumber')
print('\t\t******\t\t***\t\t***\t\t******')
for i in cr:
for j in range(len(i)):
print('\t\t',i[j],end='')
print()
def display():
cr.execute('SELECT * FROM Student WHERE DOB LIKE "2003-05-__" and total BETWEEN 100 AND 200')
print('\t\tRoll\t\tName\t\tTotal\t\tCourse\t\tDOB')
print('\t\t****\t\t****\t\t*****\t\t******\t\t***')
for i in cr:
for j in range(len(i)):
print('\t\t',i[j],end='')
print()
def sort():
cr.execute('SELECT * FROM Student order by total desc')
print('\t\tRoll\t\tName\t\tTotal\t\tCourse\t\tDOB')
print('\t\t****\t\t****\t\t*****\t\t******\t\t***')
for i in cr:
for j in range(len(i)):
print('\t\t',i[j],end='')
print()
def increment():
print('\t\tRoll\t\tName\t\tTotal\t\tCourse\t\tDOB')
print('\t\t****\t\t****\t\t*****\t\t******\t\t***')
for i in cr:
for j in i:
print('\t\t',j,end='')
print()
cobj=mc.connect(host='localhost',user='root',passwd='',port=3306,database='school')
cr=cobj.cursor()
name varchar(15),\
total int,\
course varchar(15),\
DOB date)')'''
for i in range(n):
name=input('Enter name:')
total=int(input('Enter total:'))
course=input('Enter course:')
date=input('Enter date:')
if course in ('CSC','Bio','Commerce','EG'):
cobj.commit()
else:
continue''
while True:
print('\t\tPYTHON MYSQL INTERFACE-1')
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t2.Display')
print('\t\t3.Sorting total')
print('\t\t5.Exit')
if ch==1:
course()
elif ch==2:
display()
elif ch==3:
sort()
elif ch==4:
increment()
else:
break
if t=='no':
break
cr.close()
cobj.close()
OUTPUT:
PYTHON MYSQL INTERFACE-1
****** ***** ***********
MENU
****
1.Max and Min
2.Display
3.Sorting total
5.Exit
Enter your choice1
CS 560 490 2
2.Display
3.Sorting total
5.Exit
MENU
****
2.Display
3.Sorting total
5.Exit
MENU
****
1.Max and Min
2.Display
3.Sorting total
5.Exit
MENU
****
2.Display
3.Sorting total
5.Exit
>>>
RESULT:
Create a connectivity and provide an interface between Python and MySQL to perform queries using DML
commands has executed successfully
AIM:
To create a connectivity and provide an interface between Python, MySQL and create a CSV file using
the contents of the table in MySQL.
METHODOLOGY:
The mysql.connector module is imported and the interface between Python and MySQL is created. The
csv module is imported to use the functions for creation and retrieval of the CSV file. The records of the
Student table are written into the Student.CSV and the contents are displayed in an appropriate format.
Note:
Using the STUDENT table created in the earlier code, transfer all the records into a Student.CSV and perform
the following operations:
i) Display the contents from the CSV file, showing values in each record separated by a comma
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
ii) Read from the CSV file and display the name of the top scorer.
CODING:
import mysql.connector as mc
cobj=mc.connect(host='localhost',user='root',passwd='',port=3306,database='jobinfo')
cr=cobj.cursor()
Name varchar(15),\
Designation varchar(15),\
Salary int)')
Dept_name varchar(15),\
Location varchar(15))')
for i in range(n):
name=input('Enter name:')
des=input('Enter designation:')
sal=int(input('Enter salary:'))
dept=input('Enter department:')
loc=input('Enter location:')
cobj.commit()
cobj.commit()'''
print('\n')
print('\t\t\tEMPLOYEE')
print('\t\t\t********')
print('\t\tEmp_no\t\tName\t\tDesignation\t\tDept_no\t\tSalary')
print('\t\t******\t\t****\t\t***********\t\t*******\t\t******')
for i in cr:
for j in i:
print('\t\t',j,end='')
print()
print('\n')
print('\t\t\tDEPARTMENT')
print('\t\t\t**********')
print('\t\tDept_no\t\tDept_name\t\tLocation')
print('\t\t*******\t\t*********\t\t********')
for i in cr:
for j in i:
print('\t\t',j,end='')
print()
print('\n')
print('\t\t\tManager details')
print('\t\t\t******* *******')
for i in cr:
print('\t\t ',end='')
for j in i:
print(j,end='\t\t ')
print()
cr.close()
cobj.close()
output:
EMPLOYEE
********
DEPARTMENT
**********
11110 Admin CH
Manager details
******* *******
>>>
RESULT:
Create a connectivity and provide an interface between Python, MySQL and create a CSV file using the
contents of the table in MySQL has executed successfully
AIM:
To create a connectivity and provide an interface between Python and MySQL to
perform join operations on 2 tables
METHODOLOGY:
The mysql.connector module is imported and the interface between Python and MySQL is created.
Using the DDL, DML commands the Employee and Department tables are created. A query is then executed to
join the tables and display the output in the desired format
Note:
Create Employee table (Empno, Name, Desig, Deptno, Salary) and Department table(Deptno, Dname, Location)
with appropriate number of records. Execute a query to display details of all Mangers like Empno, Name,Salary,
Dname and Location.
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
CODING:
#python mysql interface-csv
import mysql.connector as mc
import csv
cobj=mc.connect(host='localhost',user='root',passwd='',port=3306,database='school')
cr=cobj.cursor()
def content():
f=open('Student.csv','r',newline='')
r=csv.reader(f)
for i in r:
print('\t\t',end='')
for j in i:
print(j,end='\t\t')
print()
f.close()
def top():
f=open('Student.csv','r',newline='')
r=csv.reader(f)
c=0
for i in r:
if c==0:
c=1
continue
else:
top=str(i[2])
break
c=0
f.seek(0)
for i in r:
if c==0:
c=1
continue
else:
if str(i[2])>=top:
top=str(i[2])
f.seek(0)
for i in r:
if str(i[2])==top:
print(i[1],end=',')
f.close()
f=open('Student.csv','w',newline='')
w=csv.writer(f)
w.writerow(['ROLL','NAME','TOTAL','COURSE','DOB'])
for i in cr:
w.writerow(i)
f.close()
while True:
print('\t\tPYTHON MySQL INTERFACE-CSV')
print('\t\t\t\tMENU')
print('\t\t\t\t****')
print('\t\t 3.Exit')
if ch==1:
content()
elif ch==2:
top()
else:
print('\t\t\ Menu is no longer available!')
break
if t=='no':
print('\t\t\ Menu is no longer available!')
break
cr.close()
cobj.close()
output:
PYTHON MySQL INTERFACE-CSV
****** ***** ********* ***
MENU
****
1.Display contents
3.Exit
3.Exit
MENU
****
1.Display contents
3.Exit
>>>
RESULT:
Create a connectivity and provide an interface between Python and MySQL to perform join operations
on 2 tables has executed successfully
AIM:
To execute queries using DDL commands and display the result.
METHODOLOGY:
QUERIES:
USE SCHOOL;
CREATE TABLE STUDENT (RNO INT (5), NAME VARCHAR (15), PLACE VARCHAR (20), MARK int (3));
DESC STUDENT;
OUTPUT:
1) Query OK , 1 row affected (0.00 sec)
Database changed
3)
RESULT:
Mentioned database creation, Table creation, ALTER the structure of table has executed successfully
using DDL commands.
DATE:
AIM:
To execute queries using DML commands and display the result.
METHODOLOGY:
QUERIES:
1) Add 5 records into STUDENT table
OUTPUT:
2)
3)
4)
RESULT:
Insert records, update value in record, delete records, display records have executed successfully
DATE:
AIM:
METHODOLOGY:
Open mysql and apply operators and functions on a table
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601
QUERIES:
OUTPUT:
1)
2)
3)
4)
RESULT:
METHODOLOGY:
Open mysql and apply SQL commands on two tables
QUERIES:
Consider the tables SPORTS and CLUB given below:
Table: SPORTS
S_NO S_NAME FEES NO_OF_DAYS TRAINING_TYPE
Table: CLUB
S_NO CLUB_COACH LOCATION GENDER
(iii) Display the average of sports fees whose sports name contains letter ‘t’.
Select avg(fees) from Sports where S_Name like '%t%';
(iv) Display sports name, fees, coach name and location of male coach from the tables SPORTS and CLUB.
Select S_Name, Fees, Club_Coach, Location from Sports S, Club C where gender ="Male" and S.SNO = C.SNO;
RESULT:
SQL commands applied on two tables and executed output successfully.