0% found this document useful (0 votes)
144 views44 pages

Shreyas Practical Doc Final

This program defines functions to manage a stack of hotel customer records. It contains: 1) A push_cust() function to add customer names staying in a "Deluxe" room to the hotel stack. 2) A pop_cust() function to remove customer names from the stack and display them. It will display "Underflow" if the stack is empty. 3) The main code calls push_cust() to populate the hotel stack, then calls pop_cust() to remove and display customer names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views44 pages

Shreyas Practical Doc Final

This program defines functions to manage a stack of hotel customer records. It contains: 1) A push_cust() function to add customer names staying in a "Deluxe" room to the hotel stack. 2) A pop_cust() function to remove customer names from the stack and display them. It will display "Underflow" if the stack is empty. 3) The main code calls push_cust() to populate the hotel stack, then calls pop_cust() to remove and display customer names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

PROGRAM-1

AIM:

#WAP code to enter a number containing 3 digits or more . Arrange


the digits in ascending order

CODE:
lst=[]
num=int(input("Enter any number :"))
lst+=str(num) #adding the string to empty list
Lst=sorted(lst)
print("Number in ascending order :",Lst)

OUTPUT:
PROGRAM-2

AIM:
#WAP code to display the following pattern
1
31
531
7531
97531

CODE:
n=8
print("1")
for i in range(1,n):
if i%2==0: #modulas sign returns remainder
continue
else:
for j in range(i+2,0,-2): #nested loop
print(j,end=" ")
print()

OUTPUT:
PROGRAM -3
AIM:

# WAP to replace vowels with the next alphabet in a sentence (word in


lowercase).

CODE:

Str=input("Enter a string :")


Str1=Str.lower() #converting into small case letters
v=["a","e","i","o","u"] #list of vowels
Str2=" "
for i in Str1:
if i in v:
ch=ord(i)
c=ch+1
Chr=chr(c)
Str2+=Chr
else:
Str2+=i
print(Str2)

OUTPUT:

PROGRAM -4
AIM:
# Display all the names whose first letter begins with ‘A’ (accepting a
list).

CODE:

n=4
L=[]
for i in range(n):
name=input("Enter name :")
L+=[name]
print("Names that starts with A/a is/are ",end=":")
for i in L:
for j in i.split(): #splitting the name inorder to traverse letters
if j[0]=="a"or j[0]=="A":
print(i,end=",")

OUTPUT:

PROGRAM -5
AIM:

# input names in a list and display all names which start with A"""

CODE:

d={}
n=int(input("No. of employees :"))
for i in range(n):
name=input("Enter employee name :")
sal=int(input("Enter salary :"))
d[name]=sal #adding key and value to empty dictionary
t=tuple(d.values()) #it returns all the values in a tuple
for i in t :
mx=max(t)
for j in d:
if d[j]==mx:
print(j,"has salary Rs",mx,"which is the highest")

OUTPUT:

PROGRAM -6
AIM:

# WAP to input a four digit number. Write fourdigit() that calculates the
sum of squares of the first two digit number and the last two digit
numbers.

CODE:

def Four_Digit(n):
return int(n[:2])**2+int(n[2:])**2
n=input("Enter a four digit number :")
Num=Four_Digit(n)
print(Num)

OUTPUT:

PROGRAM -7
AIM:

#To accept a number and check whether it is a happy number or


not

CODE:

def Num(n):
tot=0
for i in str(n):
tot+=pow(int(i),2) # pow returns the value of i^2
return tot
num=int(input("Enter any value:"))
temp=num
while temp!=1 and temp!=4:
temp=Num(temp)
if temp==1:
print("Its a happy number ")
else:
print("Its not a happy number")

OUTPUT:

PROGRAM -8
AIM:
WAF tabulation() to open a file ‘result.txt’ in ‘w+’ mode to store index
number , name and marks of a few students.

CODE:

