0% found this document useful (0 votes)
11 views91 pages

G12CSPractical Manual

The document outlines a series of exercises conducted at Don Bosco School of Excellence, focusing on programming tasks such as computing series sums, statistical functions, and implementing menu-driven codes for various mathematical operations. Each exercise includes the aim, methodology, coding examples, and outputs demonstrating the functionality of the programs. The overall goal is to enhance students' understanding of programming concepts through practical applications.

Uploaded by

Yugendhar
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)
11 views91 pages

G12CSPractical Manual

The document outlines a series of exercises conducted at Don Bosco School of Excellence, focusing on programming tasks such as computing series sums, statistical functions, and implementing menu-driven codes for various mathematical operations. Each exercise includes the aim, methodology, coding examples, and outputs demonstrating the functionality of the programs. The overall goal is to enhance students' understanding of programming concepts through practical applications.

Uploaded by

Yugendhar
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/ 91

DON BOSCO

SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

EX.NO :1

DATE : SUMMING THE SERIES


AIM:

To compute the sum of the following series :

i) x 1/ 2! – x 3 / 4 ! + x 5 / 6 ! -x 7 / 8! + ……. ‘n’ terms

ii) x 1/ 3! – x 3 / 5 ! + x 5 / 7 ! -x 7 / 9! + ……. ‘n’ terms

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

another to compute the powers of x.

CODING:

#factorial summation series

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')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

ch=int(input('Enter the serial no.:'))

x=int(input('Enter the value of x:'))

n=int(input('Enter the value of n:'))

if ch==1:

s=0

print('\t\t\tThe series is:',end=' ')

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,'!')

print('\t\t\tThe sum of series is:',s)

elif ch==2:

s=0

mul=1

print('\t\t\tThe series is:',end=' ')

for i in range(1,2*n+1,2):

t=ntr(x,i)/denum(i+1)

s+=(t*mul)

mul*=-1

if mul==-1 and i!=2*n-1:

print(x,'**',i,'/',i+1,'! - ',end='')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

elif i!=2*n-1:

print(x,'**',i,'/',i+1,'! + ',end='')

else:

print(x,'**',i,'/',i+1,'!')

print('\t\t\tThe sum of series is:',s)

else:

print('\t\t\tMenu is no longer available!')

c=input('\n`Would you like to continue?(yes/no):')

if c=='yes':

continue

else:

print('\t\t\tMenu is no longer available!')

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

Enter the serial no.:1

Enter the value of x:5

Enter the value of n:7

The series is: 5 ** 1 / 3 ! + 5 ** 3 / 5 ! + 5 ** 5 / 7 ! + 5 ** 7 / 9 ! + 5 ** 9 / 11 ! + 5


** 11 / 13 ! + 5 ** 13 / 15 !

The sum of series is: 2.7680359598323183

`Would you like to continue?(yes/no):yes

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

MENU

****

1.(x/3!)+(x**3/5!)+.....(x**(2n-1))/(2n+1)!

2.(x/2!)-(x**3/4!)+.....(x**(2n-1)/(2n)!

3.Exit

Enter the serial no.:2

Enter the value of x:5

Enter the value of n:3

The series is: 5 ** 1 / 2 ! - 5 ** 3 / 4 ! + 5 ** 5 / 6 !

The sum of series is: 1.6319444444444446

`Would you like to continue?(yes/no):n

Menu is no longer available!


>>>

RESULT:
Sum of series has computed successfully

EX.NO :2 STATISTICAL FUNCTIONS


DATE :

AIM :

To compute the mean, mode and median of a list of integers.

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

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

from statistics import *

def mn(t):

return mean(t)

def med(t):

return median(t)

def md(t):

return mode(t)

while True:

n=int(input('Enter the number of elements in list:'))

m=[]

for i in range(n):

m.append(int(input('Enter the number:')))

print('\t\t\t\tSTATISTICS FUNCTION')

print('\t\t\t\t************** ************')

print('\t\t\t\tThe list is:',m)

a,b,c=mn(m),med(m),md(m)

print('\t\t\t\tMean=',a,', Median=',b,', Mode=',c)

ch=input('Would you like to continue?(yes/no):')

if ch=='no':

break

OUTPUT:

Enter the number of elements in list:7

Enter the number:56

Enter the number:45

Enter the number:34

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Enter the number:89

Enter the number:89

Enter the number:89

Enter the number:90

STATISTICS FUNCTION
************** ************
The list is: [56, 45, 34, 89, 89, 89, 90]

Mean= 70.28571428571429 , Median= 89 , Mode= 89


Would you like to continue?(yes/no):no
>>>

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

EX.NO: 3 MENU DRIVEN CODE- NUMERIC


DATE :
AIM:

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:

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 is executed.

Note: The menu to be displayed :

MENU

1. Factorial of a number

2. Generate ‘N’ numbers of Fibonacci series

3. Exit

CODING:

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

#user defined function-1

def factorial(n):

a=1

for i in range(1,n+1):

a*=i

print('\t\t\tThe factorial of',n,'is',a)

def fibonacci(n):

print('\t\t\tFibonacci series upto',n,'terms:')

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\tUSER DEFINED FUNCTION-1')

print('\t\t\t**** ******* **********')

print('\t\t\t\tMENU')

print('\t\t\t\t****')

print('\t\t\t1.Factorial of a number')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

print('\t\t\t2.Fibonacci series')

print('\t\t\t3.Exit')

ch=int(input('Enter your choice:'))

if ch==1:

num=int(input('Enter the number:'))

factorial(num)

elif ch==2:

num=int(input('Enter number of terms:'))

