Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

CP INTERNATIONAL SCHOOL FARRUKHABAD

ACADEMIC YEAR : 2021-22

COMPUTER SCIENCE

PRACTICAL FILE

ON

PYTHON PROGRAMS & MYSQL


TERM (II)

Submitted to:
Submitted by: Mr.C K KATIYAR

---------------------------------------------

Roll No.-

C p International School ,Farrukhabad

Page | 1
INDEX

P1. Write a menu based program to demonstrate operation on a stack.


P2. Write a menu based program to demonstrate operations on a queue.
P3. Python –MYSQL Connectivity
P4. Integrate SQL with Python by importing the MySQL module.
P5. Integrate SQL with Python by importing the pymysql module.
P6. MySQL Queries.

P1. #Write a menu based program to demonstrate operation on a stack.

def isEmpty(stk):

if len(stk)==0:
return True
else:
return False
#----------------------------------------------------------------------

def push(stk,n):
stk.append(n)
#----------------------------------------------------------------------

def pop(stk):
if isEmpty(stk):
print("UNDERFLOW CONDITION")
else:
print("Deleted element:",stk.pop())
#----------------------------------------------------------------------
def peek(stk):
return stk[-1]
#----------------------------------------------------------------------

def display(stk):
if isEmpty(stk):
print("No Element Present")
else:
for i in range(-1,-len(stk)-1,-1):
if i==-1:
print("TOP",stk[i])
else:
print(" ",stk[i])
#----------------------------------------------------------------------
#main
stk=[]
while True:
print("Stack operations")
print("1.PUSH")
print("2.POP")
Page | 2
print("3.PEEK")
print("4.DISPLAY STACK")
print("5.EXIT")
ch=int(input("Enter the choice"))

if ch==1:
n=input("Enter the element to PUSH")
push(stk,n)
print("Element pushed")
elif ch==2:
pop(stk)
elif ch==3:
if isEmpty(stk):
print("UNDERFLOW CONDITION")
else:
print(peek(stk))
elif ch==4:
display(stk)
elif ch==5:

break
else:
print("INVALID CHOICE ENTERED")
print("THANKS FOR USING MY SERVICES")

Stack operations
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
Enter the choice1
Enter the element to PUSH98
Element pushed
Stack operations
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
Enter the choice1

Enter the element to PUSH76


Element pushed
Stack operations
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
Enter the element to PUSH89
Element pushed
Stack operations
1.PUSH
2.POP
3.PEEK
Page | 3
4.DISPLAY STACK
5.EXIT
Enter the choice4
TOP 89
76
98
Stack operations
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
Enter the choice2
Enter Deleted element: 89
Stack operations
1.PUSH
2.POP
3.PEEK
4.DISPLAY STACK
5.EXIT
Enter the choice4
TOP 76
98

P2. # Write a menu based program to demonstrate operations on queue