def Tabulation():
f=open("Result.txt","w+")
n=int(input("Enter no. of students':"))
for i in range(n):
rn=int(input("Enter student's roll no.':"))
nm=input("Enter student's name :")
mk=int(input("Enter student's mark :"))
rec=str(rn)+","+nm+","+str(mk)+"\n"
f.write(rec)
f.flush() #directly saves the data into the text file
print("Contents of file are : ")
f.seek(0) #brings file pointer to initial position
Rec=f.read() #reads the entire file
print(Rec)
print()
print(Tabulation())

OUTPUT:
PROGRAM -9

AIM:

#A file ‘contact.txt’ contains a few names along with their


phone number. WAF DISPLAY() to open the file in ‘r’ mode to
retrieve the names, count and display only those names with
their phone number that start with P. Assume the file already
exists.

CODE:

def Contacts():
f=open("Contact.txt","r")
rec=f.readlines() #returs a list where each line is element
print("Names starting with P :")
for i in rec:
for j in i.split():
if j[0]=="P" or j[0]=="p":
print(i)
Contacts()

OUTPUT:
Program -10
AIM:

#The file ‘city.txt’ stores the names of a few cities with the pincodes.
WAF DISPLAY() to read data and display only those cities with pincode
whose first letter is not a vowel.

CODE:

def Cities():
f=open("City.txt","w+")
n=int(input("Number of details :"))
for i in range(n):
cn=input("Enter city name :")
pc=int(input("Enter the pincode :"))
rec=cn+"-"+str(pc)+"\n"
f.write(rec)
f.flush()
vow=["a","e","i","o","u","A","E","I","O","U"] #list of vowles
f.seek(0) #initialising file pointer
Data=f.readlines()
for i in Data:
for j in i.split():
if j[0] not in vow:
print(i,sep=";")
else:
continue
Cities()
OUTPUT:
Program -11

AIM:

# Write a function lastline() to read lines from a text file


‘paragraph.txt’ and display the last 5 lines of the file to the
screen. Write a main program to call the function.

CODE:

def Lastline():
f=open("Paragraph.txt","w+")
l=[]
ln=int(input("Number of lines ? :"))
for i in range(ln):
snt=input("Enter any string :")
l.append(snt+"\n") #\n is a new line character
f.writelines(l)
f.seek(0)
rec=f.readlines()
lg=len(rec)
for a in range(5,0,-1):
print(rec[a*-1])
Lastline()
OUTPUT:
Program -12
AIM:

#WAF to search and display details of all trains, whose destination is


‘Delhi’ from a binary file ‘train.dat’ assuming binary file containing
objects of the following type
Train = {‘Tno’: ____,’From’: _____,’To’: ____}

CODE:

import pickle
fwb=open("train.dat","wb")
n=int(input("Enter no. of details : "))
for i in range(n):
tno=int(input("Enter train no : "))
to=input("Enter train destination : ")
frm=input("Enter train starting station : ")
rec={"tno":tno,"to":to,"from":frm} #creating a dict and and storing it in file
pickle.dump(rec,fwb)
fwb.close()
def Search():
f=open("train.dat","rb")
for i in range(n):
rd=pickle.load(f)
if rd['to']=='Delhi' or rd['to']=='delhi':
print(rd)
else:
continue
Search()
OUTPUT:
PROGRAM -13
AIM:

#WAF searchdata() from an existing binary file ‘teacher.dat’. Assume


that the binary file contains the following structure :
[Tr_ID, Tr_name, Tr_pos, Salary]
Update the details of the teacher based on the name entered by the
user, if the details are found it should also display a message ‘Record
found’ else display ‘Record not found’.

CODE:
import pickle
def Search_data():
rec=open("Teacher.dat","rb")
trec=open("Temp.dat","wb")
name=input("Enter teacher name to be updated : ")
a=pickle.load(f) #used to read a binary file
for i in a :
if i[1]==name:
n=input("Enter updated name : ")
i[1]=n1
else:
continue
pickle.dump(a,trec)
trec.seek(0) #moving file pointer to initial position
b=pickle.load(trec)
print(b)
trec,close()
rec.close()
Search_data()
OUTPUT:
PROGRAM -14
AIM:

#WAP to enter the following records


Item_no int
Item_name str
Qty int
Price float
No. of records to be accepted from the user. Read file and display in the
following format.
Item no :
Item name :
Quantity :
Price per item :
Amount :

CODE:
import pickle
fwb=open("Item.dat","wb")
n=int(input("Enter no. of entries : "))
for i in range(n):
ino=int(input("Enter item no : "))
iname=input("Enter item name : ")
qty=int(input("Enter item quantity : "))
price=int(input("Enter price : "))
rec={"itemno":ino,"iname":iname,"Qty":qty,"Price":price}
pickle.dump(rec,fwb)
fwb.close()
f=open("Item.dat","rb")
for i in range(n):
a=pickle.load(f)
print("Itemno : ",a[ino])
print("Item name : ",a[iname])
print("Quantity : ",a[qty])
print("Amount : ",a[price])
f.close()
OUTPUT :
PROGRAM -15
AIM:

#A binary file ‘book.dat’ has structure


[Book_name,Book_name,Author,Price]
i) WAF createfile() to input data for a record and add to both.
ii) WAF countrec(author) in python which accepts the author name
as parameter and counts and returns the number of books by the
given author that is stored in the file.

CODE :

import pickle
fwb=open("book.dat","wb")
n=int(input("enter no. of entries : "))
for i in range(n):
bno=int(input("enter book no. : "))
bname=input("enter book name : ")
author=input("enter author : ")
price=int(input("enter price of book : "))
rec=[str(bno),bname,author,str(price)]
pickle.dump(rec,fwb)
print("Data saved successfully")
fwb.close()

def Count(author):
f=open("book.dat","rb")
count=0
for i in range(n):
a=pickle.load(f)
b=a.count(author)
count+=b
print("No. of books by ", author, ":", count)
author=input("enter author name : ")
Count(author)
OUTPUT :
PROGRAM -16

AIM:

#A list contains the following record of the customer :


[customer_name,Room_type]. Write the following user defined
functions to perform given operations on the stack named ‘Hotel’:
i) Push_Cust() - To push customer names of those customers who
are staying in the ‘Deluxe’ Room type.
ii) Pop_Cust() - To pop the names of customers from the stack and
display them. Also, display ‘Underflow’ when there are no
customers in the stack.
CODE :

hotel=[]
def push_cust():
n=int(input("Enter no of customers :"))
for i in range(n):
name=str(input("Enter name of customer :"))
room=str(input("Enter room type :"))
if room=="Delux" :
hotel.append(name)

else:
continue
return n
print(hotel)
def pop_cust(p):
for i in range(p):
j=hotel.pop()
print(j)
if len(hotel)==0:
print("underflow")
break #comes out of loop body

p=push_cust()
pop_cust(p)
OUTPUT :
PROGRAM -17

AIM:

#A list contains ten integers WAP to perform the following


operations based on the list.
i) Traverse the content of the list and push all the perfect
square numbers into a stack.
ii) Pop and display the content of the stack.

CODE :

import math
Numbers=[]
L=[1,2,3,4,5,6,7,8,9,10]
def Push():
for i in L:
a=math.sqrt(i) #finds square root of number i
if a == int(a):
Numbers.append(int(a)**2)
print("Stack : ", Numbers)
Push()

def Pop():
while len(Numbers)!=0:
d=Numbers.pop()
print(d)
print("Underflow")
Pop()
OUTPUT:
PROGRAM –MYSQL CONNECTIVITY

AIM :
#Mysql Connectivity ( Creating Database and Table )

CODE :

import mysql.connector as mys