fibonacci(num)

else:

print('\t\t\tMenu is no longer available!')

break

d=input('Would you like to continue?(yes/no):')

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

Enter your choice:1

Enter the number:5

The factorial of 5 is 120

Would you like to continue?(yes/no):yes

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

USER DEFINED FUNCTION-1


**** ******* **********
MENU
****
1.Factorial of a number

2.Fibonacci series

3.Exit

Enter your choice:2

Enter number of terms:7

Fibonacci series upto 7 terms:

0 1,1,2,3,5,8,

Would you like to continue?(yes/no):no

Menu is no longer available!


>>>

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

EX.NO: 4 MENU DRIVEN CODE- LIST/STRING


DATE :
AIM:
To write user defined functions and test them with a menu driven code 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 displayedand the user’s choice is accepted. Based on the choice the
appropriate function is executed.

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)

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

count=0

if b in L2:
for i in L2:
count+=1

if i==b:

print('\t\t\tSearch is successful at',count,'position')

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)

print('\t\t\tThe string after reversing:',s2)

while True:
print('\t\t\tUSER DEFINED FUNCTION-2')

print('\t\t\t**** ******* **********')

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')

ch=int(input('Enter your choice:'))

if ch==1:

n=int(input('Enter the number of elements:'))

L1=[]

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

for i in range(n):

L1.append(int(input('Enter the number:')))

a=int(input('Enter the number to be searched:'))

search(L1,a)

elif ch==2:

s1=input('Enter the string:')

reverse(s1)

else:
print('\t\t\tMenu is no longer available!')

break

c=input('Would you like to continue?(yes/no):')

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

Enter the number of elements:5

Enter the number:4

Enter the number:7

Enter the number:9

Enter the number:12

Enter the number:15


Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Enter the number to be searched:9

The list is: [4, 7, 9, 12, 15]

Search is successful at 3 position


Would you like to continue?(yes/no):yes
USER DEFINED FUNCTION-2
**** ******* **********
MENU
****
1.Linear search

2.Reversing string

3.Exit
Enter your choice:2
Enter the string:computer

Original string: computer


The string after reversing: retupmoc
Would you like to continue?(yes/no):no
Menu is no longer available!
>>>

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

Ex.NO:5 LARGEST, SECOND LARGEST, PRIME NUMBERS FROM A RANDOM GENERATION

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

‘No prime numbers in the generated list’.

Note: Include 3 UDFs Genreate(), Max_SecMax(), Prime() to do the above.

CODING:

from random import *

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:

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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

x=int(input('Enter the lower limit:'))

y=int(input('Enter the upper limit:'))

n=int(input('Enter the number of inputs:'))

m=[]

for i in range(n):

m.append(randint(x,y))

print('\t\t\t\tRANDOM GENERATOR')

print('\t\t\t\t********** ***************')

print('\t\t\t\tThe list of integers from',x,'to',y,'are:',m)

while True:
print('\t\t\t\t\tMENU')

print('\t\t\t\t\t*******')

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\t1.Largest number')

print('\t\t\t\t2.2nd largest number')

print('\t\t\t\t3.Prime numbers')

print('\t\t\t\t4.Exit')

ch=int(input('Enter your choice:'))

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!')

c=input('Would you like to continue?(yes/no):')

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

2.2nd largest number

3.Prime numbers

4.Exit

Enter your choice:3

The prime numbers are:[3, 3, 3]

Would you like to continue?(yes/no):no

Menu is no longer available!

>>>

RESULT:

The prime numbers from a list of randomly generated and the largest, second largest number has
found successfully

EX.NO: 6 MENU DRIVEN CODE – TUPLES


DATE :

AIM :

To perform the menu options on a tuple of values read as input.

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

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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.

Note: The displayed menu should be as follows:

MENU

i) Display words with all vowels

ii) Check BMI

iii) Exit

To check BMI(computed as Kg/m2) and return OBESE /OVERWEIGHT/NORMAL/UNDERWEIGHT,

use the following data:

BMI >= 30 Obese;

>25 Overweight

18.5 – 25 Normal.

<18.5 – Underweight

CODING:

#Menu driven using tuple

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

Enter the word:exam


Enter the word:mark
The tuple is: ('raji', 'vino', 'exam', 'mark')
The words with all 5 vowels are:
Would you like to continue?(yes/no):yes

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

NAME WEIGHT HEIGHT BMI


******* ********** ********* ******

sasi 50.0 kg 1.7 m 17.301038062283737


sangee 65.0 kg 1.8 m 20.061728395061728

Would you like to continue?(yes/no):no

Menu is no longer available!


>>>

RESULT:

perform the menu options on a tuple of values read as input has executed successfully

EX.NO: 7 MENU DRIVEN CODE – DICTIONARY


DATE :

AIM:

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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:

#user defined function-dictionary

import json

def add(a):

n=int(input('Enter the number of subscribers to be added:'))

for i in range(n):

ph=int(input('Enter the phone number:'))

name=input('Enter the name:')

a[ph]=name

def display(a):
n=int(input('Enter the phone number:'))

if n in a:

print('\t\t\tThe subscriber with given phone number is:',a[n])

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

print('\t\t\tDisplay successful')

else:

print('\t\t\tNumber not found!')

def modify(a):

ph=int(input('Enter the phone number for modification:'))

if ph in a:

det=input('Modified detail:')

a[ph]=det

else:

print('\t\t\tNumber not found!')

def delete(a):

ph=int(input('Enter the phone number to be deleted:'))

if ph in a:

print('\t\t\tDeletion successful!')

del a[ph]

else:

print('\t\t\tNumber not found!')

while True:

d={}

n=int(input('Enter the number of subscribers:'))

for i in range(n):

ph=int(input('Enter the phone number:'))

name=input('Enter the subscriber name:')

d[ph]=name

print('\t\t\tUSER DEFINED-FUNCTION DICTIONARY')

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******* *********** ************* ***************')

print('\t\t\t\tYour directory is as follows:')

print(json.dumps(d,indent=65))

while True:

print('\t\t\t\tMENU\n\t\t\t\t****')

print('\t\t\t\t1.Add subscriber details')

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')

ch=int(input('Enter the serial number:'))

if ch==1:

add(d)

print('\t\t\t\tYour directory after editing is as follows:')

print(json.dumps(d,indent=65))

elif ch==2:

display(d)

elif ch==3:

modify(d)

print('\t\t\t\tYour directory after editing is as follows:')

print(json.dumps(d,indent=65))

elif ch==4:

delete(d)

print('\t\t\t\tYour directory after editing is as follows:')

print(json.dumps(d,indent=65))

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

else:

print('\t\t\t\tMenu is no longer available!')

break

t=input('Would you like to continue?(yes/no):')

if t=='no':

break

tt=input('Would you like to start new directory?(yes/no):')

if tt=='no':

print('\t\t\t\tMenu is no longer available!')

break

OUTPUT:

Enter the number of subscribers:3


Enter the phone number:9988776655
Enter the subscriber name:yugesh
Enter the phone number:7863206234
Enter the subscriber name:sakthi
Enter the phone number:9087654321
Enter the subscriber name:kishore
USER DEFINED-FUNCTION DICTIONARY
******* *********** *************
Your directory is as follows:
{
"9988776655": "yugesh",
"7863206234": "sakthi",
"9087654321": "kishore"
}

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

Enter the serial number:1


Enter the number of subscribers to be added:1
Enter the phone number:9768904532
Enter the name:yoga

Your directory after editing is as follows:


{
"9988776655": "yugesh",
"7863206234": "sakthi",
"9087654321": "kishore",
"9768904532": "yoga"
}

Would you like to continue?(yes/no):yes


MENU
****
1.Add subscriber details
2.View subscriber
3.Modify
4.Delete
5.Exit
Enter the serial number:3
Enter the phone number for modification:9988776655
Modified detail:9988776644
Your directory after editing is as follows:
{
"9988776655": "9988776644",
"7863206234": "sakthi",
"9087654321": "kishore",
"9768904532": "yoga"
}

Would you like to continue?(yes/no):yes

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

Enter the serial number:4


Enter the phone number to be deleted:9988776655
Deletion successful!

Your directory after editing is as follows:


{
"7863206234": "sakthi",
"9087654321": "kishore",
"9768904532": "yoga"
}
Would you like to continue?(yes/no):no
Would you like to start new directory?(yes/no):no
Menu is no longer available!
>>>

RESULT:
create a dictionary to store details of ‘n’ subscribers and perform the menu operations has executed
successfully

EX.NO:8 8. NESTED DICTIONARY


DATE :

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:

Sub juniors Category

Enter Nalanda score :67

Enter Thakshashila score:78

Enter Shantiniketan score :90

Enter Kalakshetra score:60

Juniors Category

Enter Nalanda score :75

Enter Thakshashila score:68

Enter Shantiniketan score :70

Enter Kalakshetra score:98

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Seniors Category

Enter Nalanda score :80

Enter Thakshashila score:87

Enter Shantiniketan score :81

Enter Kalakshetra score:72


USER DEFINED FUNCTION-NESTED DICTIONARY
******* *********** ************* ******
The Original dictionary is:

{'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}}

The winner of Sub juniors category is Shantiniketan

The winner of Juniors category is Kalakshetra


The winner of Seniors category is Thakshashila
>>>

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

EX. NO :9 USER DEFINED MODULE – 1


DATE :

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

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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:')),)

print('\t\t\tUSER DEFINED MODULE')

print('\t\t\t**** ****** *******')

print('\t\t\tThe tuple is:',t)

while True:
print('\t\t\t\tMENU')

print('\t\t\t\t****')

print('\t\t\t 1.Palindrome check')

print('\t\t\t 2.Special Number')

print('\t\t\t 3.Exit')

ch=int(input('Enter the choice:'))

if ch==1:

for i in t:

N.palindrome(i)

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

elif ch==2:

for i in t:

N.special(i)

else:

break

a=input('Would you like to continue?(yes/no):')

if a=='no':

break

b=input('Would you like to start a new tuple?(yes/no):')

if b=='no':

print('\t\t\tMenu is no longer available!')

break

OUTPUT:
Enter the number of elements:2

Enter the number:45

Enter the number:676

USER DEFINED MODULE


**** ****** *******

The tuple is: [45, 676]

MENU
****

1.Palindrome check

2.Special Number

3.Exit

Enter the choice:1

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

Would you like to continue?(yes/no):yes

MENU
****

1.Palindrome check

2.Special Number

3.Exit

Enter the choice:2

it is not special no

it is not special no

Would you like to continue?(yes/no):no

Would you like to start a new tuple?(yes/no):no


Menu is no longer available!

>>>

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

EX.NO:10 USER DEFINED MODULE 2


DATE :
AIM:

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:

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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:

#user defined module-2

import Matrixfunction as mf

while True:

r=int(input('Enter number of rows:'))

c=int(input('Enter number of columns:'))

m1=[]

for i in range(r):

m2=[]

for j in range(c):

m2.append(int(input('Enter the element:')))

m1.append(m2)

print('\t\t\tUSER DEFINED MODULE-2')

print('\t\t\t**** ******* ********')

print('\t\t\tThe Original matrix is:')

for i in range(r):

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',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')