def isEmpty(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 isEmpty(qu):
print("UNDERFLOW CONDITION")
else:
a= qu.pop(0)
print("ELEMENT DELETED:",a)
#----------------------------------------------------------------------
def peek(stk):
return stk[-1]

def display(qu):
if isEmpty(qu):
print("NO ELEMENT PRESENT")

Page | 4
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\t1.ENQUEUE")
print("\t\t2.DEQUEUE")
print("\t\t3.DISPLAY QUEUE")
print("\t\t4.PEEK")
print(“\t\t5.EXIT")
ch=int(input("\t\tEnter 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==3:
display(qu)
elif ch==4:
if isEmpty(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: Mahesh

ELEMENT HAS BEEN INSERTED


QUEUE OPERATIONS

Page | 5
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 Mahesh
Python
REAR Selenium

QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4.PEEK
5.EXIT
Enter your choice 2

ELEMENT DELETED: Mahesh


QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE
4.PEEK
5.EXIT
Enter your choice 3

FRONT Python
REAR Selenium
QUEUE OPERATIONS
1.ENQUEUE
2.DEQUEUE
3.DISPLAY QUEUE

Page | 6
4.PEEK
5.EXIT
Enter your choice 5
THANKS FOR USING MY SERVICES

P3. #Python –MYSQL 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.execute("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 sucessfully modified")

Page | 7
#----------------------------------------------------------------------

def delete():
find=input("Enter the column name using which you want to find the record\nNOTE: NO TWO
RECORDS SHOULD HAVE SAME VALUE FOR THIS COLUMN: ")

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,p
asswd=my_sql_password)
cur=mycon.cursor()
if mycon.is_connected():
print("Successfully Connected to Database")
else:
print("Connection Faliled")
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 Recod")
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")
Enter the database Mahesh
Enter the password for MySQL aps#1
Enter the table name EMP

Successfully Connected to Database


1. Insert Record

Page | 8
2. Display Record
3. Search Record
4. Modify Record
5. Delete Recod
6. Exit
Enter the choice 1

NOTE: Please enter string/varchar/date values (if any) in quotes Enter the EMPNO 120
NOTE: Please enter string/varchar/date values (if any) in quotes Enter the EMPNAME "Mahesh
Upadhyay"
NOTE: Please enter string/varchar/date values (if any) in quotes Enter the DEPT "Computer"
NOTE: Please enter string/varchar/date values (if any) in quotes Enter the DESIGN "Coder"
NOTE: Please enter string/varchar/date values (if any) in quotes Enter the basic 100000
NOTE: Please enter string/varchar/date values (if any) in quotes Enter the CITY "Nainital"
Record successfully inserted
1. Insert Record
2. Display Record
3. Search Record
4. Modify Record
5. Delete Recod
6. Exit
Enter the choice 2
Enter the number of records to display 10
(111, 'Akash Narang', 'Account', 'Manager', 50000, 'Dehradun')
(112, 'Vijay Duneja', 'Sales', 'Clerk', 21000, 'lucknow')
(113, 'Kunal Bose', 'Computer', 'Programmer', 45000, 'Delhi')
(114, 'Ajay Rathor', 'Account', 'Clerk', 26000, 'Noida')
(115, 'Kiran Kukreja', 'Computer', 'Operator', 30000, 'Dehradun')
(116, 'Piyush Sony', 'Sales', 'Manager', 55000, 'Noida')
(117, 'Makrand Gupta', 'Account', 'Clerk', 16000, 'Delhi')
(118, 'Harish Makhija', 'Computer', 'Programmer', 34000, 'Noida')
(120, 'Mahesh Upadhyay', 'Computer', 'Coder', 100000, 'Nainital')
1. Insert Record
2. Display Record
3. Search Record
4. Modify Record
5. Delete Recod
6. Exit
Enter the choice 3
Enter the column name using which you want to find the record EMPNO Enter the
EMPNO of that record 120
[(120, 'Mahesh Upadhyay', 'Computer', 'Coder', 100000, 'Nainital')]
1. Insert Record
2. Display Record
3. Search Record
4. Modify Record
5. Delete Recod
6. Exit
Enter the choice 4

Enter the field name to modify basic


Enter the column name using which you want to find the record EMPNAME Enter the
EMPNAME of that record Mahesh Upadhyay

Page | 9
Enter the new basic 200000
Record sucessfully modified
1. Insert Record
2. Display Record
3. Search Record
4. Modify Record
5. Delete Recod
6. Exit
Enter the choice 5
Enter the column name using which you want to find the record
NOTE: NO TWO RECORDS SHOULD HAVE SAME VALUE FOR THIS COLUMN: EMPNO
Enter the EMPNO of that record 120
Record successfully deleted

4#: Integrate SQL with Python by importing the MySQL module.


import mysql.connector as sqltor
mycon=sqltor.connect(host="localhost",user="root",passwd="tiger",database="gvkcv")
if mycon.is_connected()==False:
print("error connecting to database")
cursor=mycon.cursor()
cursor.execute("select *from student10")
data=cursor.fetchall()
for i in data:
print(i)
mycon.close()

5#: Integrate SQL with Python by importing the pymysql module.

import pymysql as pym


mycon=sqltor.connect(host="localhost",user="root",passwd="tiger",database="gvkcv")
cursor=mycon.cursor()
cursor.execute("select *from student10")
data=cursor.fetchall()
for i in data:
print(i)
mycon.close()

MYSQL QUERIES:

6# Create a student table and insert data. Implement the following SQL commands on
the student table:
 ALTER table to add new attributes / modify data type / drop attribute
 UPDATE table to modify data
 ORDER By to display data in ascending / descending order
 DELETE to remove tuple(s)
 GROUP BY and find the min, max, sum, count and average

#Switched to a database
mysql> USE GVKCV;
Database changed
#Creating table student
Page | 10
mysql> create table student
-> (ROLLNO INT NOT NULL PRIMARY KEY,
-> NAME CHAR(10),
-> TELUGU CHAR(10),
-> HINDI CHAR(10),
-> MATHS CHAR(10));
Query OK, 0 rows affected (1.38 sec)
#Inserting values into table
mysql> insert into student
-> values(101,"student1",50,51,52),
-> (102,"student2",60,61,62),
-> (103,"student3",70,71,72),
-> (104,"student4",80,81,82),
-> (105,"student5",90,91,92),
-> (106,"student6",40,41,42),
-> (107,"student7",63,64,65);
Query OK, 7 rows affected (0.24 sec)
Records: 7 Duplicates: 0 Warnings: 0
#Adding new attribute computers
mysql> alter table student
-> add (computers char(10));
Query OK, 0 rows affected (1.13 sec)
Records: 0 Duplicates: 0 Warnings: 0

#Describing table

mysql> desc student;


+------------+----------+-------+------+---------+-------+
| Field | Type | Null | Key| Default | Extra |
+------------+----------+-------+-----+-----------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
| computers| char(10) | YES | | NULL | |
+-----------+----------+------+-----+---------+-------+
6 rows in set (0.21 sec)

mysql> alter table student


-> modify column computers varchar(10);
Query OK, 7 rows affected (2.38 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> desc student;


+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |

Page | 11
| computers | varchar(10) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+

6 rows in set (0.11 sec)

#Droping a attribute
mysql> alter table student
-> drop column computers;
Query OK, 0 rows affected (0.93 sec)
Records: 0 Duplicates: 0 Warnings: 0
#Describing table
mysql> desc student;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| ROLLNO | int | NO | PRI | NULL | |
| NAME | char(10) | YES | | NULL | |
| TELUGU | char(10) | YES | | NULL | |
| HINDI | char(10) | YES | | NULL | |
| MATHS | char(10) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
5 rows in set (0.14 sec)
#UPDATE DATA TO MODIFY DATA
#ACTUAL DATA
mysql> select *from student;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 101 | student1 | 50 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |
| 107 | student7 | 63 | 64 | 65 |
+--------+----------+--------+-------+-------+
7 rows in set (0.00 sec)

#UPDATE THE MARKS FOR ATTRIBUTE TELUGU FOR THE STUDENT101


mysql> UPDATE STUDENT
-> SET TELUGU=99
-> WHERE ROLLNO=101;
Query OK, 1 row affected (0.12 sec)
Rows matched: 1 Changed: 1 Warnings: 0
#DATA IN THE TABLE AFTER UPDATING
mysql> SELECT *FROM STUDENT;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 101 | student1 | 99 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |

Page | 12
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |
| 107 | student7 | 63 | 64 | 65 |
+--------+----------+--------+-------+-------+
7 rows in set (0.00 sec)

#ORDER BY DESCENDING ORDER


mysql> SELECT *FROM STUDENT
-> ORDER BY HINDI DESC;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 105 | student5 | 90 | 91 | 92 |
| 104 | student4 | 80 | 81 | 82 |
| 103 | student3 | 70 | 71 | 72 |
| 107 | student7 | 63 | 64 | 65 |
| 102 | student2 | 60 | 61 | 62 |
| 101 | student1 | 99 | 51 | 52 |
| 106 | student6 | 40 | 41 | 42 |
+--------+----------+--------+-------+-------+
7 rows in set (0.05 sec)

#ORDER BY ASCENDING ORDER


mysql> SELECT *FROM STUDENT
-> ORDER BY HINDI ASC;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 106 | student6 | 40 | 41 | 42 |
| 101 | student1 | 99 | 51 | 52 |
| 102 | student2 | 60 | 61 | 62 |
| 107 | student7 | 63 | 64 | 65 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
+--------+----------+--------+-------+-------+
7 rows in set (0.00 sec)

#DELETING A TUPLE FROM THE TABLE


mysql> DELETE FROM STUDENT
-> WHERE ROLLNO=101;
Query OK, 1 row affected (0.14 sec)
mysql> SELECT *FROM STUDENT;
+--------+----------+--------+-------+-------+
| ROLLNO | NAME | TELUGU | HINDI | MATHS |
+--------+----------+--------+-------+-------+
| 102 | student2 | 60 | 61 | 62 |
| 103 | student3 | 70 | 71 | 72 |
| 104 | student4 | 80 | 81 | 82 |
| 105 | student5 | 90 | 91 | 92 |
| 106 | student6 | 40 | 41 | 42 |

Page | 13
| 107 | student7 | 63 | 64 | 65 |
+--------+----------+--------+-------+-------+
6 rows in set (0.06 sec)

#ORDER BY BRANCH
#ACTUAL DATA
mysql> SELECT *FROM STUDENT;
+--------+--------+----------+--------+-------+-------+
| ROLL |BRANC |NAME | TELUGU| HINDI|MATHS|
+--------+--------+----------+--------+-------+-------+
| 102 | MPC | student2 | 60 | 61 | 62 |
| 103 | BIPC | student3 | 70 | 71 | 72 |
| 104 | BIPC | student4 | 80 | 81 | 82 |
| 105 | BIPC | student5 | 90 | 91 | 92 |
| 106 | BIPC | student6 | 40 | 41 | 42 |
| 107 | MPC | student7 | 63 | 64 | 65 |
+--------+--------+----------+--------+-------+-------+
6 rows in set (0.00 sec)

mysql> SELECT BRANCH,COUNT(*)


-> FROM STUDENT
-> GROUP BY BRANCH;
+--------+----------+
| BRANCH | COUNT(*) |
+--------+----------+
| MPC | 2|
| BIPC | 4|
+--------+----------+
2 rows in set (0.01 sec)
#e min, max, sum, count and average
mysql> SELECT MIN(TELUGU) "TELUGU MIN MARKS"
-> FROM STUDENT;
+------------------+
| TELUGU MIN MARKS |
+------------------+
| 40 |
+------------------+
1 row in set (0.00 sec)

mysql> SELECT MAX(TELUGU) "TELUGU MAX MARKS"


-> FROM STUDENT;
+------------------+
| TELUGU MAX MARKS |
+------------------+
| 90 |
+------------------+
1 row in set (0.00 sec)

Page | 14
mysql> SELECT SUM(TELUGU) "TELUGU TOTAL MARKS"
-> FROM STUDENT;
+--------------------+
| TELUGU TOTAL MARKS |
+--------------------+
| 403 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT COUNT(ROLLNO)


-> FROM STUDENT;
+---------------+
| COUNT(ROLLNO) |
+---------------+
| 6|
+---------------+
1 row in set (0.01 sec)
mysql> SELECT AVG(TELUGU) "TELUGU AVG MARKS"
-> FROM STUDENT;
+-------------------+
| TELUGU AVG MARKS |
+-------------------+
| 67.16666666666667 |
+-------------------+

Page | 15

You might also like