0% found this document useful (0 votes)
34 views38 pages

Class 12 Prac 1-1

Uploaded by

Sarvesh Madhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views38 pages

Class 12 Prac 1-1

Uploaded by

Sarvesh Madhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 38

---------------------------------------------------CODE 1-------------------------------------------------------------------------

#SBIOAIS XII
print("\tSTRING MANIPULATION IN A LIST USING FUNCTION")
print("\t-----------------------------------------------------------------")
print("INPUT DATA")
print("----------------")
def str(l):
print("OUTPUT DATA")
print("------------------")
c=0
for i in l:
k=len(i)-1
for j in range(len(i)):
if i[j]!=i[k]:
break
k-=1
else:
c+=1
print("No of palindromes: ",c)
m=len(l[0])
for i in range(0,len(l)):
if len(l[i])>=m:
m=len(l[i])
for j in l:
if m==len(j):
print("Longest word: ",j)
l=eval(input("Enter a list of strings: "))
str(l)

---------------------------------------------------OUTPUT-----------------------------------------------------------------------

STRING MANIPULATION IN A LIST USING FUNCTION


-----------------------------------------------------------------
INPUT DATA
----------------
Enter a list of strings: ['abc','malayalam']
OUTPUT DATA
------------------
No of palindromes: 1
Longest word: Malayalam

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 2-------------------------------------------------------------------------

#SBIOAIS XII

print("\tMAXIMUM OCCURRENCES IN A GIVEN LIST USING FUNCTION")


print("\t---------------------------------------------------------------------------")
def list_func(l):
max=0
for i in l:
if l.count(i)>max:
max=i
num=l.count(max)
print("Number with maximum occurrences: ",max)
print("The number ",max," has ",num," occurrences")

l=eval(input("LIST OF INTEGERS: "))


print("\nOUTPUT VALUES")
x=list_func(l)

-----------------------------------------------OUTPUT---------------------------------------------------------------------------

MAXIMUM OCCURRENCES IN A GIVEN LIST USING FUNCTION


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

LIST OF INTEGERS: [1,23,34,1,23,3,23,34]

OUTPUT VALUES
-----------------------
Number with maximum occurrences: 23
The number 23 has 3 occurrences

----------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 3-------------------------------------------------------------------------

#SBIOAIS XII

print(“\SORTING OF PRODUCTS IN A TUPLE USING FUNCTION”)


print(“\t-----------------------------------------------------------------------“)

def tup_func:
l=list(t)
for i in range(len(l)-1):
for j in range(len(l)-1-i):
if l[j][1]>l[j+1][1]:
l[j],l[j+1]=l[j+1],l[j]
print(“Transformed tuple : ”,tuple(l))

#Main Program
t=( )
for I in range(3):
x=input(“FOOD:”)
y=int(input(“PRICE:”))
z=input(“DATE: “)
u=(x,(y,z))
t+=u
print(“\n OUTPUT VALUES”)
print(“--------------------------“)
print(“Original Tuple: “,t)
x=tup_func(t)

---------------------------------------------------OUTPUT-----------------------------------------------------------------------
SORTING OF PRODUCTS IN A TUPLE USING FUNCTION
----------------------------------------------------------------------

FOOD: Pizza
PRICE: 250
DATE: 25-07-24
FOOD: Patsa
PRICE: 100
DATE: 28-07-24
FOOD: Icecream
PRICE: 50
DATE: 22-07-24

OUTPUT VALUES
----------------------
Original Tuple : ((‘Pizza’ , (250,’25-07-24’)) , (‘Patsa’ , (100,’28-07-24’)) , (‘Icecream’, (50,’22-07-24’)))
Transformed tuple: ((‘Icecream’ , (50,’22-07-24’)) , (‘Patsa’ , (100,’28-07-24’)),
(‘Pizza’, (250,’25-07-24’)))

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 4-------------------------------------------------------------------------