ch=int(input('Enter your choice:'))

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()

a=input('Would you like to start new matrix:')

if a=='no':

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

print('\t\t\tMenu is no longer available!')

break

OUTPUT:

Enter number of rows:2

Enter number of columns:2

Enter the element:34

Enter the element:45

Enter the element:56

Enter the element:67

USER DEFINED MODULE-2


**** ******* *********
The Original matrix is:

34 45

56 67

MENU
****
1.Swap

2.Row sort

3.Column sort

4.Exit

Enter your choice:1

45 34

67 56

Would you like to start new matrix:yes

Enter number of rows:2

Enter number of columns:2

Enter the element:12

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Enter the element:15

Enter the element:10

Enter the element:11

USER DEFINED MODULE-2


**** ******* ********

The Original matrix is:

12 15

10 11

MENU

****

1.Swap

2.Row sort

3.Column sort

4.Exit

Enter your choice:2

11 11

12 15

Would you like to start new matrix: yes

Enter number of rows:2

Enter number of columns:2

Enter the element:10

Enter the element:32

Enter the element:12

Enter the element:20

USER DEFINED MODULE-2


**** ******* ********
The Original matrix is:
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

10 32

12 20

MENU
****

1.Swap

2.Row sort

3.Column sort

4.Exit

Enter your choice:3

10 10

20 20

Would you like to start new matrix:no

Menu is no longer available!

>>>

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

EX.NO:11 TEXT FILE 1


DATE:
AIM:
To 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
METHODOLOGY:
A text file is created by reading one line at a time and asking is the user
wants to input more lines with a Y/N choice. A menu is then displayed and the related
operations are performed on the text file, depending on user’s choice.
Note: The menu options to be as follows:
MENU
1. Display number of lines

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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:

print('\t\t There are ',len(f.readlines())-1,'lines in the text file.')

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:

if j[-1]!='\n' and 'U' in j:

f2.write(j+'\t')

elif j[-1]=='\n' and 'U' in j:

f2.write(j[:-1:]+'\t')

f2.close()

except:

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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')

s=input('Enter your text:')

L1=s.split('.')

for i in L1:

a=i+'\n'

f.write(a)

f.close()

while True:

print('\t\t\t\tTEXT FILE-1')

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\t**** ******')

print('\t\t\t\tMENU')

print('\t\t\t\t****')

print('\t\t\t1.Display number of lines')

print('\t\t\t2.Copy words containing 'U'\n\t\t\tinto another file and display')

print('\t\t\t3.Convert the cases of letters in original file')

print('\t\t\t4.Exit')

ch=int(input('Enter your choice:'))

if ch==1:

lines()

elif ch==2:

copy()

elif ch==3:

convert()

else:

print('\t\t\tMenu is no longer available!')

break

t=input('Would you like to continue?:(yes/no)')

if t=='no':

break

OUTPUT:

Enter your text:YOU Are All Welcome

TEXT FILE-1

**** ******

MENU

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

****

1.Display number of lines

2.Copy words containing

into another file and display

3.Convert the cases of letters in original file

4.Exit

Enter your choice:1

There are 1 lines in the text file.

Would you like to continue?:(yes/no)yes

TEXT FILE-1

**** ******

MENU

****

1.Display number of lines

2.Copy words containing

into another file and display

3.Convert the cases of letters in original file

4.Exit

Enter your choice:2

['YOU\t']

Would you like to continue?:(yes/no)yes

TEXT FILE-1
**** ******

MENU
****

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

1.Display number of lines

2.Copy words containing

into another file and display

3.Convert the cases of letters in original file

4.Exit

Enter your choice:3

21

['you aRE aLL wELCOME\n']

Would you like to continue?:(yes/no)yes

TEXT FILE-1
**** ******

MENU
****

1.Display number of lines

2.Copy words containing

into another file and display

3.Convert the cases of letters in original file

4.Exit

Enter your choice:4

Menu is no longer available!

>>>

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

EX.NO:12 TEXT FILE - 2


DATE :

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

AIM:

To create a text file with ‘N’ lines of text and perform the operations in the menu

according to the user’s choice

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.

Note: The menu options to be as follows:

MENU

1. Count the number of words

2. Display Palindrome words

3. Display words starting and ending with a vowel

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

print('\t\t\tThe number of words are:',count)

f.close()

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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:

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

break

else:
L=a.split()

for i in L:

if i[0] in 'aeiouAEIOU' and i[-1] in 'aeiouAEIOU':

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')

n=int(input('Enter number of lines:'))

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\t1.Count number of words')

print('\t\t\t2.Display palindromes')

print('\t\t\t3.Words that start and end with vowel')

print('\t\t\t4.Exit')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

ch=int(input('Enter your choice:'))

if ch==1:
count()

elif ch==2:
palindrome()

elif ch==3:

vowel()

else:
print('\t\t\tMenu is no longer available!')

break

b=input('\nWould you like to continue?(yes/no):')

if b=='no':
print('\t\t\tMenu is no longer available!')

break

OUTPUT:

Enter number of lines:2

Enter the text:welcome to our school

Enter the text:thank you

TEXT FILE-2
**** ******

MENU
****

1.Count number of words

2.Display palindromes

3.Words that start and end with vowel

4.Exit
Enter your choice:1

The number of words are: 6


Would you like to continue?(yes/no):yes
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

TEXT FILE-2
**** ******

MENU
****

1.Count number of words