def Create_DB():
con=mys.connect(host="localhost",user="root",password="mysql")
try:
if con.is_connected():
cur=con.cursor()
q="create database employees"
cur.execute(q)
print("employees database created sucessfully")
except:
print("database name already exists")
con.close()
def create_table():
con=mys.connect(host="localhost",user="root",password="mysql",database="employees")
if con.is_connected():
cur=con.cursor()
q="create table EMP(ENO int primary key,ename varchar(20),gender varchar(3),salary
int)"
cur.execute(q)
print("EMP table created sucessfully")
else:
print("table name already exist")
con.close()
ch="y"
while ch=="y" or ch=='Y':
print("/n interfacing python with mysql")
print("1:to create database")
print("2:to create table")
opt=int(input("Enter your choice"))
if opt==1:
Create_DB()
elif opt==2:
create_table()
else:
print("Invalid choice")
ch=input("Want to continue?")

OUTPUT :
PROGRAM –INSERTING
AIM:

#To insert data into a mysql table using python


programing

CODE:
import mysql.connector as mys
mycon=mys.connect(host='localhost',user='root',
passwd='mysql',database='12a_2023')
mycursor=mycon.cursor()
print("wlecome to employee data entry")
ans="y"
while ans=="y":
eno=int(input("enter employee no.:"))
nm=input("enter name:")
dp=input("enter dept:")
s=int(int(input("enter salary:"))
query="insert into emp values ({0},'{1}','{2}',{3})".format
(eno,nm,dp,s)
mycursor.execute(query)
mycon.commit()
print("##record saved...##")
ans=input("add more??")
OUTPUT :
PROGRAM -UPDATING
AIM:

#To UPDATE data in a mysql table using python


programing

CODE:

import mysql.connector as mys


mych= mys.connect(host="localhost",user= "root",passwd="mysql",
database="12_a")
mycursor=mych.cursor()
print("Welcome to Employee UPDATE SCREEN!!")
eno=int(input("Enter employee number: "))
query="select * from emp where empno={}".format(eno)
mycursor.execute(query)
data=mycursor.fetchone()
if data!=None:
print("Record found..Details are:")
print(data)
ans=input("Update salary? :(y/n)")
if ans=="y":
s=int(input("enter the new salary:"))
query="update emp set salary={} where empno={}".format(s,eno)
mycursor.execute(query)
mych.commit()
print("Record updated")
else:
print("Employee not found")

OUTPUT :
PROGRAM-DELETING
AIM:

#To DELETE data in a mysql table using python


programing

CODE:
import mysql.connector as mys

mych= mys.connect(host="localhost",user= "root",passwd="mysql", database="12_a")

mycursor=mych.cursor()

print("Welcome to Employee DELETE SCREEN!!")

eno=int(input("Enter employee number: "))

query="select * from emp where empno={}".format(eno)

mycursor.execute(query)

data=mycursor.fetchone()

if data!=None:

print("Record found..Details are:")

print(data)

ans=input("delete details? :(y/n)")

if ans=="y":

query="DELETE from emp where empno={}".format(eno)

mycursor.execute(query)

mych.commit()

print("Record DELETED")

else:

print("RECORD NOT FOUND")


OUTPUT :
MYSQL QUERY FOR 2 TABLES

1. To display employee name of employee, jobid with


the corresponding job title.

mysql>select EMPLOYEEid,NAME,employee.JOBid,JOBtitle
from employee,job
where employee.JOBid=job.JOBid;

+------------+-------------------+-------+--------------------------+
| EMPLOYEEid | NAME | JOBid | JOBtitle |
+------------+-------------------+-------+--------------------------+
| E1 | Sumit Sinha | 102 | Vice President |
| E2 | Vijay Singh Tomar | 101 | President |
| E3 | Ajay Rajpal | 103 | Administration Assistant |
| E4 | Mohit Ramnani | 102 | Vice President |
| E5 | Shaija Singh | 103 | Administration Assistant |
+------------+-------------------+-------+--------------------------+

2. To display names of employee sales, and


corresponding job title who have achieve sales more
than 13 lakhs.
mysql> select NAME,SALES,JOBtitle from employee,JOB
WHERE employee.JOBid=JOB.JOBid AND SALES>1300000;

+--------------+---------+--------------------------+
| NAME | SALES | JOBtitle |
+--------------+---------+--------------------------+
| Ajay Rajpal | 1400000 | Administration Assistant |
| Shaija Singh | 1450000 | Administration Assistant |
+--------------+---------+--------------------------+

3. To display name and corresponding name title of


those employee who have ‘sigh’ anywhere in their
names.

mysql> SELECT NAME,JOBtitle from employee,JOB where


EMPLOYEE.JOBid=JOB.JOBid AND NAME LIKE "%Singh%";
+-------------------+--------------------------+
| NAME | JOBtitle |
+-------------------+--------------------------+
| Vijay Singh Tomar | President |
| Shaija Singh | Administration Assistant |
+-------------------+--------------------------+

4. Write the command to change the jobid to 104 of the


employee with ID as ‘E4’ in the employee table?

mysql> UPDATE EMPLOYEE SET JOBid=104 WHERE


EMPLOYEEid="E4";
+------------+-------------------+---------+-------+
| EMPLOYEEid | NAME | SALES | JOBid |
+------------+-------------------+---------+-------+
| E1 | Sumit Sinha | 1100000 | 102 |
| E2 | Vijay Singh Tomar | 1300000 | 101 |
| E3 | Ajay Rajpal | 1400000 | 103 |
| E4 | Mohit Ramnani | 1250000 | 104 |
| E5 | Shaija Singh | 1450000 | 103 |
+------------+-------------------+---------+-------+
II)

