Program no.
12
Write a Python to display unique vowels and the total number of
unique vowels present in the given word using Stack.
Source Code:
vowels=[‘a’,’e’,’i’,’o’,’u’]
word=input(“Enter the word to search for vowels:”)
Stack=[]
for letter in word.lower():
if letter in vowels:
if letter not in Stack:
Stack.append(letter)
print(Stack)
print(‘The total number of unique vowels present
in’,word,’are’,len(Stack))
Output:
Enter the word to search for vowels: Computer Science
[‘o’,’u’,’e’,’i’]
The total number of unique vowels present in Computer Science are 4
1|Page
Program no.06
WAP to remove all the lines that contain the character ‘a’ in a file and
write it to another file.
Source Code:
input_file=open(‘Sample.txt’,’r’)
output_file=open(‘dataoutput.txt’,’w’)
lines=input_file.readlines()
for i in lines:
if ‘a’ not in i:
output_file.write(i)
input_file.close()
output_file.close()
Output:
list of following mysql queries
create table student student and insert data into it
delete to remove tuples
2|Page
Program no.13
WAP to count the number o-f upper case alphabets, vowels and
consonants present in a text file and display the same.
Source Code:
def cnt():
f=open(“Sample.txt”,”r”)
rec=f.read()
print(rec)
v=0
cons=0
lwr=0
upr=0
for ch in rec:
if (ch.islower()):
lwr+=1
elif(ch.isupper()):
upr+=1
ch=ch.lower()
if(ch in[‘a’,’e’,’i):
3|Page
v+=1
else:
cons+=1
f.close()
print(“Vowels are:”,v)
print(“Consonants are:”,cons)
print(“Lower case letters are:”,lwr)
print(“Upper case letters are:”,upr)
cnt()
Output:
Twinkle Twinkle little stars
How I wonder what you are
Vowels are: 16
Consonants are: 38
Lower case letters are: 41
Upper case letters are: 4
4|Page
Program no.11
Write a menu-driven program in Python to achieve the following
objective using user-defined functions:
1. Create a list of distinct words from given string.
2. Reverse each distinct word that appears in the given string.
3.Compute occurence(frequency) of each word in the given string.
Given string is: ‘apple orange mango apple orange mango apple guava
mango mango’
Source Code:
def listDistinctWords(str):
str=str.split()
L=[]
for word in str:
if word not in L:
L.append(word)
return L
def revStr(S):
rev=’’
5|Page
for ch in S:
rev=ch+rev
return rev
def freq(str):
L=listDistinctWords(str)
print(L)
freq_values=[]
for i in range(0,len(L)):
c=str.count(L[i])
freq_values.append(c)
print(‘Frequency of :’,L[i],’is’,c)
print()
str=‘apple orange mango apple orange mango apple guava mango
mango’
while True:
print(‘MAIN MENU’)
print(‘1. To create a list of distinct words from given string ’)
print(‘2. For reverse each distinct word that appears in the given
string’)
print(‘3. For counting the frequency of each word in the given string
’)
6|Page
print()
choice=int(input(‘Press 1-3 to continue or other than 1-3 to
Quit/Exit’))
if choice==1:
L1=listdistinctWords(str)
print(‘List of Distinct Words:\n’,L1)
print()
elif choice==2:
L=listDistinctWords(str)
print(‘Reversed Words:’)
for word in L:
print(revStr(word),end=’’)
print()
elif choice==3:
freq(str)
else:
break
Output:
MAIN MENU
1. To create a list of distinct words from given string
7|Page
2. For reversing each distinct word in the given string
3. For counting the frequency of each word in the given string
Press 1-3 to continue or other than 1-3 to Quit/Exit1
List of Distinct Words:
[‘apple’,’orange’,’mango’,’guava’]
MAIN MENU
1. To create a list of distinct words from given string
2. For reversing each distinct word in the given string
3. For counting the frequency of each word in the given string
Press 1-3 to continue or other than 1-3 to Quit/Exit2
Reversed Words:
elppa
egnaro
ognam
avaug
MAIN MENU
1. To create a list of distinct words from given string
8|Page
2. For reversing each distinct word in the given string
3. For counting the frequency of each word in the given string
Press 1-3 to continue or other than 1-3 to Quit/Exit3
[‘apple’,’orange’,’mango’,’guava’]
Frequency of: apple is 3
Frequency of: orange is 2
Frequency of: mango is 4
Frequency of:guava is 1
MAIN MENU
1. To create a list of distinct words from given string
2. For reversing each distinct word in the given string
3. For counting the frequency of each word in the given string
Press 1-3 to continue or other than 1-3 to Quit/Exit4
9|Page
Program no.05
WAP to generate random numbers between 1 to 6.
Source Code:
import random
Guess=True
while Guess:
n=random.randint(1,6)
userinput=int(input(“Enter a number between 1 to 6:”))
if userinput==n:
print(“Congratulations!!!, You won the lottery”)
else:
print(“Sorry, Try again, The lucky number was:”,n)
val=input(“Do you want to continue y/n:”)
if val in[‘y’,’Y’]:
Guess=True
else:
Guess=False
Output:
10 | P a g e
Enter a number between 1 to 6: 5
Congratulations!!!, You won the lottery
Do you want to continue y/n: y
Enter a number between 1 to 6: 1
Sorry, Try again, The lucky number was: 2
Do you want to continue y/n: n
11 | P a g e
Program no.07
WAP to print Fibonacci Series using function.
Source Code:
def Fibonacci(n):
firstterm = -1
secondterm = 1
print(“***Fibonacci Series***”)
for i in range(n):
thirdterm = firstterm + secondterm
print(thirdterm,end=””)
firstterm = secondterm
secondterm = thirdterm
n=int(input(“Enter the number of terms you want to print:”))
Fibonacci(n)
Output:
Enter the number of terms you want to print: 10
***Fibonacci Series***
0 1 1 2 3 5 8 13 21 34
12 | P a g e
Program no. 04
WAP to check whether a given string is a palindrome string or not.
Source Code:
def Palindrome(myLength):
for i in range(0,myLength//2):
if myString[i]!=myString[myLength-i-1]:
print(“The given string”,myString, “is not a Palindrome string”)
break
else:
continue
else:
print (“The given string”,myString, “is a Palindrome string”)
print(“Palindrome Check”)
myString = input(“Enter a string: ”)
myLength = len(myString)
Palindrome(myLength)
Output:
Palindrome Check
Enter a string: malayalam
13 | P a g e
The given string Malayalam is a Palindrome string
Palindrome Check
Enter a string: Malayalam
The given string Malayalam is not a Palindrome string
14 | P a g e
Program no.08
WAP to create and call a function INDEX_LIST(l), where L is the list of
elements passed as argument to the function. The function returns
another list named ‘indexList’ that stores the indices of all Non-Zero
Elements of L.
Source Code:
def INDEX_LIST(L):
indexList=[]
for i in range(len(L)):
if L[i]!=0:
indexList.append(i)
return indexList
L=[12,4,0,11,0,56]
print(INDEX_LIST(L))
Output:
L=[12,4,0,11,0,56]
[0,1,3,5]
15 | P a g e
Program no.03
WAP to create a Stack for storing only odd numbers out of all the
numbers entered by the user. Display the content of stack along with
the largest odd number in the Stack.
Source Code:
n=int(input(“Enter the number of values:”))
stack=[]
for i in range(n):
num=int(input(“Enter number:”))
if num%2 != 0:
stack.append(num)
print(‘***STACK CONTENTS***:’,stack)
largestNum=stack.pop()
while(len(stack)>0):
num=stack.pop()
if num>largestNum:
largestNum=num
print(“The largest number found is:”,largestNum)
Output:
Enter the number of values: 5
16 | P a g e
Enter number: 11
Enter number: 12
Enter number: 23
Enter number: 34
Enter number: 45
***STACK CONTENTS***:[11,23,45]
The largest number found is: 45
17 | P a g e
Program no. 09
WAP to create and call a function PUSH(Arr), where Arr is a list of
numbers. From this list push all numbers divisible by into a stack
implemented using a list. Display the stack if it has at least one
element, otherwise display appropriate error message.
Source Code:
def PUSH(Arr):
stck=[]
for x in range(0,len(Arr)):
if Arr[x]%5==0:
stck.append(Arr[x])
if len(stck)==0:
print(‘STACK IS EMPTY’)
else:
print(‘STACK CONTENTS(Numbers divisible by 5):’,stck)
Arr=[]
n=int(input(‘Enter the number of elements you want:’))
for i in range(n):
num=int(input(‘Enter the element:’))
Arr.append(num)
18 | P a g e
PUSH(Arr)
Output:
Enter the number of elements you want: 3
Enter the element: 12
Enter the element: 22
Enter the element: 34
STACK IS EMPTY
19 | P a g e
Program no.02
Julie has created a dictionary containing names and marks as key
value pairs of 6 students. Write a program, with separate user defined
functions to perform the following operations:
Push the keys (name of the student) of the dictionary into a
stack, where the corresponding value (marks) is greater than 75.
Pop and display the content of the stack.
Source Code:
R=[“OM’:76, “JAI”:45, “BOB”:89, “ALI”:65, “ANU”:90, “TOM”:82]
def PUSH(S,N):
S.append(N)
def POP(S):
if S != []:
return S.pop()
else:
return None
ST=[]
for k in R:
if R[k].=75:
PUSH(ST,k)
20 | P a g e
while True:
if ST != []:
print(POP(ST).end=””)
else:
break
Output:
R=[“OM’:76, “JAI”:45, “BOB”:89, “ALI”:65, “ANU”:90, “TOM”:82]
TOM ANU BOB OM
21 | P a g e
Program no. 10
Write a program to generate random numbers between 1 to 6 and
check whether a user won a lottery or not.
Source Code:
import random
n=random.randint(1,6)
guess=int(input(“Enter a number between 1 to 6:”))
if n==guess:
print(“Congratulations, You won the lottery”)
else:
print(“Sorry, Try again, The lucky number was:”,n)
Output:
Enter a number between 1 to 6 : 1
Congratulations, You won the lottery
Enter a number between 1 to 6 : 2
Sorry, Try again, The lucky number was: 4
22 | P a g e
Program no. 01
Write a program for linear search.
Source Code:
L=eval(input(“Enter the elements:”))
n=len(L)
item=eval(input(“Enter the element that you want to search:”))
for i in range(n):
if L[i]==item:
print(“Element found at the position:”,i+1)
break
else:
print(“Element not found”)
Output:
23 | P a g e
Enter the elements: 45, 47, 85, 41, 32, 56, 74, 65
Enter the element that you want to search: 32
Element found at the position: 5
24 | P a g e
25 | P a g e
26 | P a g e
27 | P a g e
28 | P a g e
29 | P a g e
30 | P a g e