2.Display palindromes

3.Words that start and end with vowel

4.Exit
Enter your choice:2
Palindromes:None

None
Would you like to continue?(yes/no):yes

TEXT FILE-2
**** ******

MENU
****

1.Count number of words

2.Display palindromes

3.Words that start and end with vowel

4.Exit
Enter your choice:3

Words starting and ending with vowels:None

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

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

EX.NO:13 BINARY FILE 1


DATE :

AIM :

To create a Binary file STUDENT.DAT/.BIN/.TXT with each record having details of

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

1. Display Stream wise topper detail

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']

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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+')

n=int(input('Enter the number of details:'))

for i in range(n):
d={}

d['Roll']=int(input('Enter roll number:'))

d['Name']=input('Enter name:')

d['Stream']=input('Enter stream:')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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\t1.Display stream for topper')

print('\t\t\t2.Increment and decrement')

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

s=input('Would you like to continue?(yes/no):')

if s=='no':

print('\t\t\tMenu is no longer available!')

break

OUTPUT:

Enter the number of details:2

Enter roll number:1201

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Enter name:HARI

Enter stream:Computer Science

Enter total:478

Enter roll number:1202

Enter name:LATHA

Enter stream:Biology

Enter total:490
BINARY FILE-1
****** ******

MENU
****

1.Display stream for topper

2.Increment and decrement

3.Exit

Enter choice:1

Topper details: {'Roll': 1202, 'Name': 'LATHA', 'Stream': 'Biology', 'Total': 490}

Would you like to continue?(yes/no):yes

BINARY FILE-1
****** ******

MENU
****

1.Display stream for topper

2.Increment and decrement

3.Exit

Enter choice:2

Would you like to continue?(yes/no):yes

BINARY FILE-1
****** ******

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

MENU
****

1.Display stream for topper

2.Increment and decrement

3.Exit

Enter choice:1

Topper details: {'Roll': 1202, 'Name': 'LATHA', 'Stream': 'Biology', 'Total': 488}

Would you like to continue? (yes/no):no

Menu is no longer available!


>>>

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

EX.NO:14 BINARY FILE 2


DATE :

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.

Note: The menu options to be as follows:

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

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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('

n=int(input('Enter number of employees:'))

for i in range n:

L=[]

L.append(input('Enter name:'))

L.append(int(input('Enter age:')))

L.append(input('Enter department:'))

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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')

ch=int(input('Enter your choice:'))

if ch==1:

display()

elif ch==2:

delete()

else:

print('\t\t\tMenu is no longer available!')

break

t=input('Would you like to continue?:(yes/no)')

if t=='no':

print('\t\t\tMenu is no longer available!')

break

output:

Menu

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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
***************

[1001, 'sasi', 57, 34000]

[1002, 'raj', 59, 72000]

AFTER DELETION
***************

[1001, 'sasi', 57, 34000]

Menu

1. Add Record

2. Display Records

3. delete a Record

4. Exit

Enter choice (1-4): 4

>>>

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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

EX.NO:15 CSV FILE - WITH LISTS


DATE :

AIM:

To create CSV files and manipulate its contents

METHODOLOGY:

A GST.CSV is created to store 4 records with the following data:

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'],['********','***']])

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

n=int(input('Enter number of items:'))

for i in range(n):

L=[]

L.append(input('Enter the category:'))

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)

w.writerow(['ITEM ID','NAME','CATEGORY','UNIT PRICE'])

w.writerow(['**** **','****','********','**** *****'])

for i in range(n):
L=[]

L.append(int(input('Enter item ID:')))

L.append(input('Enter item name:'))

L.append(input('Enter item category:'))

L.append(int(input('Enter unit price:')))

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)

w.writerow(['ITEM ID','NAME','CATEGORY','UNIT PRICE','GST','TOTAL PRICE'])

w.writerow(['**** **','****','********','**** *****','***','***** *****'])

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)

print('\t\t\tThe file content of GST File is as follows:')

for i in r:
for j in i:
print(j,end='\t')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

print('\n\t\t')

f.close()

f=open('ITEMS.csv')

r=csv.reader(f)

print('\t\t\tThe file content is as follows:')

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)

print('\t\t\tThe file content is as follows:')

for i in r:
for j in i:
print(j,end='\t')

print('\n\t\t')

f.close()

gst()

OUTPUT:

Enter number of items:3

Enter the category:soap

Enter GST:12

Enter the category:mobile

Enter GST:18

Enter the category:laptop

Enter GST:21

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Enter item ID:1

Enter item name:soap

Enter item category:soap

Enter unit price:56

Enter item ID:2

Enter item name:HP

Enter item category:laptop

Enter unit price:18000

Enter item ID:3

Enter item name:SAMSUNG

Enter item category:mobile

Enter unit price:15000

The file content of GST File is as follows:

CATEGORY GST
****** ***

soap 12

mobile 18

laptop 21

The file content is as follows:

ITEM ID NAME CATEGORY UNIT PRICE


**** ** **** ******** **** *****

1 soap soap 56

2 HP laptop 18000

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

3 SAMSUNG mobile 15000

The file content is as follows:

ITEM ID NAME CATEGORY UNIT PRICE GST TOTAL PRICE


******** **** ******** **** ***** *** ***** *****

1 soap soap 56 12 62.72

2 HP laptop 18000 21 21780.0

3 SAMSUNG mobile 15000 18 17700.0

>>>

RESULT:

Create CSV files and manipulate its contents has executed successfully

EX.NO:16 STACK OPERATIONS - 1


DATE :

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

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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:')))

ch=input('Would you like to continue?:')

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()