#SBIOAIS XII
print(“\t SEARCHING VALUES IN DICTIONARY USING FUNCTION”)
print(“\t------------------------------------------------------------------------“)
print(“\t INPUT DATA”)
print(“\t------------------“)
n=int(input(“Enter no of records:”))
x=list()
for i in range(n):
e=input(“Enter subject:”)
f=int(input(“Enter marks=”)
d={}
d[e]=f
x+=d,
print(x)

def dic(d):
print(“\t OUTPUT DATA”)
print(“\t--------------------“)
l=[]
global a
for i in x:
for j in i.keys():
if j==a:
l+=list(i.values())
print(“\t List of the marks=”,l)

a=input(“\t Enter subject =”)


dic(d)

---------------------------------------------------OUTPUT-----------------------------------------------------------------------
SEARCHING VALUES IN A DICTIONARY USING FUNCTION
--------------------------------------------------------------------------
INPUT DATA
----------------
Enter no of records : 4
Enter subject : Maths
Enter marks=56
Enter subject : Science
Enter marks=67
Enter subject : Maths
Enter marks=78
Enter subject : Science
Enter marks=89
[{‘Maths’: 56} , { ‘Science’ : 67} , { ‘Maths’ : 78} , {‘Science’ : 89} ]

OUTPUT DATA
-------------------
Enter subject = Maths
List of the marks=[ 56 , 78]
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 5-------------------------------------------------------------------------

#SBIOAIS XII
import random
while True:
Choice=input ("\nDo you want to roll the dice (y/n):")
no=random.randint(1,6)
if Choice=='y':
print("\nYour Number is:",no)
else:
break

---------------------------------------------------OUTPUT-----------------------------------------------------------------------

Do you want to roll the dice (y/n):y


Your Number is: 3
Do you want to roll the dice (y/n):y
Your Number is: 4
Do you want to roll the dice (y/n):n

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

---------------------------------------------------CODE 6-------------------------------------------------------------------------

#SBIOAIS XII
f=open ("Story.txt","r")
Contents=f.readlines ()
for line in Contents:
words=line. split ()
for i in words:
print(i+'#',end=' ')
print (" ")
f.close ()
--------------------------------------------------------------------------------------------------------------------------------------
Story.txt :
Like a Joy on the heart of a sorrow.
The sunset hangs on a cloud.

---------------------------------------------------OUTPUT----------------------------------------------------------------------

Like# a# Joy# on# the# heart# of# a# sorrow. #


The# sunset# hangs# on# a# cloud. #

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 7-------------------------------------------------------------------------

#SBIOAIS XII
f=open ("Story.txt",'r')
Contents=f.read()
Vowels=0
Consonants=0
Lower_case=0
Upper_case=0
for ch in Contents:
if ch in 'aeiouAEIOU':
Vowels+=1
if ch in 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ':
Consonants+=1
if ch.islower() :
Lower_case+=1
if ch.isupper() :
Upper_case+=1
f.close ()
print ("The total numbers of vowels in the file:",Vowels)
print ("The total numbers of consonants in the file:",Consonants)
print ("The total numbers of uppercase in the file:",Upper_case)
print ("The total numbers of lowercase in the file:", Lower_case)
--------------------------------------------------------------------------------------------------------------------------------------
Story.txt :
Like a Joy on the heart of a sorrow.
The sunset hangs on a cloud.

---------------------------------------------------OUTPUT-----------------------------------------------------------------------

The total numbers of vowels in the file: 20


The total numbers of consonants in the file: 29
The total numbers of uppercase in the file: 3
The total numbers of lowercase in the file: 46

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 8-------------------------------------------------------------------------

#SBIOAIS XII
def Disp():
F=open("Poem.txt")
S=F.read()
W=S.split()
print("The following words are less than 5 characters")
for i in W:
if len(i)<5:
print(i,end=' ')
F.close()
Disp()
--------------------------------------------------------------------------------------------------------------------------------------
Poem.txt:
Dancing rays of light,
Painting colors on the sky,
Nature’s masterpiece.

---------------------------------------------------OUTPUT-----------------------------------------------------------------------

The following words are less than 5 characters


rays of on the sky,

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 9-------------------------------------------------------------------------

#SBIOAIS XII
F1=open("Sample.txt",'r')
F2=open("New.txt",'w')
S=F1.readlines()
for i in S:
if i[0]=='A' or i[0]=='a':
F2.write(i)
print("Written Successfully")
F1.close()
F2.close()
--------------------------------------------------------------------------------------------------------------------------------------
Sample.txt:
Aeroplane was invented by the Right Brothers.
My favorite color is skyblue.
An apple a day keeps the doctor away.

---------------------------------------------------OUTPUT-----------------------------------------------------------------------

Written Successfully

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

--------------------------------------------------------------------------------------------------------------------------------------
New.txt:
Aeroplane was invented by the Right Brothers.
An apple a day keeps the doctor away.
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 10-----------------------------------------------------------------------

#SBIOAIS XII
import pickle
def Create():
F=open("Students.dat",'ab')
opt='y'
while opt=='y':
Roll_No=int(input('Enter roll number:'))
Name=input("Enter Name:")
L=[Roll_No, Name]
pickle.dump (L,F)
opt=input("Do you want to add another student detail (y/n):")
F.close()

def Search():
F=open("Students.dat",'rb')
no=int (input("Enter Roll. No of student to search:"))
found=0
try:
while True:
S=pickle.load (F)
if S[0]==no:
print("The searched Roll. No is found and Details are:", S)
found=1
break
except:
F.close()
if found==0:
print("The Searched Roll. No is not found")

#Main Program
Create ()
Search ()

---------------------------------------------------OUTPUT-----------------------------------------------------------------------

Enter roll number: 1


Enter Name: Arun
Do you want to add another student detail (y/n): y
Enter roll number:2
Enter Name: Bala
Do you want to add another student detail (y/n): y
Enter roll number: 3
Enter Name: Charan
Do you want to add another student detail (y/n):n
Enter Roll. No of student to search: 3
The searched Roll.No is found and Details are: [3, 'Charan']

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 11-----------------------------------------------------------------------

#SBIOAIS XII

import pickle
def Create():
F=open ("Marks.dat", 'ab')
opt='y'
while opt=='y':
Roll_No=int(input('Enter roll number:'))
Name=input("Enter Name:")
Mark=int(input("Enter Marks:"))
L=[Roll_No, Name, Mark]
pickle.dump(L,F)
opt=input("Do you want to add another student detail (y/n):")
F.close()

def Update():
F=open ("Marks.dat",'rb+')
no=int(input("Enter Student Roll No to modify marks:"))
found=0
try:
while True:
Pos=F.tell()
S=pickle.load(F)
if S[0]==no:
print("The searched Roll No is found and Details are:",S)
S[2]=int(input("Enter Now Mark to be update: "))
F.seek(Pos)
pickle.dump(S,F)
found=1
F.seek(Pos) #moving the file pointer again beginning of the current record.
print("Mark updated Successfully and Details are:", S)
break
except:
F.close()
if found==0:
print("The Searched Roll. No is not found")

#Main Program
Create()
Update ()

----------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------OUTPUT-----------------------------------------------------------------------
Enter roll number:1
Enter Name: Arun
Enter Marks: 450
Do you want to add another student detail (y/n): y
Enter roll number: 2
Enter Name: Bala
Enter Marks: 342
Do you want to add another student detail (y/n): y
Enter roll number:3
Enter Name: Charan
Enter Marks: 423
Do you want to add another student detail (y/n):y
Enter roll number:4
Enter Name: Dinesh
Enter Marks: 356
Do you want to add another student detail (y/n): y
Enter roll number:5 Enter Name: Divya
Enter Marks: 476
Do you want to add another student detail (y/n):n
Enter Student Roll. No to modify marks:3
The searched Roll. No is found and Details are: [3, 'Charan', 423]
Enter New Mark to be update: 470
Mark updated Successfully and Details are: [3, 'Charan', 470]

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 12-----------------------------------------------------------------------
#SBIOAIS XII
import csv
def Create():
F=open("Emp.csv", 'a',newline='')
W=csv.writer (F)
opt='y'
while opt=='y':
No=int(input("Enter Employee Number:"))
Name=input("Enter Employee Name:")
Sal=float(input("Enter Employee Salary:"))
L=[No, Name, Sal]
W.writerow (L)
opt=input("Do you want to continue (y/n)?:")
F.close()

def Search():
F=open("Emp.csv",'r',newline='\r\n')
no=int(input("Enter Employee number to search"))
found=0
row=csv.reader (F)
for data in row:
if data[0]==str(no):
print("\nEmployee Details are:")
print("===============")
print("Name:", data [1])
print("Salary:", data[2])
print("==============")
found=1
break
if found==0:
print("The searched Employee number is not found")
F.close()

#Main Program
Create()
Search()

------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------OUTPUT-----------------------------------------------------------------------

Enter Employee Number: 1


Enter Employee Name: Anand
Enter Employee Salary: 23000
Do you want to continue (y/n)?:y
Enter Employee Number:2
Enter Employee Name: Akash
Enter Employee Salary: 25000
Do you want to continue (y/n)?:y
Enter Employee Number: 3
Enter Employee Name: Balu
Enter Employee Salary: 27000
Do you want to continue (y/n)?:y
Enter Employee Number: 4
Enter Employee Name: Bavya
Enter Employee Salary: 29000
Do you want to continue (y/n)?:y
Enter Employee Number: 5
Enter Employee Name: Manoj
Enter Employee Salary: 35000
Do you want to continue (y/n)?:n

Enter Employee number to search: 3

Employee Details are:


===============
Name: Balu
Salary: 27000
===============

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 13-----------------------------------------------------------------------
#SBIOAIS XII
def Push():
Doc_ID=int(input("Enter the Doctor ID:"))
Doc_Name=input("Enter the Name of the Doctor:")
Mob=int(input("Enter the Mobile Number of the Doctor:"))
Special=input("Enter the Specialization:")
if Special=='ENT':
Stack.append([Doc_ID,Doc_Name])

def Pop():
if Stack==[]:
print("Stack is empty")
else:
print("The deleted doctor detail is:", Stack.pop())

def Peck():
if Stack==[]:
print("Stack is empty")
else:
top=len(Stack)-1
print("The top of the stack is:", Stack[top])

def Disp():
if Stack==[]:
print("Stack is empty")
else:
top=len(Stack)-1
for i in range(top,-1,-1):
print(Stack[i])
Stack=[]
ch='y'
print("Performing Stack Operations Using List\n")
while ch=='y' or ch=='Y':
print()
print("1.PUSH")
print("2.POP")
print("3.PEEK")
print("4.Disp")
opt=int(input("Enter your choice:"))
if opt==1:
Push()
elif opt==2:
Pop()
elif opt==3:
Peck()
elif opt==4:
Disp()
else:
print("Invalid Choice, Try Again!!!")
ch=input("\nDo you want to Perform another operation(y/n):")
---------------------------------------------------OUTPUT-----------------------------------------------------------------------

Performing Stack Operations Using List

1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice: 1
Enter the Doctor ID:1
Enter the Name of the Doctor:Arun
Enter the Mobile Number of the Doctor:9858
Enter the Specialization: ENT

Do you want to Perform another operation(y/n):y

1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice:1
Enter the Doctor ID:2
Enter the Name of the Doctor: Usha
Enter the Mobile Number of the Doctor:8785
Enter the Specialization:Cardio

Do you want to Perform another operation(y/n):y

1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice:1
Enter the Doctor ID:3 Enter the Name of the Doctor: Murali
Enter the Specialization:ENT
Enter the Mobile Number of the Doctor:7854

Do you want to Perform another operation(y/n):y

1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice:1
Enter the Doctor ID:4
Enter the Name of the Doctor:Rani
Enter the Mobile Number of the Doctor:8778
Enter the Specialization: Anesthesia

Do you want to Perform another operation(y/n):y


1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice:4
[3, 'Murali']
[1, 'Arun']

Do you want to Perform another operation(y/n):y

1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice:3
The top of the stack is: [3, 'Murali']

Do you want to Perform another operation(y/n):y

1.PUSH
2.POP
3.РЕЕК
4.Disp
Enter your choice:2
The deleted doctor detail is: [3, 'Murali']

Do you want to Perform another operation(y/n):y

1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice:4
[1, Arun']

Do you want to Perform another operation(y/n):n

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 14-----------------------------------------------------------------------

#SBIOAIS XII
import csv
with open('users.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["user_id", "password"]) # Header
while True:
user_id = input("Enter user ID: ")
password = input("Enter password: ")
writer.writerow([user_id, password])
more = input("Do you want to add another user? (yes/no): ")
if more.lower() != 'yes':
break
print("CSV file 'users.csv' created successfully.")

with open('users.csv','r',newline='') as f:
reader=csv.reader(f)
uid=input("Enter the ID to be searched:")
for i in reader:
if i[0]==uid:
print("password for ",uid,"=",i[1])

---------------------------------------------------OUTPUT-----------------------------------------------------------------------

Enter user ID: T10


Enter password: pass
Do you want to add another user? (yes/no): yes
Enter user ID: T20
Enter password: word
Do you want to add another user? (yes/no): yes
Enter user ID: T30
Enter password: pswd
Do you want to add another user? (yes/no): no
CSV file 'users.csv' created successfully.
Enter the ID to be searched: T30
password for T30 = pswd

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 15-----------------------------------------------------------------------

#SBIOAIS XII

f=open("sample.txt",'r')
x=f.readlines()
f.close()

f1=open("x.txt",'w')
f2=open("y.txt",'w')
for i in x:
if 'a' in i:
f2.write(line)
else:
f1.write(line)
print("All line that contains a character has been removed from x.txtfile")
print("All line that contains a character has been saved from y.txt file")
f1.close()
f2.close()

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

sample.txt
I am a python
hello world

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

---------------------------------------------------OUTPUT-----------------------------------------------------------------------
All line that contains ‘A’ character has been removed from x.txtfile
All line that contains ‘A’ character has been saved to y.txt file

x.txt:
hello world

y.txt:
I am a python

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 16-----------------------------------------------------------------------

TABLE: STATIONARY

TABLE: CONSUMERS

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

QUERY-1
mysql> SELECT * FROM CONSUMERS WHERE ADDRESS='DELHI';

QUERY-2

mysql> SELECT * FROM STATIONARY WHERE PRICE BETWEEN 8 AND 15;


QUERY-3

mysql> SELECT CONSUMERS.CONSUMER_NAME, CONSUMERS.ADDRESS, STATIONARY.COMPANY,


STATIONARY.PRICE FROM CONSUMERS, STATIONARY WHERE CONSUMERS.S_ID=STATIONARY.S_ID;

QUERY-4

mysql> UPDATE STATIONARY SET PRICE=PRICE+2;

Query OK, 5 rows affected (0.00 sec)

QUERY-5

mysql> SELECT DISTINCT(COMPANY) FROM STATIONARY;

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 17-----------------------------------------------------------------------

TABLE: ITEM

TABLE: TRADERS

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

QUERY-1
mysql> SELECT * FROM ITEM ORDER BY I_NAME;

QUERY-2
mysql> SELECT I_NAME,PRICE FROM ITEM WHERE PRICE BETWEEN 10000 AND 22000;
QUERY-3
mysql> SELECT TCODE, COUNT(*) FROM ITEM GROUP BY TCODE;

QUERY-4
mysql> SELECT PRICE, I_NAME,QTY FROM ITEM WHERE QTY>150;

QUERY-5
mysql> SELECT TNAME FROM TRADERS WHERE CITY IN ('DELHI','MUMBAI');

----------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 18-----------------------------------------------------------------------

TABLE: DOCTOR

TABLE: SALARY

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

QUERY-1
mysql> SELECT NAME FROM DOCTOR WHERE DEPARTMENT='MEDICINE';
QUERY-2
mysql> SELECT AVG(BASIC+ALLOWANCE) AS SALARY FROM SALARY,DOCTOR WHERE
DEPARTMENT='ENT' AND SALARY.ID=DOCTOR.ID;

QUERY-3
mysql> SELECT MIN(ALLOWANCE) FROM SALARY,DOCTOR WHERE GENDER='F' AND
SALARY.ID=DOCTOR.ID;

QUERY-4
mysql> SELECT DOCTOR.ID, DOCTOR.NAME, SALARY.BASIC, SALARY.ALLOWANCE FROM DOCTOR,
SALARY WHERE DOCTOR.ID=SALARY.ID;
QUERY-5
mysql> SELECT DISTINCT DEPARTMENT FROM DOCTOR;

--------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------CODE 19-------------------------------------------------------------------

TABLE: STU

Query-1

mysql> SELECT NAME FROM STU WHERE DEPT='HINDI' AND GENDER='F';

Query-2

mysql> SELECT NAME FROM STU WHERE AGE BETWEEN 18 AND 20;

Query-3

mysql> SELECT NAME FROM STU WHERE NAME LIKE 'A%';

Query-4

Mysql> SELECT NAME FROM STU WHERE NAME LIKE '_N%';


-------------------------------------------------------CODE 20
-------------------------------------------------------------------

TABLE: STU

Query-1

mysql> DELETE FROM STU WHERE ROLLNO=8;

Query-2

mysql> UPDATE STU SET FEES=170 WHERE ROLLNO=1 AND FEES<130;

Query-3

mysql> ALTER TABLE STU ADD AREA VARCHAR(20);


Query-4

mysql> SELECT NAME FROM STU WHERE AREA IS NULL;

Query-5

mysql> ALTER TABLE STU DROP AREA;


-------------------------------------------------------CODE 21
-------------------------------------------------------------------

Query-1

mysql> SELECT AVG(PRICE) FROM COST WHERE COMPANY='RAYMOND';

Query-2

mysql> SELECT * FROM UNIFORM ORDER BY STOCKDATE DESC;


Query-3

mysql> SELECT COMPANY,MAX(PRICE),MIN(PRICE) FROM COST GROUP BY COMPANY;

Query-4

mysql> SELECT COMPANY, COUNT(*) FROM COST GROUP BY COMPANY HAVING COUNT(*)>2;

Query-5

mysql> SELECT U.UCODE,UNAME,UCOLOR,SIZE,COMPANY FROM UNIFORM U,COST C WHERE


U.UCODE=C.UCODE;
---------------------------------------------------CODE 22-----------------------------------------------------------------------

#SBIOAIS XII

import mysql.connector

mydb=mysql.connector.connect(host="localhost",user="root",passwd="sbioa@2023")

mycursor=mydb.cursor()

mycursor.execute("Create database CS;")

mycursor.execute("Use CS;")

print("\tFETCHING RECORDS FROM TABLE")

print("\t----------------------------------------\n")

print("\t INPUT DATA\n")

print("\t----------------\n")

mycursor.execute("Create table student(rollno int(20),name varchar(50),total_marks int(10));")

for i in range(5):

M=int(input("rollno="))

H=input("name=")

S=int(input("total marks="))

mycursor.execute("insert into student values({},'{}',{})".format(M,H,S))

mydb.commit()

print("\n\t OUTPUT DATA\n")

print("\t---------------------\n")

print("FETCHING RECORDS FROM TABLE")

print("------------------------------------------")

mycursor.execute("select * from student;")

data=mycursor.fetchall()

for i in data:

print(i)

----------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------OUTPUT-----------------------------------------------------------------------

FETCHING RECORDS FROM TABLE


----------------------------------------
INPUT DATA
----------------
rollno=01
name=RAM
total marks=690
rollno=02
name=KALYAN
total marks=349
rollno=03
name=FAHIM
total marks=691
rollno=04
name=NAVIN
total marks=600
rollno=05
name=DEV
total marks=120

OUTPUT DATA
---------------------
FETCHING RECORDS FROM TABLE
------------------------------------------
(1, 'RAM', 690)
(2, 'KALYAN', 349)
(3, 'FAHIM', 691)
(4, 'NAVIN', 600)
(5, 'DEV', 120)
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 23-----------------------------------------------------------------------

#SBIOAIS XII
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="sbioa@2023")
mycursor=mydb.cursor()
mycursor.execute("Create database CS;")
mycursor.execute("Use CS;")
print("\t\tCOUNTING RECORDS FROM TABLE")
print("\t-------------------------------------------\n")
print("\t INPUT DATA\n")
print("\t----------------\n")
mycursor.execute("Create table employee(empno int(20),name varchar(50),department
varchar(50),salary int(20));")
for i in range(5):
a=int(input("empno="))
b=input("name=")
c=input("department=")
d=int(input("salary="))
mycursor.execute("insert into employee values({},'{}','{}',{})".format(a,b,c,d))
mydb.commit()
print("\n\t OUTPUT DATA\n")
print("\t---------------------\n")
print("RECORDS FROM TABLE")
print("-----------------------------")
mycursor.execute("select * from employee;")
data=mycursor.fetchall()
for i in data:
print(i)
print("no of rows extracted:",mycursor.rowcount)

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------OUTPUT-----------------------------------------------------------------------

COUNTING RECORDS FROM TABLE


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

INPUT DATA

----------------
empno=001
name=ram
department=mech
salary=100000
empno=002
name=kalyan
department=civil
salary=150000
empno=003
name=navin
department=IT
salary=116000
empno=004
name=vishnu
department=coding
salary=11000
empno=005
name=dev
department=machine learning
salary=1000000

OUTPUT DATA

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

RECORDS FROM TABLE


-----------------------------
(1, 'ram', 'mech', 100000)
(2, 'kalyan', 'civil', 150000)
(3, 'navin', 'IT', 116000)
(4, 'vishnu', 'coding', 11000)
(5, 'dev', 'machine learning', 1000000)
no of records extracted: 5

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 24-----------------------------------------------------------------------

#SBIOAIS XII
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="sbioa@2023")
mycursor=mydb.cursor()
mycursor.execute("Create database CS;")
mycursor.execute("Use CS;")
print("\t\tSEARCHING RECORDS FROM TABLE")
print("\t----------------------------------------\n")
print("\t INPUT DATA\n")
print("\t----------------\n")
mycursor.execute("Create table watches(watchid int(20),watchname varchar(30),price
int(30),quantity int(20));")
for i in range(5):
a=int(input("watchid="))
b=input("watchname=")
c=int(input("price="))
d=int(input("quantity="))
mycursor.execute("insert into watches values({},'{}',{},{})".format(a,b,c,d))
mydb.commit()
print("\n\t OUTPUT DATA\n")
print("\t---------------------\n")
print("SEARCHING RECORDS FROM TABLE")
print("--------------------------------------------")
mycursor.execute("select * from watches;")
data=mycursor.fetchall()
for i in data:
print(i)
b=input("Enter a watchname=")
mycursor.execute("select * from watches where watchname='{}'".format(b))
data1=mycursor.fetchall()
for i in data1:
print(i)
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------OUTPUT-----------------------------------------------------------------------

SEARCHING RECORDS FROM TABLE


----------------------------------------
INPUT DATA
----------------

watchid=100
watchname=rolex
price=10000000
quantity=10
watchid=110
watchname=titan
price=10000
quantity=12
watchid=120
watchname=gshock
price=1000
quantity=25
watchid=140
watchname=sonata
price=100
quantity=90
watchid=150
watchname=casio
price=1500
quantity=50

OUTPUT DATA
---------------------

SEARCHING RECORDS FROM TABLE

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

(100, 'rolex', 10000000, 10)


(110, 'titan', 10000, 12)
(120, 'gshock', 1000, 25)
(140, 'sonata', 100, 90)
(150, 'casio', 1500, 50)

Enter a watchname=gshock
(120, 'gshock', 1000, 25)

--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 25-----------------------------------------------------------------------

#SBIOAIS XII
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="sbioa@2023")
mycursor=mydb.cursor()
mycursor.execute("Create database CS;")
mycursor.execute("Use CS;")
print("\t\tDELETING RECORDS FROM TABLE")
print("\t-----------------------------------------\n")
print("\t INPUT DATA\n")
print("\t----------------\n")
mycursor.execute("Create table product(pid int(20),pname varchar(50),cost int(50));")
for i in range(5):
a=int(input("pid="))
b=input("pname=")
c=int(input("cost="))
mycursor.execute("insert into product values({},'{}',{})".format(a,b,c))
mydb.commit()
print("\n\t OUTPUT DATA\n")
print("\t---------------------\n")
print("DISPLAYING RECORDS FROM TABLE")
print("---------------------------------------------")
mycursor.execute("select * from product;")
data=mycursor.fetchall()
for i in data:
print(i)
b=input("Enter a pid=")
mycursor.execute("delete from product where pid='{}'".format(b))
print("RECORDS AFTER DELETING")
print("----------------------------------")
mycursor.execute("select * from product;")
data1=mycursor.fetchall()
for i in data1:
print(i)
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------OUTPUT-----------------------------------------------------------------------

DELETING RECORDS FROM TABLE


-----------------------------------------
INPUT DATA
----------------
pid=100
pname=coca cola
cost=75
pid=110
pname=pepsi
cost=30
pid=120
pname=lays
cost=10
pid=140
pname=milky bar
cost=20
pid=150
pname=kurkure
cost=35

OUTPUT DATA
---------------------

DISPLAYING RECORDS FROM TABLE


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

(100, 'coca cola', 75)


(110, 'pepsi', 30)
(120, 'lays', 10)
(140, 'milky bar', 20)
(150, 'kurkure', 35)
Enter a pid=140

RECORDS AFTER DELETING


----------------------------------
(100, 'coca cola', 75)

(110, 'pepsi', 30)

(120, 'lays', 10)

(150, 'kurkure', 35)

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

You might also like