1.To display average price of all the uniform of


Raymond company from the table cost.

mysql> select avg(price) from cost where


company="Raymond";

+------------+
| avg(price) |
+------------+
| 720.0000 |
+------------+
2. To display details of all the uniform from the
‘UNIFORM TABLE’ in the descending order of stock date.

mysql> select * from uniform order by StockDate desc;

+-------+-------+--------+------------+
| Ucode | Uname | Ucolor | StockDate |
+-------+-------+--------+------------+
| 1 | Shirt | White | 2021-03-31 |
| 3 | Skirt | Grey | 2021-02-18 |
| 2 | Pant | Black | 2020-01-01 |
| 5 | Socks | Blue | 2019-03-19 |
| 4 | Tie | Blue | 2019-01-01 |
| 6 | Belt | Black | 2017-12-09 |
+-------+-------+--------+------------+

3. To display max price and min price of each company.

mysql> select company,max(price), min(price) from cost


group by company;

+---------+------------+------------+
| company | max(price) | min(price) |
+---------+------------+------------+
| Galin | 830 | 830 |
| Mattex | 620 | 150 |
| Raymond | 940 | 500 |
| Yasin | 810 | 770 |
+---------+------------+------------+

4. To display company where the number of uniform size


is ‘M’.
mysql> select company,count(size) from cost group by
company having count(size)>2;
+---------+-------------+
| company | count(size) |
+---------+-------------+
| Mattex | 3 |
+---------+-------------+

5. To display the uniform code, name, colour, size and


company.

mysql> select
cost.Ucode,Uname,Ucolor,cost.size,Company from
cost,uniform where cost.Ucode=uniform.Ucode;

+-------+-------+--------+------+---------+
| Ucode | Uname | Ucolor | size | Company |
+-------+-------+--------+------+---------+
| 1 | Shirt | White | M | Raymond |
| 1 | Shirt | White | L | Mattex |
| 2 | Pant | Black | XL | Mattex |
| 2 | Pant | Black | M | Yasin |
| 2 | Pant | Black | L | Raymond |
| 3 | Skirt | Grey | M | Yasin |
| 3 | Skirt | Grey | L | Galin |
| 4 | Tie | Blue | S | Mattex |
+-------+-------+--------+------+---------+
III)

1. WRITE A QUERY TO LIST NAME OF FEMALE STUDENTS IN


HINDE DEPARTMENT?

mysql> select Name,Dept,DOA from student where Fees


between 200 and 300;