ch=input('Would you like to keep continue?:')

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):

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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\tThe stack contents are as follows:')

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')

ch=int(input('Enter your choice:'))

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

s=input('Would you like to conitnue?(yes/no):')

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

Enter your choice:1

Enter stack capacity:4

Enter the element:456

Would you like to continue?:yes

Enter the element:678

Would you like to continue?:yes

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Enter the element:890

Would you like to continue?:yes

Enter the element:900

Would you like to continue?:no

Would you like to conitnue?(yes/no):yes

STACK OPERATION-1
***** ***********

MENU
****

1.Push

2.Pop

3.Peek

4.Display

5.Exit

Enter your choice:2


Would you like to keep continue? no
Would you like to continue? (yes/no): yes

STACK OPERATION-1
***** ***********

MENU
****

1.Push

2.Pop

3.Peek

4.Display

5.Exit
Enter your choice:3

The topmost element in the stack is 890

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Would you like to conitnue?(yes/no):yes

STACK OPERATION-1
***** ***********

MENU
****

1.Push

2.Pop

3.Peek

4.Display

5.Exit

Enter your choice:4

The stack contents are as follows:

890,678,456,

Would you like to conitnue?(yes/no):no

Menu is no longer available!

>>>

RESULT:

Implement a Stack using a list of integers and performing the related operations using user defined
functions has executed successfully

EX.NO:17 STACK OPERATIONS - 2


DATE :
Deepak A M.Sc., B.Ed. (CS), PGT
DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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

performed on a nested list using user defined functions

Note: The menu options to be displayed:

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:

print('\t\t\t\tStack overflow!\n\t\t\tCannot push anymore elements!')

break

else:

country=input('Enter country:')

capital=input('Enter capital:')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

stack.append([country,capital])

ch=input('Would you like to continue?:')

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()

ch=input('Would you like to keep continue?:')

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=[]

r=stack

while True:

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

if len(stack)==0:

break

else:

L.append(stack.pop())

print('\t\t\tThe stack contents are as follows:')

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')

ch=int(input('Enter your choice:'))

if ch==1:

n=int(input('Enter stack capacity:'))

stack=push(stack)

elif ch==2:

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

stack=pop(stack)

elif ch==3:

peek(stack)

elif ch==4:

stack=display(stack)

else:

print('\t\t\tMenu is no longer available!')

break

s=input('Would you like to conitnue?(yes/no):')

if s=='no':

print('\t\t\tMenu is no longer available!')

break

OUTPUT:
STACK OPERATION-2
***** ***********

MENU
****
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Enter your choice:1

Enter stack capacity:3

Enter country:INDIA

Enter capital:NEW DELHI

Would you like to continue?:yes

Enter country:ENGLAND

Enter capital:LONDON

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Would you like to continue?:yes

Enter country:USA

Enter capital:WASHINGTON

Would you like to continue?:no

Would you like to conitnue?(yes/no):yes

STACK OPERATION-2
***** ***********

MENU
****

1.Push
2.Pop
3.Peek
4.Display
5.Exit

Enter your choice:2


Would you like to keep continue?:no
Would you like to conitnue?(yes/no):yes

STACK OPERATION-2
***** ***********

MENU
****

1.Push

2.Pop

3.Peek

4.Display

5.Exit

Enter your choice:3

The topmost element in the stack is ['ENGLAND', 'LONDON']

Would you like to conitnue?(yes/no):yes

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

STACK OPERATION-2
***** ***********

MENU
****

1.Push

2.Pop

3.Peek

4.Display

5.Exit

Enter your choice:4

The stack contents are as follows:

['ENGLAND', 'LONDON'],['INDIA', 'NEW DELHI'],

Would you like to conitnue?(yes/no):no


Menu is no longer available!

>>>

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

EX.NO:18 PYTHON MYSQL INTERFACE


DATE :
AIM:

To create a connectivity and provide an interface between Python and MySQL to

perform queries using DML commands

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.

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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.

C. Sort the student list in the descending order of total.

D. Increase total marks for CS students by 5 %, for those, whose total is less than 180.

CODING:

#python mysql interface-1

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='')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

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():

cr.execute('UPDATE Student SET total=(total+(0.05*total)) WHERE Course="CSC" AND total<180')


cobj.commit()

cr.execute('SELECT * FROM Student')

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()

'''cr.execute('CREATE TABLE student(roll_no int primary key,\

name varchar(15),\

total int,\

course varchar(15),\

DOB date)')'''

