Programs For Term-2 Exam
Programs For Term-2 Exam
Programs For Term-2 Exam
Solution:
s = []
top=0
def push():
global top
if(top==3):
print("Stack is full")
else:
id=input("Enter elements into the stack")
s.append(id)
top=top+1
def pop():
global top
#print(len(s))
#print(top)
if top==0:
print('Stack is empty')
else:
dn = s.pop()
print(dn)
top=top-1
def disp():
if len(s)==0:
print('empty stack')
else:
for i in range(len(s)):
print(s[i])
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')
Solution:
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
s.append(data)
print("Book added to stack")
def pop():
if len(s)==0:
print('Stack is empty')
else:
dn = s.pop()
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')
Solution:
def isEmpty(stk): # checks whether the stack is empty or not
if stk==[]:
return True
else:
return False
def DeleteCustomer(stk):
def Display(stk):
if isEmpty(stk):
print("stack is empty")
else:
top=len(stk)-1
print("Elements in the stack are: ")
for i in range(top,-1,-1):
print(str(stk[i]))
stk=[]
top=None
AddCustomer("anitha")
AddCustomer("vanitha")
DeleteCustomer(stk)
Display(stk)
PART-B: MySQL
TABLE: STATIONARY
TABLE: CONSUMER
ii) To display the details of Stationary whose Price is in the range of 8 to 15(Both
values included)
iii) To display the ConsumerName , Address from table Consumer and Company and
CREATE TABLE STATIONARY (S_ID char(5) NOT NULL PRIMARY KEY, StationaryName
CREATE TABLE CONSUMER (C_ID int , ConsumerName char(25), Address char(25), S_ID char(5));
OUTPUT:
iii) select consumername, address, company, price from stationary, consumer where
stationary.s_id=consumer.s_id;
Company
ABC
XYZ
CAM
To create two tables for item and traders and execute the given commands using SQL.
TABLE:ITEM
Code IName Qty Price Company TCode
1001 DIGITAL PAD 121 120 11000 XENTIA T01
1006 LED SCREEN 40 70 38000 SANTORA T02
1004 CAR GPS SYSTEM 50 2150 GEOKNOW T01
1003 DIGITAL CAMERA 12X 160 8000 DIGICLICK T02
1005 PEN DRIVE 32GB 600 1200 STOREHOME T03
TABLE:TRADERS
i) To display the details of all the items in ascending order of item names (i.e
IName)
ii) To display item name and price of all those items, whose price is in
the range of 10000 and 22000 (both values inclusive)
iii) To display the number of items , which are traded by each trader.
The expected output of this query should be
T01 2
T02 2
T03 1
iv) To display the Price , item name(i.e IName) and quantity(i.e Qty) of
those items which have quantity more than 150.
V) To display the names of those traders, who are either from DELHI or from MUMBAI.
CREATE TABLE ITEM (Code int , IName char(25) , Qty int , Price int , Company char(25),
TCode char(5));
INSERT INTO ITEM VALUES (1001,” DIGITAL PAD 121”,120, 11000,” XENTIA”, “T01”);
INSERT INTO ITEM VALUES (1006,” LED SCREEN 40”,70, 38000,” SANTORA”, “T02”);
OUTPUT:
ii) select IName , Price from ITEM where Price between 10000 and 22000;
IName Price
DIGITAL PAD 121 11000
Tcode Count(*)
T01 2
T02 2
T03 1
TName
ELECTRONICS SALES
BUSY STORE CORP
AIM: To create two tables for doctor and salary and execute the given
commands using SQL
TABLE:DOCTOR
TABLE: SALARY
i) Display NAME of all doctors who are in “MEDICINE” having more than 10
ii) Display the average salary of all doctors working in “ENT” department using the
iv) Display DOCTOR.ID , NAME from the table DOCTOR and BASIC , ALLOWANCE
CREATE TABLE DOCTOR(ID int NOT NULL PRIMARY KEY, NAME char(25) , DEPT
char(25) , SEX char , EXPERIENCE int);
CREATE TABLE SALARY(ID int, BASIC int, ALLOWANCE int, CONSULTATION int);
OUTPUT:
NAME
Bill
min(ALLOWANCE)
1700
DEPT
ENT
ORTHOPEDIC
CARDIOLOGY
SKIN
MEDICINE
TABLE : COMPANY
TABLE : CUSTOMER
1. To display those company name which are having price less than 30000.
2. To display the name of the companies in reverse alphabetical order.
3. To increase the price by 1000 for those customer whose name starts with ‘S’
4. To add one more column totalprice with decimal (10,2) to the table customer
CREATE TABLE CUSTOMER(custid int(3), name varchar(15), price int(10), qty int(3) , cid
int(3));
OUTPUT :
NAME
NEHA SONI
2. select name from company order by name desc;
NAME
SONY
ONIDA
NOKIA
DELL
BLACKBERRY
update customer set price = price + 1000 where name like 's%';
5. TEACHER (RELATION)
To create table for teacher and execute the given commands using SQL.
TABLE : TEACHER
3. To list names of all teachers with their date of joining in ascending order.
OUTPUT:
Name
Shriya
Name Dateofjoin
Santhosh 1996-12-12
Jishnu 1997-10-01
Shakthi 1997-02-25
Shiva 1997-02-27
Shriya 1997-02-27
Ragu 1997-05-09
Sharmila 1998-03-24
Shanmathi 1999-01-07
count(*)
3
Department count(*)
Computer 2
History 3
Maths 3
Create the tables with the following description and insert at least 5 rows with
appropriate data in them.
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",passwd="purab@123",database="sc
hool")
cursor=db.cursor()
def create():
cursor.execute('''CREATE TABLE IF NOT EXISTS STORE2
(Storeid char(5) Primary Key,
Name varchar(20),
Location varchar(20),
City varchar(15),
NoofEmp int,
Dateofopen date,
Sales int)''')
def dispstruct():
cursor.execute('''describe STORE2''')
for x in cursor:
print(x)
def insert1():
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'))
cursor.execute("insert into store2 \
values('"+a+"','"+b+"','"+c+"','"+d+"','"+str(e)+"','"+f+"','"+str(g)+"')")
db.commit()
def showdata():
cursor.execute('''select * from STORE2''')
row = cursor.fetchall()
for r in row:
print(r)
def altertab():
try:
cursor.execute('''alter table STORE2 drop city''')
cursor.execute('''alter table STORE2 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
6. showrecords
5. Exit''')
ch=int(input('enter choice'))
if (ch==1):
create()
elif (ch==2):
dispstruct()
elif (ch==3):
insert1()
elif (ch==6):
showdata()
elif (ch==4):
altertab()
elif (ch==5):
break
else:
print('invalid input')
7. 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. (create and insert is common for 6th and 7th Program)
import mysql.connector
db=mysql.connector.connect(host="localhost",user="root",passwd="purab@123",database="sc
hool")
cursor=db.cursor()
def create():
cursor.execute('''CREATE TABLE IF NOT EXISTS STORE2
(Storeid char(5) Primary Key,
Name varchar(20),
Location varchar(20),
City varchar(15),
NoofEmp int,
Dateofopen date,
Sales int)''')
def insert1():
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'))
cursor.execute("insert into store2 \
values('"+a+"','"+b+"','"+c+"','"+d+"','"+str(e)+"','"+f+"','"+str(g)+"')")
db.commit()
def nstore():
cursor.execute('''select city,count(*) from STORE group by city''')
row = cursor.fetchall()
for r in row:
print(r)
def mxsales():
cursor.execute('''select name,max(Sales) from STORE where Noofemp>10''')
row = cursor.fetchall()
for r in row:
print(r)
def nemp():
def showdata():
cursor.execute('''select * from STORE2''')
row = cursor.fetchall()
for r in row:
print(r)
while(1):
print('''menu
1. Create table
2. Insert data
3. Show data
4. City wise total number of stores
5. Maximum sales (more than 10 employees)
6. City wise total number of employees
7. Exit''')
ch=int(input('enter choice'))
if (ch==1):
create()
elif (ch==2):
insert1():
elif (ch==3):
showdata():
elif (ch==4):
nstore()
elif (ch==5):
mxsales()
elif (ch==6):
nemp()
elif (ch==4):
break
else:
print('invalid input')