0% found this document useful (0 votes)
4 views13 pages

Practical Programs

The document contains a series of Python programs that perform various file operations, including reading and writing to text, binary, and CSV files, as well as implementing stack operations. Additionally, it includes SQL queries for retrieving and manipulating data from multiple database tables related to products, students, books, workers, and travel. Each section of the document provides specific functionalities and examples of how to implement them in Python and SQL.
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)
4 views13 pages

Practical Programs

The document contains a series of Python programs that perform various file operations, including reading and writing to text, binary, and CSV files, as well as implementing stack operations. Additionally, it includes SQL queries for retrieving and manipulating data from multiple database tables related to products, students, books, workers, and travel. Each section of the document provides specific functionalities and examples of how to implement them in Python and SQL.
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

1).Write a Program to read a text file and display each word separated by”#”.

def WordaddHash():

with open ("[Link]") as fin:

data=[Link] ()

word=[Link]()

for i in word:

print (i, "#", end=" ")

WordaddHash ()

2).Write a Program to read a text file and print the lines that are starting with “T”.

def PrintSpecificLines():

fin=open('[Link]','r')

data=[Link]()

for line in data:

if line[0] =='T':

print(line)

[Link]()

PrintSpecificLines ()

3). Write a Program to read a text file count and print the [Link] occurance of “is” and “and” in a text file.

def Countisand():

fin=open ('[Link]', 'r')

data= [Link]()

word=[Link]()

count=0

for i in word:

if i=='is' or i== "and":

count+=1

print("The total no. of words with is/and=",count)

[Link]()

Countisand()
4).Write a Program to read a text file and print the lines that are ending with “e” or “g”

def DisplayLast():

fin=open('[Link]','r')

data=[Link]()

print(data)

for line in data:

if line[-2] =='e'or line[-2] =='g':

print(line)

[Link]()

DisplayLast ()

5).Write a Python program to store and search the details of the binary file in list datatype.

import pickle

def bwrite():

fout=open("[Link]","ab")

while True:

rno=int(input("Enter the rollno:"))

name=input("Enter the name:")

marks=int(input("Enter the marks:"))

data=[rno,name,marks]

[Link](data,fout)

ch=input("Enter your choice(y/n)")

if ch =="n" or ch=="N":

break

[Link]()

def Bsearch():

fin=open("[Link]","rb")

rno=int(input("Enter the rno of the record to search"))

try:

while True:
data=[Link](fin)

if data[0]==rno:

print("Record found")

print(data)

break

except Exception:

[Link]()

bwrite()

Bsearch()

6).Write a Python program for the following operations,

i).Write a function Createdvd() to write the following contents into the binary file, [Link] that has the
following structure: {MNO:[MNAME, MTYPE]}
ii).Write a user defined function, showdvd(mtype), that accepts mtype as parameter and displays all the records
from the binary file [Link], that have the value of Movie Type as mtype.

import pickle

def Createdvd():

fout=open("[Link]","ab")

M={}

while True:

mno=int(input("Enter the movieno:"))

mname=input("Enter the MovieName:")

mtype=input("Enter the movie type:")

M[mno]=[mname,mtype]

ch=input("Enter your choice(y/n):")

if ch in "Nn":

break

[Link](M,fout)

[Link]()

def showdvd(mtype):
fin=open("[Link]","rb")

try:

while True:

data=[Link](fin)

for i in data:

if data[i][1]==mtype:

print(i,data[i])

except EOFError:

[Link]()

Createdvd()

mtype=input("Enter themovie type")

showdvd(mtype)

7).Write aPython program to store and retrieve the contents of a CSV file .

import csv

def writecsv():

fout=open("[Link]","a")

stuwriter=[Link](fout,delimiter="|")

while True:

Rno=int(input("Enter the rollno:"))

Name=input("Enter the Name")

Marks=int(input("Enter the marks"))

sturec=[Rno,Name,Marks]

