20 Prgs-Practical
20 Prgs-Practical
20 Prgs-Practical
COMPUTER SCIENCE
PRACTICAL FILE ON
PYTHON PROGRAMS AND MY SQL
Session-2023-24
Contents of file:
Python is an interpreted, high-level, general purpose
programming language.
Created by Guido Van Rossum and first released in
1991.
Python’s design philosophy emphasizes code
readability.
Its language constructs and object-oriented approach
aim to help programmers write, clear logical code.
*****FILE END*****
Contents of file:
Python is an interpreted high-level, general-purpose,
programming language.
Created by Guido Van Rossum and first released in
1991.
Python’s design philosophy emphasizes code
readability.
Its language constructs and object-oriented approach
aim to help programmers write clear, logical code.
*****FILE END*****
STACK OPERATIONS
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
ENTER THE CHOICE 1
ENTER THE ELEMENT TO PUSH 76
ELEMENT PUSHED
STACK OPERATIONS
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
ENTER THE CHOICE 1
ENTER THE ELEMENT TO PUSH 89
ELEMENT PUSHED
STACK OPERATION
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
ENTER THE CHOICE 4
TOP 89
76
98
STACK OPERATIONS
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
ENTER THE CHOICE 2
DELETED ELEMENT:89
STACK OPERATIONS
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
ENTER THE CHOICE 4
TOP 76
98
17.#WRITE A MENU BASED PROGRAM TO
DEMONSTRATE OPERATIONS ON QUEUE.
def is empty(qu):
if len(qu)==0:
return true
else:
return false
def ENQUEUE(qu,item):
qu.append(item)
if len(qu)==1:
rear=front=0
else:
rear=len(qu)-1:
front=0
def DEQUEUE(qu):
if is empty(qu):
print(“UNDERFLOW CONDITION”)
else:
a=qu.pop(0)
print(“ELEMENT DELETED:”, a)
def peek(stk):
return stk[-1]
def display(qu):
if is empty(qu):
print(“NO ELEMENT PRESNT”)
else:
for I in range(len(qu)):
if i==0:
print(“FRONT”, qu[i])
elif i==len(qu)-1:
print(“REAR”, qu[i])
else:
print(“ ” ,qu[i])
#main
qu[]
while true:
print(“\t\t QUEUE OPERATIONS”)
print(“\t\t 1.ENQUEUE”)
print(“\t\t2.DEQUEUE”)
print(“\t\t3.DISPLAY QUEUE”)
print(“\t\t4.PEEK”)
print(“\t\t5.EXIT”)
ch=int(input(“\t\t ENTER YOUR CHOICE:”))
if ch==1:
x=input(“Enter the element to be inserted:”)
ENQUEUE(qu,x)
print(“ELEMENT HAS BEEN INSERTED”)
elif ch==2:
DEQUEUE(qu)
elif ch==4:
display(qu)
if is empty(qu):
print(“UNDERFLOW CONDITION”)
else:
print(peek(qu))
elif ch==5:
break
else:
print(“INVALID CHOICE ENTERED”)
print(“THANKS FOR USING MY SERVICES”)
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4.PEEK
5.EXIT
ENTER YOUR CHOICE 1
ENTER THE ELEMENT TO BE INSERTED
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4.PEEK
5.EXIT
ENTER YOUR CHOICE 1
ENTER THE ELEMENT TO BE INSERTED:PYTHON
ELEMENT HAS BEEN INSERTED
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4.PEEK
5.EXIT
ENTER YOUR CHOICE 1
ENTER THE ELEMENT TO BE INSERTED:SELENIUM
ELEMENT HAS BEEN INSERTED
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4. PEEK
5.EXIT
ENTER YOUR CHOICE 3
FRONT RITESH
PYTHON
REAR SELENIUM
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4.PEEK
5.EXIT
ENTER YOUR CHOICE 2
ELEMENT DELETED:RITESH
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4.PEEK
5.EXIT
ENTER YOUR CHOICE 3
FRONT PYTHON
FRONT SELENIUM
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4.PEEK
5.EXIT
ENTER YOUR CHOICE 5
THANKS FOR USING MY SERVICES.
import csv
fh=open (“Student.csv”, “w”)
stuwriter=csv.writer (fh)
stuwriter.writerow ([‘Rollno’, ‘Name’, ‘Marks’])
for i in range (5):
print (“Student record”, (i+1))
rollno=int (input (“Enter rollno:”))
name=input (“Enter Name:”)
marks=float (input(“Enter marks:”))
sturec=[rollno, name, marks]
stuwriter.writerow(sturec)
fh.close( )
fh=open ("Student.csv", "r", newline='\r\n')
stureader=csv.reader(fh)
for rec in stureader:
print(rec)
19.#PYTHON – MY SQL CONNECTIVITY.
Import mysql.connector
def insert():
cur.execute(“desc{}”, format(table_name))
data=cur.fetchall()
full_input= “ ”
for I in data:
print(“NOTE:Please enter string/varchar/date
values(if any)in quotes”)
print(“Enter the”, i[0],end= “ ” )
single_value=input()
full_input=full_input+single_value+ “,”
full_input=full_input.rstrip(“,”)
cur.execute(“Insert
into{}values({})”.format(table_name,full_input))
mycon.commit()
print(“Record successfully inserted”)
def display():
n =int(input(“Enter the number of records to display”)
cur.execute(“Select*from{}”. Format(table_name))
for I in cur. fetchmany(n):
print(i)
def search():
find= input(“Enter the column name using which
you want to find the record”)
print(“Enter the”,find, “of that record”, end= “ ”)
find_value=input()
cur.exeute(“select*from{}where{}={}”.
format(table_name, find, find_value))
print(cur.fetchall())
def modify():
mod =input(“Enter the field name to modify”)
find =input(“Enter the column name using which
you want to find the record”)
print(“Enter the”, find, “of that record”, end= “ ”)
find_value=input()
print(“Enter the new”, mod,end= “ ”)
mod_value=input()
cur.execute(“update{} set{}= ‘{}’ where{}=
‘{}’.format(table_name, mod, mod_value, find, find
_value)
mycon.commit()
print(“Record successfully modified”)
def delete():
find =input(“Enter the column name using which
you want to find the record)
print(“Enter the”, find, “of that record”, end= “ ”)
find_value=input()
cur.execute(“delete from{} where{}= ‘{}’.
format(table_name, find, find_value))
mycon.commit()
print(“Record successfully deleted”)
#_main_
database_name=input(“Enter the database”)
my_sql_password=input(“Enter the password for
MYSQL”)
table_name=input(“Enter the table name”)
mycon=mysql.connector.connect(host=”localhost”
, user= “root”,
database=database_name,password=my_sql_
password)
cur=mycon.cursor()
if mycon.is_connected():
print(“Successfully connected to database”)
else:
print(“Connection failed”)
while True:
print(“\t\t1.Insert Record”)
print(“\t\t2.Display Record”)
print(“\t\t3.Search Record”)
print(“\t\t4.Modify Record”)
print(“\t\t5.Delete Record”)
print(“\t\t6.Exit\n”)
ch =int(input(“Enter the choice”)
if ch==1:
insert()
elif ch==2:
display()
elif ch==3:
search()
elif ch==4:
modify()
elif ch==5:
delete()
elif ch==6:
mycon.close()
break
else:
print(“Invalid choice entered”)