+--------+----------+------------+
| Name | Dept | DOA |
+--------+----------+------------+
| Ankit | History | 1998-03-24 |
| Anu | Hindi | 1996-12-12 |
| Charan | Hindi | 1997-09-05 |
| Deepa | History | 1997-06-27 |
| Dinesh | Computer | 1997-02-25 |
| Usha | null | 1997-07-31 |
+--------+----------+------------+
2.WRITE A QUERY TO LIST THE NAMES OF STUDENTS WHOSE
AGE BETWEEN 18 AND 20?
mysql> select Name from student where Dept="Hindi" and
Gender="F";

+------+
| Name |
+------+
| Anu |
+------+
3. WRITE A QUERY TO DISPLAY NAME OF STUDENTS WHOSE
NAME IS STARTING WITH ‘A’?

mysql> select Name from student where name like "A%";

+-------+
| Name |
+-------+
| Arun |
| Ankit |
| Anu |
+-------+

4. WRITE THE QUERY TO LIST THE NAME OF STUDENTS WHOSE


NAME HAVE SECOND ALPHABET IN THEIR NAMES?
5.
mysql> select Name from student where Name like "_n
%";

+-------+
| Name |
+-------+
| Ankit |
| Anu |
+-------+
IV)

1.Write a Query to delete the details of Roll number


is 8?

mysql> delete from student where Rollno=8;


+--------+--------+--------+------+----------+------------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees |
+--------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 21 | Computer | 1997-01-10 | 120 |
| 2 | Ankit | M | 21 | History | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | Hindi | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | null | 1999-07-01 | 400 |
| 5 | Charan | M | 18 | Hindi | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | History | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | Computer | 1997-02-25 | 210 |
+--------+--------+--------+------+----------+------------+------+
2. Write a Query to change the fess of Student to 170
whose Roll number is 1, if the existing fess is less
than 130?

mysql> update student set Fees=170 where Rollno=1 and


Fees<130;

+--------+--------+--------+------+----------+------------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees |
+--------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 21 | Computer | 1997-01-10 | 170 |
| 2 | Ankit | M | 21 | History | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | Hindi | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | null | 1999-07-01 | 400 |
| 5 | Charan | M | 18 | Hindi | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | History | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | Computer | 1997-02-25 | 210 |
+--------+--------+--------+------+----------+------------+------+
3. Write a Query to add a new column Area of type
varchar in table STU.

mysql> alter table student add Area varchar(60);

+--------+--------+--------+------+----------+------------+------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees | Area |
+--------+--------+--------+------+----------+------------+------+------+
| 1 | Arun | M | 21 | Computer | 1997-01-10 | 170 | NULL |
| 2 | Ankit | M | 21 | History | 1998-03-24 | 200 | NULL |
| 3 | Anu | F | 20 | Hindi | 1996-12-12 | 300 | NULL |
| 4 | Bala | M | 19 | null | 1999-07-01 | 400 | NULL |
| 5 | Charan | M | 18 | Hindi | 1997-09-05 | 250 | NULL |
| 6 | Deepa | F | 19 | History | 1997-06-27 | 300 | NULL |
| 7 | Dinesh | M | 22 | Computer | 1997-02-25 | 210 | NULL |
+--------+--------+--------+------+----------+------------+------+------+

4. Write a Query to Display Name of all students whose


Area Contains NULL

mysql> select Name from student where Area is NULL;

+--------+
| Name |
+--------+
| Arun |
| Ankit |
| Anu |
| Bala |
| Charan |
| Deepa |
| Dinesh |
+--------+

5. Write a Query to delete Area Column from the table


STU.
mysql> alter table student drop AREA;

+--------+--------+--------+------+----------+------------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees |
+--------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 21 | Computer | 1997-01-10 | 170 |
| 2 | Ankit | M | 21 | History | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | Hindi | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | null | 1999-07-01 | 400 |
| 5 | Charan | M | 18 | Hindi | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | History | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | Computer | 1997-02-25 | 210 |
+--------+--------+--------+------+----------+------------+------+

6. Write a Query to delete table from Database.


→DROP TABLE STU;

You might also like