Class 12 Prac 1-1
Class 12 Prac 1-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-----------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 2-------------------------------------------------------------------------
#SBIOAIS XII
-----------------------------------------------OUTPUT---------------------------------------------------------------------------
OUTPUT VALUES
-----------------------
Number with maximum occurrences: 23
The number 23 has 3 occurrences
----------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------CODE 3-------------------------------------------------------------------------
#SBIOAIS XII
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)
---------------------------------------------------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-----------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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----------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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-----------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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-----------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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-----------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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-----------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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-----------------------------------------------------------------------
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
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
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
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
1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice:3
The top of the stack is: [3, 'Murali']
1.PUSH
2.POP
3.РЕЕК
4.Disp
Enter your choice:2
The deleted doctor detail is: [3, 'Murali']
1.PUSH
2.POP
3.PEEK
4.Disp
Enter your choice:4
[1, Arun']
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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-----------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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
QUERY-4
QUERY-5
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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
Query-2
mysql> SELECT NAME FROM STU WHERE AGE BETWEEN 18 AND 20;
Query-3
Query-4
TABLE: STU
Query-1
Query-2
Query-3
Query-5
Query-1
Query-2
Query-4
mysql> SELECT COMPANY, COUNT(*) FROM COST GROUP BY COMPANY HAVING COUNT(*)>2;
Query-5
#SBIOAIS XII
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="sbioa@2023")
mycursor=mydb.cursor()
mycursor.execute("Use CS;")
print("\t----------------------------------------\n")
print("\t----------------\n")
for i in range(5):
M=int(input("rollno="))
H=input("name=")
S=int(input("total marks="))
mydb.commit()
print("\t---------------------\n")
print("------------------------------------------")
data=mycursor.fetchall()
for i in data:
print(i)
----------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------OUTPUT-----------------------------------------------------------------------
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-----------------------------------------------------------------------
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
---------------------
--------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------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-----------------------------------------------------------------------
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
---------------------
--------------------------------------------
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-----------------------------------------------------------------------
OUTPUT DATA
---------------------
------------------------------------------------------------------------------------------------------------------------------------