'''n=int(input('Enter the number of inputs:'))

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

for i in range(n):

roll=int(input('Enter the roll number:'))

name=input('Enter name:')

total=int(input('Enter total:'))

course=input('Enter course:')

date=input('Enter date:')

if course in ('CSC','Bio','Commerce','EG'):

cr.execute('INSERT INTO student values({},"{}",{},"{}","{}")'.format(roll,name,total,course,date))

cobj.commit()

else:

continue''
while True:
print('\t\tPYTHON MYSQL INTERFACE-1')

print('\t\t****** ***** ***********')

print('\t\t\t\tMENU')

print('\t\t\t\t****')

print('\t\t1.Max and Min')

print('\t\t2.Display')

print('\t\t3.Sorting total')

print('\t\t4.Increment CSC total')

print('\t\t5.Exit')

ch=int(input('Enter your choice'))

if ch==1:

course()

elif ch==2:

display()

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

elif ch==3:

sort()

elif ch==4:

increment()

else:

print('\t\t\tMenu is no longer available!')

break

t=input('Would you like to continue?:')

if t=='no':

print('\t\t\tMenu is no longer available!')

break

cr.close()
cobj.close()

OUTPUT:
PYTHON MYSQL INTERFACE-1
****** ***** ***********
MENU
****
1.Max and Min

2.Display

3.Sorting total

4.Increment CSC total

5.Exit
Enter your choice1

Course Max Min Number


****** *** *** ******

CS 560 490 2

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

BIO 570 510 2

Would you like to continue?:yes

PYTHON MYSQL INTERFACE-1


****** ***** ***********
MENU
****

1.Max and Min

2.Display

3.Sorting total

4.Increment CSC total

5.Exit

Enter your choice2

Roll Name Total Course DOB

**** **** ***** ****** ***

12001 RAM 560 CS 2003-05-22

12002 RAMYA 570 BIO 2003-05-22

12003 RAJ 590 EG 2003-05-22

Would you like to continue? yes

PYTHON MYSQL INTERFACE-1


****** ***** ***********

MENU
****

1.Max and Min

2.Display

3.Sorting total

4.Increment CSC total

5.Exit

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Enter your choice3

Roll Name Total Course DOB


**** **** ***** ****** ***

12003 RAJ 590 EG 2003-05-22

12002 RAMYA 570 BIO 2003-05-22

12001 RAM 560 CS 2003-05-22

12005 ARVIND 510 BIO 2003-07-22

12004 POOJA 490 CS 2003-07-22

Would you like to continue? yes

PYTHON MYSQL INTERFACE-1


****** ***** ***********

MENU
****
1.Max and Min

2.Display

3.Sorting total

4.Increment CSC total

5.Exit

Enter your choice4

Roll Name Total Course DOB


**** **** ***** ****** ***

12001 RAM 560 CS 2003-05-22

12002 RAMYA 570 BIO 2003-05-22

12003 RAJ 590 EG 2003-05-22

12004 POOJA 490 CS 2003-07-22

12005 ARVIND 510 BIO 2003-07-22

Would you like to continue? yes

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

PYTHON MYSQL INTERFACE-1


****** ***** ***********

MENU
****

1.Max and Min

2.Display

3.Sorting total

4.Increment CSC total

5.Exit

Enter your choice5


Menu is no longer available!

>>>

RESULT:

Create a connectivity and provide an interface between Python and MySQL to perform queries using DML
commands has executed successfully

EX.NO:19 PYTHON MYSQL INTERFACE- CSV CREATION


DATE :

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:

#python mysql interface-2

import mysql.connector as mc

cobj=mc.connect(host='localhost',user='root',passwd='',port=3306,database='jobinfo')

cr=cobj.cursor()

'''cr.execute('CREATE TABLE Employee(Emp_no int,\

Name varchar(15),\

Designation varchar(15),\

Dept_no int PRIMARY KEY,\

Salary int)')

cr.execute('CREATE TABLE Dept(Dept_no int PRIMARY KEY,\

Dept_name varchar(15),\

Location varchar(15))')

n=int(input('Enter number of records:'))

for i in range(n):

eno=int(input('Enter Employee number:'))

name=input('Enter name:')

des=input('Enter designation:')

dno=int(input('Enter department number:'))

sal=int(input('Enter salary:'))

dept=input('Enter department:')

loc=input('Enter location:')

cr.execute('INSERT INTO Employee VALUES({},"{}","{}",{},{})'.format(eno,name,des,dno,sal))

cobj.commit()

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

cr.execute('INSERT INTO Dept VALUES({},"{}","{}")'.format(dno,dept,loc))