[Link](sturec)

ch=input("Enter your choice (Y/N)")

if ch in "Nn":

break

[Link]()

def readcsv():

fin=open("[Link]","r",newline="\n")
stureader=[Link](fin)

print("student details:")

for rec in stureader:

print(rec)

[Link]()

writecsv()

readcsv()

8). Write a python program to store the details of student’s participation in sports and display the details
of the student who won the game.

import csv

def Accept():

fout=open("[Link]","a",newline="")

wr=[Link](fout)

for i in range(3):

sid=int(input("Enter the Student ID:"))

sname=input("Enter Student Name:")

game=input("Enter name of the game:")

res=input("Enter Result")

headings=["Student Id","Student Name","Game Name","Result"]

rec=[sid,sname,game,res]

[Link](headings)

[Link](rec)

[Link]()

def Woncount():

fin=open("[Link]","r")

rd=[Link](fin)

print(next(rd))

for i in rd:

if i[3]=="Won":
print(i)

[Link]()

Accept()

Woncount()

9).Write a Menudriven Program to perform stack operations on list data type.

def isEmpty(stk):

if stk==[]:

return True

else:

return False

def push(stk,ele):

[Link](ele)

print("Element added Successsfully")

print(stk)

def pop(stk):

if isEmpty(stk):

print("Stack is Empty")

else:

print("Deleted element is:",[Link]())

def peek(stk):

if isEmpty(stk):

print("Stack is Empty....")

else:

print("Element at the top of Stack",stk[-1])

def display(stk):

if isEmpty(stk):

print("Stack is Empty")

else:

for i in range(len(stk)-1,-1,-1):
print(stk[i],end=" ")

stack=[]

while True:

print(">>>>> Stack Operations>>>>>")

print("[Link]")

print("[Link]")

print("[Link]")

print("[Link]")

print("[Link]")

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

if ch==1:

element=int(input("Enter the element which you want to insert "))

push(stack,element)

if ch==2:

pop(stack)

if ch==3:

peek(stack)

if ch==4:

display(stack)

elif ch==5:

break

10).Write a program with separate user-defined functions to perform the following operations:

(i) To create a function push(str,d) where stack is an empty list and d is dictionary of items from the
dictionary push the keys(name of the student) into a stack, where the corresponding value(marks)
is greater than 70

(ii) To create a function pop(stk),where stk is a stack implemented by a list of student names. The function
returns the items deleted from the stack.

(iii)To display the elements of the stack (after performing push or pop)

SOURCE CODE:

stk=[]
d={"Ramesh":58, "Umesh":78, "Vishal":90, "Khushi":60, "Ishika":95}

def push(stk,d):

for i in d:

if d[i]>70:

[Link](i)

print(stk)

def Pop(stk):

while True:

if stk==[]:

print( "Stack is Empty")

break

else:

print("The items in stack")

return [Link]()

def display():

if stk==[]:

print( "Stack is Empty")

else:

top=len(stk)-1

for i in range(top,-1,-1):

print(stk[i])

print("Stack operations")

while True:

print("[Link]:")

print("[Link]")

print("[Link]")

print("[Link]")

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

if opt==1:
push(stk,d)

elif opt==2:

Pop(stk)

elif opt==3:

display()

else:

break

SQL QUERY
Table1:Gym

PrCode PrName UnitPrice Manufacturer

P101 Cross Trainer 25000 Avon Fitness

P102 Tread Mill 32000 A G fitline

P103 Massage Chai 20000 Fit Express

P104 Vibration Trainer 22000 Avon Fitness

P105 Bike 13000 Fit Express

Write the MYSQL statement for the following:

1. Display details of all the products with UnitPrice in the range 20000 to 30000.

2. Display the names of all the product by the Manufacturer “Avon Fitness” in descending order of unit price.

3. Increase 10% of UnitPrice for all the products.

4. Display the details all the products with Manufacturer name starting with “A”.
Table2: Student

