0% found this document useful (0 votes)
14 views6 pages

KNNH

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)
14 views6 pages

KNNH

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/ 6

Program 16: Write a Program to show MySQL database connectivity in python.

Solution:

import mysql.connector

con=mysql.connector.connect(host='localhost',user='root',password='',db='school')

stmt=con.cursor()

query='select * from student;'

stmt.execute(query)

data=stmt.fetchone()

print(data)

Program 17: Write a Python program to implement all basic operations of a stack, such as
adding element (PUSH operation), removing element (POP operation) and displaying the
stack elements (Traversal operation) using lists.

Solution:

#Implementation of List as stack

s=[]

c="y"

while (c=="y"):

print ("1. PUSH")


print ("2. POP ")

print ("3. Display")

choice=int(input("Enter your choice: "))

if (choice==1):

a=input("Enter any number :")

s.append(a)

elif (choice==2):

if (s==[]):

print ("Stack Empty")

else:

print ("Deleted element is : ",s.pop())

elif (choice==3):

l=len(s)

for i in range(l-1,-1,-1): #To display elements from last element to first

print (s[i])

else:

print("Wrong Input")

c=input("Do you want to continue or not? ")


Program 18: Write a program to display unique vowels present in the given word using
Stack.

Solution:

#Program to display unique vowels present in the given word

#using Stack

vowels =['a','e','i','o','u']

word = input("Enter the word to search for vowels :")

Stack = []

for letter in word:

if letter in vowels:

if letter not in Stack:

Stack.append(letter)

print(Stack)

print("The number of different vowels present in",word,"is",len(Stack))


Program 19: Write a program in Python to add, delete and display elements from a queue
using list.

Solution:

#Implementing List as a Queue - using function append() and pop()

a=[]

c='y'

while (c=='y'):

print ("1. INSERT")

print ("2. DELETE ")

print ("3. Display")

choice=int(input("Enter your choice: "))

if (choice==1):

b=int(input("Enter new number: "))

a.append(b)

elif (choice==2):

if (a==[]):

print("Queue Empty")

else:

print ("Deleted element is:",a[0])

a.pop(0)

elif (choice==3):

l=len(a)

for i in range(0,l):

print (a[i])

else:

print("wrong input")
c=input("Do you want to continue or not: ")

You might also like