cobj.commit()'''

print('\t\tPYTHON MySQL INTERFACE-2')

print('\t\t****** ***** ***********')

print('\n')

print('\t\t\tEMPLOYEE')

print('\t\t\t********')

cr.execute('SELECT * FROM Employee')

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**********')

cr.execute('SELECT * FROM dEPT')

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')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

print('\t\t\tManager details')

print('\t\t\t******* *******')

cr.execute('SELECT Emp_no,Name,Salary,Dept_name,Location FROM Employee,Dept WHERE


Employee.Dept_no=Dept.Dept_no')

print('\t\t Emp_no\t\t Name\t\t Salary\t\t Dept_name\t\t Location')

print('\t\t ******\t\t ****\t\t ******\t\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:

PYTHON MySQL INTERFACE-2


****** ***** ***********

EMPLOYEE
********

Emp_no Name Designation Dept_no Salary


****** **** *********** ******* ******

10001 SEKHAR HR 11110 67000

10002 YOGA ENG 11111 65000

10003 SOWMI ACC 11112 60000

DEPARTMENT

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

**********

Dept_no Dept_name Location


******* ********* ********

11110 Admin CH

11111 Prod VLR

11112 Fin BLR

Manager details
******* *******

Emp_no Name Salary Dept_name Location


****** **** ****** ********* ********

10001 SEKHAR 67000 Admin CH

10002 YOGA 65000 Prod VLR

10003 SOWMI 60000 Fin BLR

>>>

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

EX.NO:20 PYTHON- MYSQL CONNECTIVITY FOR JOINS


DATE :

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)

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

for i in r:
if c==0:

c=1

continue

else:
if str(i[2])>=top:

top=str(i[2])

print('\t\t The topper(s) is/are:',end='')

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)

cr.execute('SELECT * FROM student')

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****** ***** ********* ***')

print('\t\t\t\tMENU')

print('\t\t\t\t****')

print('\t\t 1.Display contents')

print('\t\t 2.Display top scorer')

print('\t\t 3.Exit')

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

ch=int(input('Enter your choice:'))

if ch==1:
content()

elif ch==2:
top()

else:
print('\t\t\ Menu is no longer available!')

break

t=input('\nWould you like to continue?(yes/no):')

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

2.Display top scorer

3.Exit

Enter your choice:1

ROLL NAME TOTAL COURSE DOB

12001 RAM 560 CS 15-05-2003

12002 RAMYA 570 BIO 22-05-2003

12003 RAJ 590 EG 25-02-2003

12004 POOJA 490 CS 20-06-2003

12005 ARVIND 510 BIO 10-12-2002

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Would you like to continue?(yes/no):yes

PYTHON MySQL INTERFACE-CSV


****** ***** ********* ***
MENU
****
1.Display contents

2.Display top scorer

3.Exit

Enter your choice:2

The topper(s) is/are: RAJ

Would you like to continue?(yes/no):yes

PYTHON MySQL INTERFACE-CSV


****** ***** ********* ***

MENU
****

1.Display contents

2.Display top scorer

3.Exit

Enter your choice:3

Menu is no longer available!

>>>

RESULT:

Create a connectivity and provide an interface between Python and MySQL to perform join operations
on 2 tables has executed successfully

EX.NO.21 STRUCTURED QUERY LANGUAGE USING DDL COMMANDS


DATE:

AIM:
To execute queries using DDL commands and display the result.

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

METHODOLOGY:

Open mysql and type DDL commands such as CREATE, ALTER

QUERIES:

1) Create database SCHOOL and select it

CREATE DATABASE SCHOOL;

USE SCHOOL;

2) Create table STUDENT with following attributes RNO, NAME,PLACE,MARK

CREATE TABLE STUDENT (RNO INT (5), NAME VARCHAR (15), PLACE VARCHAR (20), MARK int (3));

3) To display table structure

DESC STUDENT;

4) TO add one more column RANK

ALTER TABLE STUDENT ADD RANK VARCHAR (10);

OUTPUT:
1) Query OK , 1 row affected (0.00 sec)

Database changed

2) Query OK , 0 row affected (0.02 sec)

3)

4) Query OK, 0 rows affected (0.01 sec)

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Records: 0 Duplicates: 0 Warnings: 0

RESULT:

Mentioned database creation, Table creation, ALTER the structure of table has executed successfully
using DDL commands.

EX.NO.22 STRUCTURED QUERY LANGUAGE USING DML COMMANDS

DATE:

AIM:
To execute queries using DML commands and display the result.

METHODOLOGY:

Open mysql and type DML commands such as INSERT,UPDATE,DELETE

QUERIES:
1) Add 5 records into STUDENT table

INSERT INTO STUDENT VALUES(1201,’RAJ’,’VLR’,489,’DISTINCTION’),( 1202,’RAM’,’KPD’,439,’FIRST’),(


1203,’ABI’,’VLR’,479,’DISTINCTION’),(1204,’YOGA’,’VLR’,419,’FIRST’),( 1205,’ARUN’,’RPT’,289,’SECOND’);

2) Display all records

SELECT * FROM STUDENT;

3) Modify the mark of student ABI to 490

UPDATE STUDENT SET MARK=490 WHERE NAME=’ABI’;

4) Delete record of RNO is 1205

DELETE FROM STUDENT WHERE RNO=1205;

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

OUTPUT:

1) Query OK, 5 rows affected (0.00 sec)

Records: 5 Duplicates: 0 Warnings: 0

2)

3)

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

4)

Query OK, 1 row affected (0.00 sec)

RESULT:

Insert records, update value in record, delete records, display records have executed successfully

EX.NO.23 STRUCTURED QUERY LANGUAGE USING OPERATORS, FUNCTIONS

DATE:

AIM:

To execute queries using operators, functions and display the result.

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:

1) to display records in descending order of MARK

SELECT * FROM STUDENT ORDER BY MARK DESC;

2) Display records whose name with second character is ‘A’

SELECT * FROM STUDENT WHERE NAME LIKE ‘_A%’;

3) Display unique place

SELECT DISTINCT PLACE FROM STUDENT;

4) Display records whose mark between 470 to 490

SELECT * FROM STUDENT WHERE MARK BETWEEN 470 and 490;

OUTPUT:

1)

2)

3)

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

4)

RESULT:

Apply operators and function on Table and executed result successfully

EX.NO.24 STRUCTURED QUERY LANGUAGE APPLY ON TWO TABLES


DATE:
AIM:
To apply structured query commands on two tables

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

1120 Cricket 5500 5 Group

1326 Badminton 4500 3 Personal

1786 Swimming 5000 5 Personal

1909 Tennis 3500 2 Group

1005 Basket Ball 3800 4 Group

Deepak A M.Sc., B.Ed. (CS), PGT


DON BOSCO
SCHOOL OF EXCELLENCE
SENIOR SECONDARY SCHOOL – CBSE
BOSCO NAGAR, TIRUPATTUR - 635601

Table: CLUB
S_NO CLUB_COACH LOCATION GENDER

1005 Mr. Sumit Sharma Pitampura Male

1786 Mr. Rohit Jain Shalimar Bagh Male

1909 Ms. Sandhya Kumari Ashok Vihar Female

1120 Mr. Kapil Yadav Vikas Puri Male

1326 Ms. Rohini Vijay Nagar Female


Write SQL queries for the following—
(i) Display sports name and coach name from table SPORTS and CLUB.
Select S_Name, Club_coach from Sports S, Club C where S.SNO and C.SNO;

(ii) Display the number of sports on the basis of type of training.


Select training_type count(*) from Sports group by training_type;

(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.

Deepak A M.Sc., B.Ed. (CS), PGT

You might also like