Roll No Name Class DOB Gender City Marks

1 Nanda X 06-06-1995 M Agra 551

2 Sauraph XII 07-05-1993 M Mumbai 462

3 Sanal XI 06-05-1994 F Delhi 400

4 Trisla XII 08-08-1995 F Mumbai 450

5 Store XII 08-10-1995 M Delhi 365

6 Marisla XI 12-12-1994 F Dubai 250

7 Neha X 08-12-1995 F Moscow 377

8 Nishanth X 12-06-1995 M Moscow 489

Write the MYSQL statement for the following:

1. Display the details of the students in reverse alphabetical order as per the name

2. To display the number of the cities available in the table

3. To display the Name, Class and Total Number of Students who have secured more than 450 marks class wise

4. To display the Class, DOB and City whose marks in the range of 450 to 551

Table3: Books

Book_Id Book_Name Author Name Publishers Price Type Qty

F0001 The Tears William Hopkins First Publ. 750 Fiction 10

F0002 Thunder Bolts Anna Roberts First Publ. 700 Fiction 5

T0001 My First C++ Brain & Books EPB 250 Text 10

T0002 C++ Brainworkers A.W Rossaine TDM 325 Text 5

C0001 Fast Cook LattaKapoor EPB 350 Cook 8


Table3: Issued

Book_Id Quantity_Issued

F0001 3

T0001 1

C0001 5

Write the MYSQL statement for the following:

1. To display the name of the books in descending order of their Price


2. To display the Book_Id, Book_Name and Quantity_Issued for all books which have been issued
3. To show the Book Name, Author Name and Price from EPB Publishers.
4. To increase the price of all the books of first publ by 50

Table4: Workers

W_Id First Name Last Name Address City

102 Sam Tones 33 Elm St. Paris

105 Sarah Ackerman 440 U.S.110 New York

144 Manila Sengupta 24 Friends Street New Delhi

210 George Smith 83 First Street Howard

255 Mary Jones 842 Vine Ave. Los Angle

300 Robert Samuel 9 Fifth Cross Washington

335 Henry Williams 12Moore street Boston

403 Ronny Lee 121 Harrison St New York

451 Pat Thompson 11 Red Road Paris


Table 4: Design

W_Id Salary Benefits Designation

102 75000 15000 Manager

105 85000 25000 Director

144 70000 15000 Manager

210 75000 12500 Manager

255 50000 12000 Clerk

300 45000 10000 Clerk

335 40000 10000 Clerk

403 32000 7500 Salesman

451 28000 7500 Salesman

1. To display the FirstName, City and Total salary of all clerks from the table where total salary=salary +
Benefits.
2. To display the content of the workers in ascending order of FirstName.
3. Increase the benefits of the entire salesman by 10% in the table.
4. To display the minimum salary of employee of each designation.

Table5: Travel

CNO CNAME TRAVELDATE KM VCODE NOP

101 [Link] 2015-12-13 200 V01 32

103 FedrickSym 2016-03-21 120 V03 45

105 Hitesh jain 2016-04-23 450 V02 42

102 Ravi anish 2016-01-13 80 V02 40

107 John malina 2015-02-10 65 V04 2

104 Sahanubhuti 2016-01-18 90 V05 4

106 Ramesh jaya 2016-04-06 100 V01 25


Table5:Vehicle

VCODE VEHICLE TYPE PERKM

V01 Volvo Bus 150

V02 Ac Delux Bus 125

V03 Ordinary Bus 80

V05 Suv 30

V04 Car 18

[Link] display the CNo and Cname of these customers from the table travel who are travelled between “2015-
05-01” and “2015-12-31”

2. To display CNo, CName, TravelDate from the table travel in descending order of CNo

3. To display the CName of all customers from the table travel who are travelling by vehicle with code V01 or
V02.

4. To display all the details from table travel for the customers, who have travel distance more than 120km in

ascending order of NOP

You might also like