Practical File XII CS
Practical File XII CS
EXPECTED INPUT :
EXPECTED OUTPUT :
CODE :
# PROGRAM 1
def series1(n):
init1 = 8
init2 = 7
print(init1)
print(init2)
for i in range(3,n+1):
if(i%2==1):
init1 = init1 + 3
print(init1)
else:
init2 = init2 + 5
print(init2)
def sereis2(n):
mul = 1
term = 0
for i in range(n):
term = term + 3 * mul
mul = mul + 2
print(term)
while(1):
print(" Menu")
print("1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?")
print("2. Series 2 : 3, 12, 27, 48, 75, 108, ?")
print("3. Exit")
ch = int(input("Enter your choice:"))
if(ch==1):
n = int(input("Enter the number of terms (greater than 2) :"))
series1(n)
elif(ch==2):
n = int(input("Enter the number of terms :"))
series2(n)
elif(ch==3):
break
else:
print("Wrong Choice")
OUTPUT:
Menu
1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?
2. Series 2 : 3, 12, 27, 48, 75, 108, ?
3. Exit
Enter your choice:1
Enter the number of terms (greater than 2) :5
8
7
11
12
14
Menu
1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?
2. Series 2 : 3, 12, 27, 48, 75, 108, ?
3. Exit
Enter your choice:2
Enter the number of terms :5
3
12
27
48
75
Menu
1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?
2. Series 2 : 3, 12, 27, 48, 75, 108, ?
3. Exit
Enter your choice:7
Wrong Choice
Menu
1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?
2. Series 2 : 3, 12, 27, 48, 75, 108, ?
3. Exit
Enter your choice:3
PRACTICAL 2
AIM : Write a menu driven Python program to calculate and return the
values of
• Area of triangle
• Area of a circle
• Area of regular polygon
EXPECTED INPUT :
• Area of triangle : The dimension of base and height
• Area of a circle : The dimension of radius
• Area of regular polygon : The dimension of side and number of sides
EXPECTED OUTPUT :
CODE :
# PROGRAM 2
import math
def tarea(b,h):
a = 0.5 * b * h
return a
def carea(r):
a = [Link] * r *r
return a
def rarea(n,s):
ang = 180/n
ang = ang * 3.14 /180
a = (s * s * n) / ( 4 * [Link](ang))
return a
while(1):
print(" Menu")
print("1. Area of Triangle")
print("2. Area of Circle")
print("3. Area of Regular polygon")
print("4. Exit")
ch = int(input("Enter your choice:"))
if(ch==1):
b = float(input("Enter the base of triangle : "))
h = float(input("Enter the height of triangle : "))
print("The area of triangle : ",tarea(b,h))
elif(ch==2):
r = float(input("Enter the radius of circle :"))
print("The area of circle :",carea(r))
elif (ch==3):
n= int(input("Enter the number of sides"))
s = float(input("Enter the dimension of side"))
print("The area of the polygon :",rarea(n,s))
elif(ch==4):
break
else:
print("Wrong Choice")
OUTPUT:
Menu
1. Area of Triangle
2. Area of Circle
3. Area of Regular polygon
4. Exit
Enter your choice:1
Enter the base of triangle : 4
Enter the height of triangle : 5.5
The area of triangle : 11.0
Menu
1. Area of Triangle
2. Area of Circle
3. Area of Regular polygon
4. Exit
Enter your choice:2
Enter the radius of circle :3.5
The area of circle : 38.48451000647496
Menu
1. Area of Triangle
2. Area of Circle
3. Area of Regular polygon
4. Exit
Enter your choice:3
Enter the number of sides5
Enter the dimension of side4
The area of the polygon : 27.54608577716545
Menu
1. Area of Triangle
2. Area of Circle
3. Area of Regular polygon
4. Exit
Enter your choice:7
Wrong Choice
Menu
1. Area of Triangle
2. Area of Circle
3. Area of Regular polygon
4. Exit
Enter your choice:4
PRACTICAL 3
AIM : Write a menu driven program in Python using user defined functions
to take a string as input and
• Check if it is palindrome
• Count number of occurrences of a given character
• Get an index from user and replace the character at that index with
user given value.
EXPECTED INPUT :
• Check if it is palindrome : A string
• Count number of occurrences of a given character : A string and a
character to search for
• Get an index from user and replace the character at that index with
user given value : A string, a character to replace and a character to
replace it with
EXPECTED OUTPUT :
• Check if it is palindrome : Display either “String is a palindrome” or
“String is not a palindrome”
• Count number of occurrences of a given character : The number of
occurrences of the character, 0 otherwise
• Get an index from user and replace the character at that index with
user given value : Resultant string after replacement
CODE :
# PROGRAM 3
import math
def palindrome(s):
rev = s[::-1]
if(rev == s):
print("The string is palindrome")
else:
print("The string is not a palindrome")
def countc(s,c):
c = [Link](c)
return c
def replacec(s,i):
c = s[i]
nc = input("Enter the character to replace :")
if(len(nc)==1):
ns = [Link](c,nc)
print(ns)
return ns
else:
print("Enter only one character")
while(1):
print(" Menu")
print("1. Palindrome")
print("2. Number of Occurence")
print("3. Replace character")
print("4. Exit")
ch = int(input("Enter your choice:"))
if(ch==1):
s = input("Enter the string :")
palindrome(s)
elif(ch==2):
s = input("Enter the string :")
c = input("Enter a character :")
if(len(c)==1):
print("The character ",c," is in ",s, ",", countc(s,c), " times")
else:
print("Enter only one character")
elif (ch==3):
s = input("Enter the string :")
i = int(input("Enter an index :"))
print("The string after replacement is :",replacec(s,i))
elif(ch==4):
break
else:
print("Wrong Choice")
OUTPUT:
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter your choice:1
Enter the string :hello
The string is not a palindrome
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter your choice:1
Enter the string :madam
The string is palindrome
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter your choice:2
Enter the string :Success
Enter a character :s
The character s is in Success , 2 times
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter your choice:2
Enter the string :Success
Enter a character :o
The character o is in Success , 0 times
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter your choice:3
Enter the string :Perfect
Enter an index :1
Enter the character to replace :o
Porfoct
The string after replacement is : Porfoct
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter your choice:6
Wrong Choice
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter your choice:4
PRACTICAL 4
AIM : Write a menu driven program in Python using user defined functions
that take a list as parameter and return
• maximum
• minimum
• sum of the elements
EXPECTED INPUT :
● ‘n’ as number of elements where n>0
● ‘n’ number of elements in a list
EXPECTED OUTPUT :
CODE :
# PROGRAM 4
def maximum(l):
m = l[0]
for i in range(len(l)):
if(l[i]>m):
m=l[i]
return m
def minimum(l):
m = l[0]
for i in range(len(l)):
if(l[i]<m):
m=l[i]
return m
def suml(l):
s=0
for i in range(len(l)):
s = s + l[i]
return s
while(1):
print(" Menu")
print("1. Maximum of list")
print("2. Minimum of list")
print("3. Sum of list")
print("4. Exit")
ch = int(input("Enter your choice:"))
if(ch==1):
l = list(map(int,input("\nEnter the numbers : ").strip().split()))
m = maximum(l)
print("Maximum of given list elements is :",m)
elif(ch==2):
l = list(map(int,input("\nEnter the numbers : ").strip().split()))
m = minimum(l)
print("Minimum of given list elements is :",m)
elif (ch==3):
l = list(map(int,input("\nEnter the numbers : ").strip().split()))
s = suml(l)
print("Sum of given list elements is :",s)
elif(ch==4):
break
else:
print("Wrong Choice")
OUTPUT:
Menu
1. Maximum of list
2. Minimum of list
3. Sum of list
4. Exit
Enter your choice:1
EXPECTED INPUT :
Angle in degrees
EXPECTED OUTPUT :
Sine and co-sine ration of the angle using Taylor series and using math
module
CODE :
#PROGRAM 5
import math
def sin(x,n):
t=x
su=t
d=1
for i in range(n):
t=((-1)*t*x*x)/((d+1)*(d+2))
d=d+2
su=su+t
return su
def cos(x,n):
t=1
su=t
for i in range(1,n):
t=((-1)*t*x*x)/((i*(i+1)))
su=su+t
return su
while(1):
print('''menu
1. Sine
2. Cos
3. Exit''')
ch=int(input('enter choice'))
if ch==1:
x = float(input('enter the value of x'))
x = x * 3.14 / 180
n = int(input('enter no. of terms'))
print("Sine value by user defined function : ",sin(x,n))
print("Sine value by built in function : ",[Link](x))
elif ch==2:
x = float(input('enter the value of x'))
x = x * 3.14 / 180
n = int(input('enter no. of terms'))
print("Cos value by user defined function : ",cos(x, n))
print("Cos value by built in function : ",[Link](x))
elif ch==3:
break
else:
print('invalid input')
OUTPUT:
menu
1. Sine
2. Cos
3. Exit
enter choice1
enter the value of x0
enter no. of terms10
Sine value by user defined function : 0.0
Sine value by built in function : 0.0
menu
1. Sine
2. Cos
3. Exit
enter choice1
enter the value of x45
enter no. of terms20
Sine value by user defined function : 0.706825181105366
Sine value by built in function : 0.706825181105366
menu
1. Sine
2. Cos
3. Exit
enter choice2
enter the value of x0
enter no. of terms10
Cos value by user defined function : 1.0
Cos value by built in function : 1.0
menu
1. Sine
2. Cos
3. Exit
enter choice2
enter the value of x60
enter no. of terms20
Cos value by user defined function : 0.5436084594656847
Cos value by built in function : 0.5004596890082058
menu
1. Sine
2. Cos
3. Exit
enter choice6
invalid input
menu
1. Sine
2. Cos
3. Exit
enter choice3
PRACTICAL 6
AIM : Write a menu driven program in Python using function to
• Display factorial of a number
• Find sum of first n natural numbers
• Display n terms of Fibonacci series
• Sum of digits of a number
EXPECTED INPUT :
EXPECTED OUTPUT :
CODE :
# PROGRAM 6
def fact(n):
if(n<2):
return 1
else:
return n * fact(n-1)
def sumn(n):
if (n==1):
return 1
else:
return n + sumn(n-1)
def fibo(n):
if(n==1):
return 0
elif(n==2)or(n==3):
return 1
else:
return fibo(n-1)+fibo(n-2)
def sumd(n):
if(n==0):
return 0
else:
d = n%10
n=n//10
return d + sumd(n)
while(1):
print('''menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit''')
ch=int(input('enter choice'))
if (ch==1):
n = int(input("Enter the number : "))
if(n>=0):
print("The factorial of the number is : ",fact(n))
else:
print("The factorial of negative number is not defined")
elif (ch==2):
n = int(input("Enter the number : "))
print("The sum of natural numbers upto ",n," is ",sumn(n))
elif (ch==3):
n = int(input("Enter the number of terms : "))
print("The Fibonacci Series is:")
for i in range(1,n-1):
x = fibo(i)
print(x)
elif (ch==4):
n = int(input("Enter the number : "))
print("The sum of digits is :",sumd(n))
elif (ch==5):
break
else:
print('invalid input')
OUTPUT:
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice1
Enter the number : 6
The factorial of the number is : 720
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice1
Enter the number : 0
The factorial of the number is : 1
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice1
Enter the number : -2
The factorial of negative number is not defined
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice2
Enter the number : 5
The sum of natural numbers upto 5 is 15
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice3
Enter the number of terms : 6
The Fibonacci Series is:
0
1
1
2
3
5
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice3
Enter the number of terms : 2
The Fibonacci Series is:
0
1
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice4
Enter the number : 65
The sum of digits is : 11
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice8
invalid input
menu
1. Factorial of a number
2. Sum of n natural numbers
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice5
PROGRAM 7
EXPECTED INPUT :
EXPECTED OUTPUT :
The location of element in the list if present in the list and appropriate
message otherwise
CODE :
# PROGRAM 7
def lsearch(arr,x):
flag = 0
count = 0
for i in range(len(arr)):
count = count + 1
if(arr[i]==x):
return(i, count)
return(-1, count)
while(1):
print('''menu
1. Linear Search
2. Exit''')
ch=int(input('enter choice'))
if (ch==1):
l = list(map(int,input("Enter the elements of list
:").strip().split()))
ele = int(input("Enter the element to search"))
res,count = lsearch(l,ele)
if(res == -1):
print("The element not found", "The number of
comparisons ", count)
else:
print("The element found at ",res," using ",count, "
number of comparisons")
elif (ch==2):
break
else:
print('invalid input')
OUTPUT:
menu
1. Linear Search
2. Exit
enter choice1
Enter the elements of list :3 5 6 8 9 2 1
Enter the element to search2
The element found at 5 using 6 number of comparisons
menu
1. Linear Search
2. Exit
enter choice1
Enter the elements of list :3 5 6 9 8 2
Enter the element to search4
The element not found The number of comparisons 6
menu
1. Linear Search
2. Exit
enter choice8
invalid input
menu
1. Linear Search
2. Exit
enter choice2
PROGRAM 8
AIM : Write a menu driven program to demonstrate a function
with two default parameters and different ways to use the function.
EXPECTED INPUT :
Case 1 : All the parameter values
Case 2 : Two parameter values
Case 3 : One parameter value
EXPECTED OUTPUT :
CODE :
# PROGRAM 8
def func(x,y=10,z=20):
print("Value of x : ",x)
print("Value of y : ",y)
print("Value of z : ",z)
print("Sum is ", x+y+z)
while(1):
print('''menu
1. All three parameter
2. Two parameters
3. One parameter
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
a=int(input("Enter the first parameter: "))
b=int(input("Enter the second parameter: "))
c=int(input("Enter the third parameter: "))
func(a,b,c)
elif (ch==2):
a=int(input("Enter the first parameter: "))
b=int(input("Enter the second parameter: "))
func(a,b)
elif (ch==3):
a=int(input("Enter the first parameter: "))
func(a)
elif (ch==4):
break
else:
print('invalid input')
OUTPUT:
menu
1. All three parameter
2. Two parameters
3. One parameter
4. Exit
enter choice1
Enter the first parameter: 12
Enter the second parameter: 22
Enter the third parameter: 32
Value of x : 12
Value of y : 22
Value of z : 32
Sum is 66
menu
1. All three parameter
2. Two parameters
3. One parameter
4. Exit
enter choice2
Enter the first parameter: 23
Enter the second parameter: 34
Value of x : 23
Value of y : 34
Value of z : 20
Sum is 77
menu
1. All three parameter
2. Two parameters
3. One parameter
4. Exit
enter choice3
Enter the first parameter: 35
Value of x : 35
Value of y : 10
Value of z : 20
Sum is 65
menu
1. All three parameter
2. Two parameters
3. One parameter
4. Exit
enter choice8
invalid input
menu
1. All three parameter
2. Two parameters
3. One parameter
4. Exit
enter choice4
PRACTICAL 9
EXPECTED INPUT :
EXPECTED OUTPUT :
• Count number of characters
• Count number of words
• Count number of vowels
• Count number of lines
• Count number of digits
• Count number of special characters
CODE :
# PROGRAM 9
def character(file):
cc = 0
ch='1'
while(ch):
ch = [Link](1)
cc = cc + 1
print("The number of characters are :",cc)
def words(file):
cw = 0
ch='1'
while(ch):
ch = [Link]()
wd = [Link]()
cw = cw + len(wd)
print("The number of words are :",cw)
def vowels(file):
cv = 0
ch='1'
while(ch):
ch = [Link](1)
if(ch in ('a','e','i','o','u')):
cv = cv + 1
def lines(file):
cl = 0
ch='1'
while(ch):
ch = [Link]()
cl = cl + 1
def digits(file):
cd = 0
ch='1'
while(ch):
ch = [Link](1)
if([Link]()==True):
cd = cd + 1
def spcharacter(file):
cs = 0
ch='1'
while(ch):
ch = [Link](1)
if([Link]()==False):
cs = cs + 1
file = open("[Link]","r")
while(1):
print('''menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit''')
ch=int(input('enter choice'))
if (ch==1):
[Link](0,0)
character(file)
elif (ch==2):
[Link](0,0)
words(file)
elif (ch==3):
[Link](0,0)
vowels(file)
elif (ch==4):
[Link](0,0)
lines(file)
elif (ch==5):
[Link](0,0)
digits(file)
elif (ch==6):
[Link](0,0)
spcharacter(file)
elif (ch==7):
break
else:
print('invalid input')
OUTPUT:
menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit
enter choice1
The number of characters are : 406
menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit
enter choice2
The number of words are : 59
menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit
enter choice3
The number of vowels are : 102
menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit
enter choice4
The number of lines are 9
menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit
enter choice5
The number of digits are : 7
menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit
enter choice6
The number of special characters are : 87
menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit
enter choice8
invalid input
menu
1. Count number of characters
2. Count number of words
3. Count number of vowels
4. Count number of lines
5. Count number of digits
6. Count number of special characters
7. Exit
enter choice7
PRACTICAL 10
AIM : Write a menu driven program in Python using function to read a text
file and
• Display number of times each word appears in the file.
• Display word with maximum and minimum length
• Display words starting with uppercase
EXPECTED INPUT :
EXPECTED OUTPUT :
CODE :
# PROGRAM 10
def words(file):
wdict = {}
ch='1'
while(ch):
ch = [Link]()
wd = [Link]()
for w in wd:
if(w in wdict):
wdict[w] = wdict[w] + 1
else:
wdict[w] = 1
for k,v in [Link]():
print("The word ",k, "appears ",v, " times")
def mmword(file):
max = 0
min = 100
ch='1'
while(ch):
ch = [Link]()
wd = [Link]()
for w in wd:
if(len(w) > max):
max = len(w)
mxword = w
if(len(w)<min):
min = len(w)
mnword = w
print("The word with maximum length is : ",mxword)
print("The word with minimum length is : ",mnword)
def uword(file):
ch='1'
while(ch):
ch = [Link]()
wd = [Link]()
for w in wd:
if(w[0].isupper()):
print(w)
file = open("[Link]","r")
while(1):
print('''menu
1. Number of occurance of each word
2. Maximum and minimum length word
3. Word starting with uppercase
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
[Link](0,0)
words(file)
elif (ch==2):
[Link](0,0)
mmword(file)
elif (ch==3):
[Link](0,0)
uword(file)
elif (ch==4):
break
else:
print('invalid input')
OUTPUT:
menu
1. Number of occurance of each word
2. Maximum and minimum length word
3. Word starting with uppercase
4. Exit
enter choice1
The word Carl appears 5 times
The word is appears 1 times
The word a appears 2 times
The word cat. appears 1 times
The word likes appears 3 times
The word to appears 3 times
The word take appears 1 times
The word walks. appears 1 times
The word play. appears 1 times
The word eat appears 1 times
The word fish. appears 1 times
The word At appears 1 times
The word the appears 3 times
The word end appears 1 times
The word of appears 1 times
The word day, appears 1 times
The word curls appears 1 times
The word up appears 1 times
The word and appears 1 times
The word sleeps appears 1 times
The word by appears 1 times
The word fire. appears 1 times
The word You appears 1 times
The word are appears 1 times
The word good appears 1 times
The word cat, appears 1 times
The word Carl! appears 1 times
menu
1. Number of occurance of each word
2. Maximum and minimum length word
3. Word starting with uppercase
4. Exit
enter choice2
The word with maximum length is : walks.
The word with minimum length is : a
menu
1. Number of occurance of each word
2. Maximum and minimum length word
3. Word starting with uppercase
4. Exit
enter choice3
Carl
Carl
Carl
Carl
At
Carl
You
Carl!
menu
1. Number of occurance of each word
2. Maximum and minimum length word
3. Word starting with uppercase
4. Exit
enter choice6
invalid input
menu
1. Number of occurance of each word
2. Maximum and minimum length word
3. Word starting with uppercase
4. Exit
enter choice4
PRACTICAL 11
EXPECTED INPUT :
EXPECTED OUTPUT :
CODE :
# PROGRAM 11
def copyfile(f):
fc = open("[Link]","w")
str = '1'
while(str!=''):
str=[Link]()
[Link](str)
[Link]()
print("File copied successfully")
def dispfilecopy():
file = open("[Link]","r")
str = '1'
while(str!=''):
str=[Link]()
print(str)
[Link]()
file = open("[Link]","r")
while(1):
print('''menu
1. Create Copy
2. Display Copy
3. Exit''')
ch=int(input('enter choice'))
if (ch==1):
[Link](0,0)
copyfile(file)
elif (ch==2):
dispfilecopy()
elif (ch==3):
break
else:
print('invalid input')
OUTPUT:
menu
1. Create Copy
2. Display Copy
3. Exit
enter choice1
File copied successfully
menu
1. Create Copy
2. Display Copy
3. Exit
enter choice2
Carl is a cat. Carl likes
to take walks. Carl likes
to play. Carl likes to eat fish. At the end of the
day, Carl curls up and sleeps by the fire. You are
a good cat, Carl!
menu
1. Create Copy
2. Display Copy
3. Exit
enter choice5
invalid input
menu
1. Create Copy
2. Display Copy
3. Exit
enter choice3
PRACTICAL 12
AIM : Read a text file to print the frequency of the word ‘He’ and ‘She’ found
in the file
EXPECTED INPUT :
EXPECTED OUTPUT :
CODE :
# PROGRAM 12
def countheshe():
counthe = 0
countshe = 0
file = open("[Link]","r")
str = '1'
while(str!=''):
str=[Link]()
word = [Link]()
for w in word:
if([Link]() == "he"):
counthe = counthe + 1
if([Link]() == "she"):
countshe = countshe + 1
print("The number of He are: ", counthe)
print("The number of She are: ", countshe)
[Link]()
while(1):
print('''menu
1. Count He and She
2. Exit''')
ch=int(input('enter choice'))
if (ch==1):
countheshe()
elif (ch==2):
break
else:
print('invalid input')
OUTPUT:
menu
1. Count He and She
2. Exit
enter choice1
The number of He are: 7
The number of She are: 8
menu
1. Count He and She
2. Exit
enter choice4
invalid input
menu
1. Count He and She
2. Exit
enter choice2
PRACTICAL 13
AIM : Write a menu driven program in Python to create a CSV file with
following data
• Roll no
• Name of student
• Mark in Sub1
• Mark in sub2
• Mark in sub3
• Mark in sub4
• Mark in sub5
EXPECTED INPUT :
The following data for ‘n’ students
• Roll no
• Name of student
• Mark in Sub1
• Mark in sub2
• Mark in sub3
• Mark in sub4
• Mark in sub5
EXPECTED OUTPUT :
● total and percentage for each student.
● The name of students if in any subject marks are greater than 80%
(Assuming marks are out of 100)
CODE :
# PROGRAM 13
import csv
def writefile(file):
l=[]
writer = [Link](file)
r = int(input("Enter the roll no :"))
n = input("Enter the name :")
s1 = int(input("Enter the marks for Subject 1 "))
s2 = int(input("Enter the marks for Subject 2 "))
s3 = int(input("Enter the marks for Subject 3 "))
s4 = int(input("Enter the marks for Subject 4 "))
s5 = int(input("Enter the marks for Subject 5 "))
data = [r,n,s1,s2,s3,s4,s5]
[Link](data)
def readfile(file):
reader = [Link](file)
for r in reader:
print("Roll no: ",r[0])
print("Name :",r[1])
for i in range(2,7):
print("Subject ",i-1,"marks :",r[i])
total = 0
for i in range(2,7):
total = total + int(r[i])
print ("Total : ",total)
print("Percentage : ", total/5)
def read90(file):
reader = [Link](file)
for r in reader:
flag = 0
for i in range(2,7):
if(int(r[i])>80):
flag = 1
if(flag==1):
print("Roll no: ",r[0])
print("Name :",r[1])
for i in range(2,7):
print("Subject",i-1,"marks :",r[i])
while(1):
print('''menu
1. Write to CSV file
2. Display the total and percentage
3. Display students who scored above 80
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
file = open("[Link]","a+",newline='')
writefile(file)
[Link]()
elif (ch==2):
file = open("[Link]","r")
[Link](0,0)
readfile(file)
[Link]()
elif (ch==3):
file = open("[Link]","r")
[Link](0,0)
read90(file)
[Link]()
elif (ch==4):
break
else:
print('invalid input')
OUTPUT:
menu
1. Write to CSV file
2. Display the total and percentage
3. Display students who scored above 80
4. Exit
enter choice1
Enter the roll no :1
Enter the name :Rajesh
Enter the marks for Subject 1 76
Enter the marks for Subject 2 87
Enter the marks for Subject 3 88
Enter the marks for Subject 4 81
Enter the marks for Subject 5 76
menu
1. Write to CSV file
2. Display the total and percentage
3. Display students who scored above 80
4. Exit
enter choice1
Enter the roll no :2
Enter the name :Suresh
Enter the marks for Subject 1 86
Enter the marks for Subject 2 57
Enter the marks for Subject 3 77
Enter the marks for Subject 4 65
Enter the marks for Subject 5 77
menu
1. Write to CSV file
2. Display the total and percentage
3. Display students who scored above 80
4. Exit
enter choice1
Enter the roll no :3
Enter the name :Meenal
Enter the marks for Subject 1 76
Enter the marks for Subject 2 56
Enter the marks for Subject 3 99
Enter the marks for Subject 4 87
Enter the marks for Subject 5 76
menu
1. Write to CSV file
2. Display the total and percentage
3. Display students who scored above 80
4. Exit
enter choice2
Roll no: 1
Name : Rajesh
Subject 1 marks : 76
Subject 2 marks : 87
Subject 3 marks : 88
Subject 4 marks : 81
Subject 5 marks : 76
Total : 408
Percentage : 81.6
Roll no: 2
Name : Suresh
Subject 1 marks : 86
Subject 2 marks : 57
Subject 3 marks : 77
Subject 4 marks : 65
Subject 5 marks : 77
Total : 362
Percentage : 72.4
Roll no: 3
Name : Meenal
Subject 1 marks : 76
Subject 2 marks : 56
Subject 3 marks : 99
Subject 4 marks : 87
Subject 5 marks : 76
Total : 394
Percentage : 78.8
menu
1. Write to CSV file
2. Display the total and percentage
3. Display students who scored above 80
4. Exit
enter choice3
Roll no: 1
Name : Rajesh
Subject 1 marks : 76
Subject 2 marks : 87
Subject 3 marks : 88
Subject 4 marks : 81
Subject 5 marks : 76
menu
1. Write to CSV file
2. Display the total and percentage
3. Display students who scored above 80
4. Exit
enter choice5
invalid input
menu
1. Write to CSV file
2. Display the total and percentage
3. Display students who scored above 80
4. Exit
enter choice4
AIM : Create a CSV file by entering user-id and password, read and search
the password for given userid.
EXPECTED INPUT :
Username and password to write to the file
Username to search the file
EXPECTED OUTPUT :
The csv file with the data of username and password
The password if the username is found, appropriate message otherwise
CODE :
# PROGRAM 14
import csv
def writefile(file):
writer = [Link](file)
r = input("User Name :")
n = input("Password :")
data = [r,n]
[Link](data)
def readfile(file):
reader = [Link](file)
for r in reader:
print("User Name: ",r[0])
print("Password:",r[1])
def search(file):
reader = [Link](file)
u = input("Enter the user name :")
flag=0
for r in reader:
if(r[0]==u):
print("The password is :",r[1])
flag=1
break
if(not flag):
print("Username not found")
while(1):
print('''Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit''')
ch=int(input('Enter choice'))
if (ch==1):
file = open("[Link]","a+",newline='')
writefile(file)
[Link]()
elif (ch==2):
file = open("[Link]","r")
[Link](0,0)
readfile(file)
[Link]()
elif (ch==3):
file = open("[Link]","r")
[Link](0,0)
search(file)
[Link]()
elif (ch==4):
break
else:
print('invalid input')
OUTPUT:
Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit
Enter choice1
User Name :user1
Password :abcd
Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit
Enter choice1
User Name :user2
Password :jklm
Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit
Enter choice1
User Name :user3
Password :pqrs
Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit
Enter choice2
User Name: user1
Password: abcd
User Name: user2
Password: jklm
User Name: user3
Password: pqrs
Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit
Enter choice3
Enter the user name :user3
The password is : pqrs
Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit
Enter choice3
Enter the user name :user7
Username not found
Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit
Enter choice7
invalid input
Menu
1. Write to CSV file
2. Display the records
3. Search user name
4. Exit
Enter choice4
AIM : Read a CSV file (containing item no, name, rate, QOH) from hard disc
and print all the items whose rate is between Rs 500 and Rs 1000.
EXPECTED INPUT :
The csv file with required data. The present file is
EXPECTED OUTPUT :
Details of item in the price range of 500 and 1000
CODE :
# PROGRAM 15
import csv
def readfile(file):
reader = [Link](file)
for r in reader:
print("Item No : ",r[0])
print("Item Name :",r[1])
print("Rate :",r[2])
print("Quantity :",r[3])
def disp5001000(file):
reader = [Link](file)
for r in reader:
if(int(r[2])>=500 and int(r[2])<=1000):
print("Item No : ",r[0])
print("Item Name :",r[1])
print("Rate :",r[2])
print("Quantity :",r[3])
while(1):
print('''Menu
1. Read the file
2. Display selection
3. Exit''')
ch=int(input('Enter choice'))
if (ch==1):
file = open("[Link]","r")
[Link](0,0)
readfile(file)
[Link]()
elif (ch==2):
file = open("[Link]","r")
[Link](0,0)
disp5001000(file)
[Link]()
elif (ch==3):
break
else:
print('invalid input')
OUTPUT:
Menu
1. Read the file
2. Display selection
3. Exit
Enter choice1
Item No : 123
Item Name : Pen
Rate : 100
Quantity : 25
Item No : 221
Item Name : Bag
Rate : 800
Quantity : 10
Item No : 324
Item Name : Geometry box
Rate : 200
Quantity : 20
Menu
1. Read the file
2. Display selection
3. Exit
Enter choice2
Item No : 221
Item Name : Bag
Rate : 800
Quantity : 10
Menu
1. Read the file
2. Display selection
3. Exit
Enter choice5
invalid input
Menu
1. Read the file
2. Display selection
3. Exit
Enter choice3
PRACTICAL 16
EXPECTED INPUT :
● Book no : integer
● Book name : string
● Book price : float
EXPECTED OUTPUT :
Display of data in stack formation
CODE :
# PROGRAM 15
s = []
def push():
b_ID=int(input('enter book no.'))
b_NAME=input('enter book name')
b_PRICE=float(input('enter price'))
data=b_ID,b_NAME,b_PRICE
[Link](data)
print("Book added to stack")
def pop():
if len(s)==0:
print('Stack is empty')
else:
dn = [Link]()
print(dn)
def disp():
if len(s)==0:
print('empty stack')
else:
for i in range(len(s)):
print("Book Id :",s[i][0])
print("Book Name :",s[i][1])
print("Book Price :",s[i][2])
while(1):
print('''menu
1. Push
2. Pop
3. Display
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
push()
elif (ch==2):
pop()
elif(ch==3):
disp()
elif (ch==4):
break
else:
print('invalid input')
OUTPUT:
menu
1. Push
2. Pop
3. Display
4. Exit
enter choice1
enter book no.12
enter book nameBridges of Madison county
enter price230
Book added to stack
menu
1. Push
2. Pop
3. Display
4. Exit
enter choice1
enter book no.22
enter book nameThere is no such place as far away
enter price340
Book added to stack
menu
1. Push
2. Pop
3. Display
4. Exit
enter choice3
Book Id : 12
Book Name : Bridges of Madison county
Book Price : 230.0
Book Id : 22
Book Name : There is no such place as far away
PRACTICAL 17
AIM : Write a menu driven program in Python to establish connection
between Python and MySQL. Perform following operations using the
connection
● Create a store table given above (in SQL questions)
● Insert rows in the table using user given data
● Drop the attribute ‘city’ and create a new attribute of ‘pincode’.
EXPECTED INPUT :
The data for table
Store id, Store Name, Location, City : String
Number of employees : integer
Date of opening the store : date
Sales : integer
EXPECTED OUTPUT :
The structure of table as created and after the alteration
The contents of the table after inserting values from MySQL
CODE :
# PROGRAM 17
import pymysql
db=[Link](host='localhost',user='root',password='password',datab
ase='classroom')
cursor=[Link]()
def create():
[Link]('''CREATE TABLE IF NOT EXISTS STORE
(Storeid char(5) Primary Key,
Name varchar(20),
Location varchar(20),
City varchar(15),
NoofEmp int,
Dateofopen date,
Sales int)''')
def dispstruct():
[Link]('''describe STORE''')
row = [Link]()
print("Column ", " Datatype", " Null", " Key", " Default", "
Extra")
for c in row:
print(c[0] ," ", c[1]," ", c[2]," ", c[3]," ", c[4] ," ",c[5])
def insert():
try:
a=input('Enter storeid')
b=input('Enter name')
c=input('Enter Location')
d=input('Enter City')
e=int(input('Enter NoofEmp'))
f= input("Enter the date of opening")
g = int(input('Enter sales'))
[Link]('''INSERT INTO STORE VALUES
(%s,%s,%s,%s,%s,%s,%s)''',(a,b,c,d,e,f,g))
except :
print("error")
[Link]()
return
[Link]()
def altertab():
try:
[Link]('''alter table STORE drop city''')
[Link]('''alter table STORE add pincode int(6)''')
except:
print("Column already deleted")
return
while(1):
print('''menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit''')
ch=int(input('enter choice'))
if (ch==1):
create()
elif (ch==2):
dispstruct()
elif (ch==3):
insert()
elif (ch==4):
altertab()
elif (ch==5):
break
else:
print('invalid input')
OUTPUT:
menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit
enter choice1
menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit
enter choice2
Column Datatype Null Key Default Extra
Storeid char(5) NO PRI None
Name varchar(20) YES None
Location varchar(20) YES None
City varchar(15) YES None
NoofEmp int(11) YES None
Dateofopen date YES None
Sales int(11) YES None
menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit
enter choice3
Enter storeidS12
Enter nameABC Store
Enter LocationAntop Hill
Enter CityMumbai
Enter NoofEmp32
Enter the date of opening2020/02/12
Enter sales10000
menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit
enter choice3
Enter storeidS15
Enter nameRBC Store
Enter LocationKane Nagar
Enter CityMumbai
Enter NoofEmp35
Enter the date of opening2015/10/22
Enter sales20000
menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit
enter choice4
menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit
enter choice2
Column Datatype Null Key Default Extra
Storeid char(5) NO PRI None
Name varchar(20) YES None
Location varchar(20) YES None
NoofEmp int(11) YES None
Dateofopen date YES None
Sales int(11) YES None
pincode int(6) YES None
menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit
enter choice7
invalid input
menu
1. Create table
2. Display structure
3. Insert Records
4. Alter Table
5. Exit
enter choice5
EXPECTED INPUT :
EXPECTED OUTPUT :
Updated table
Records in ascending or descending order
CODE :
# PROGRAM 18
import pymysql
db=[Link](host='localhost',user='root',password='password',datab
ase='classroom')
cursor=[Link]()
def update():
try:
[Link]('''select * from STORE''')
row = [Link]()
for r in row:
print("Enter the pincode for ",r[0])
p = input()
[Link]('''update STORE set pincode = (%s) where
Storeid = (%s)''',(p,r[0]))
except:
print("Error")
[Link]()
return
[Link]()
def dispasc():
[Link]('''select * from STORE order by Dateofopen''')
row = [Link]()
for r in row:
print(r)
def dispdesc():
[Link]('''select * from STORE order by Dateofopen desc''')
row = [Link]()
for r in row:
print(r)
while(1):
print('''menu
1. Update table
2. Display Ascending order
3. Display Descending order
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
update()
elif (ch==2):
dispasc()
elif (ch==3):
dispdesc()
elif (ch==4):
break
else:
print('invalid input')
OUTPUT:
menu
1. Update table
2. Display Ascending order
3. Display Descending order
4. Exit
enter choice1
Enter the pincode for S12
400037
Enter the pincode for S15
400073
menu
1. Update table
2. Display Ascending order
3. Display Descending order
4. Exit
enter choice2
('S15', 'RBC Store', 'Kane Nagar', 35, [Link](2015, 10, 22), 20000,
400073)
('S12', 'ABC Store', 'Antop Hill', 32, [Link](2020, 2, 12), 10000,
400037)
menu
1. Update table
2. Display Ascending order
3. Display Descending order
4. Exit
enter choice3
('S12', 'ABC Store', 'Antop Hill', 32, [Link](2020, 2, 12), 10000,
400037)
('S15', 'RBC Store', 'Kane Nagar', 35, [Link](2015, 10, 22), 20000,
400073)
menu
1. Update table
2. Display Ascending order
3. Display Descending order
4. Exit
enter choice6
invalid input
menu
1. Update table
2. Display Ascending order
3. Display Descending order
4. Exit
enter choice4
Final table contents
PRACTICAL 19
AIM : Write a menu driven program in Python to establish connection
between Python and MySQL. Perform following operations using the
connection using the ‘store’ table created above.
● Display total number of stores in each pincode
● Display the maximum sales of stores having more than 10
employees
● Display total number of employees in each pincode.
EXPECTED INPUT :
Table with appropriate data
EXPECTED OUTPUT :
● Total number of stores in each pincode
● Maximum sales of stores having more than 10 employees
● Total number of employees in each pincode
CODE :
# PROGRAM 19
import pymysql
db=[Link](host='localhost',user='root',password='password'
,database='classroom')
cursor=[Link]()
def nstore():
[Link]('''select pincode,count(*) from STORE group by
pincode''')
row = [Link]()
for r in row:
print(r)
def mxsales():
[Link]('''select max(Sales) from STORE where
Noofemp>10''')
row = [Link]()
for r in row:
print(r)
def nemp():
[Link]('''select pincode,sum(Noofemp) from STORE group
by pincode''')
row = [Link]()
for r in row:
print(r)
while(1):
print('''menu
1. Pincode wise total number of stores
2. Maximum sales (more than 10 employees)
3. Pincode wise total number of employees
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
nstore()
elif (ch==2):
mxsales()
elif (ch==3):
nemp()
elif (ch==4):
break
else:
print('invalid input')
OUTPUT:
menu
1. Pincode wise total number of stores
2. Maximum sales (more than 10 employees)
3. Pincode wise total number of employees
4. Exit
enter choice1
(400037, 3)
(400073, 1)
menu
1. Pincode wise total number of stores
2. Maximum sales (more than 10 employees)
3. Pincode wise total number of employees
4. Exit
enter choice2
(20000,)
menu
1. Pincode wise total number of stores
2. Maximum sales (more than 10 employees)
3. Pincode wise total number of employees
4. Exit
enter choice3
(400037, Decimal('40'))
(400073, Decimal('35'))
menu
1. Pincode wise total number of stores
2. Maximum sales (more than 10 employees)
3. Pincode wise total number of employees
4. Exit
enter choice7
invalid input
menu
1. Pincode wise total number of stores
2. Maximum sales (more than 10 employees)
3. Pincode wise total number of employees
4. Exit
enter choice4
PRACTICAL 20
AIM : Write a menu driven program in Python to establish connection
between Python and MySQL. Perform following operations using the
connection using the ‘store’ table created above
● Display first record
● Display next record
● Display previous record
● Display last record
EXPECTED INPUT :
EXPECTED OUTPUT :
CODE :
# PROGRAM 19
import pymysql
db=[Link](host='localhost',user='root',password='password'
,database='classroom')
cursor=[Link]()
[Link]("select * from STORE")
row = [Link]()
pos=0
while(1):
print('''menu
1. First
2. Next
3. Previous
4. Last
5. Exit''')
ch=int(input('enter choice'))
if (ch==1):
pos=0
print(row[0])
elif (ch==2):
pos = pos + 1
print(row[pos])
elif (ch==3):
pos = pos - 1
print(row[pos])
elif (ch==4):
pos = len(row)
print(row[len(row)-1])
elif (ch==5):
break
else:
print('invalid input')
OUTPUT:
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice1
('S12', 'ABC Store', 'Antop Hill', 32, [Link](2020, 2, 12),
10000, 400037)
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice2
('S15', 'RBC Store', 'Kane Nagar', 35, [Link](2015, 10, 22),
20000, 400073)
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice2
('S22', 'Royal Store', 'Sector 4', 6, [Link](2018, 9, 24),
30000, 400037)
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice2
('S62', 'Ketan Store', 'Sector 7', 2, [Link](2018, 5, 21),
53000, 400037)
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice3
('S22', 'Royal Store', 'Sector 4', 6, [Link](2018, 9, 24),
30000, 400037)
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice4
('S62', 'Ketan Store', 'Sector 7', 2, [Link](2018, 5, 21),
53000, 400037)
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice1
('S12', 'ABC Store', 'Antop Hill', 32, [Link](2020, 2, 12),
10000, 400037)
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice6
invalid input
menu
1. First
2. Next
3. Previous
4. Last
5. Exit
enter choice5
SQL PRACTICAL 1
QUESTION 1
Create the tables with the following description and insert at least 5 rows with
appropriate data in them.
Name of table : Store
QUERY 1
Create the tables with the following description and insert at least 5 rows with
appropriate data in them.
QUERY 1
QUERY 2
Create the following table with the data given and perform the queries given below
QUERY 1
c. Display the names of customers with their date of joining in descending order.
d. Display the customers who have joined after 1998 May 31.
h. To give boost to spending by females the government wants to add Rs. 5000
to all accounts held by females. Write a command to do this.
i. Add a new column named ‘amt_limit’. This is the limit of one time withdrawal.
Choose the appropriate data type.
QUERY 1
b. Display the names of customers whose salesman stays in the same city as
theirs.
c. Display the name of the salesman and the grade of the customer in
ascending order of the name of the salesman.
d. Display the number of salesmen from each city.