Shreyas Practical Doc Final
Shreyas Practical Doc Final
AIM:
CODE:
lst=[]
num=int(input("Enter any number :"))
lst+=str(num) #adding the string to empty list
Lst=sorted(lst)
print("Number in ascending order :",Lst)
OUTPUT:
PROGRAM-2
AIM:
#WAP code to display the following pattern
1
31
531
7531
97531
CODE:
n=8
print("1")
for i in range(1,n):
if i%2==0: #modulas sign returns remainder
continue
else:
for j in range(i+2,0,-2): #nested loop
print(j,end=" ")
print()
OUTPUT:
PROGRAM -3
AIM:
CODE:
OUTPUT:
PROGRAM -4
AIM:
# Display all the names whose first letter begins with ‘A’ (accepting a
list).
CODE:
n=4
L=[]
for i in range(n):
name=input("Enter name :")
L+=[name]
print("Names that starts with A/a is/are ",end=":")
for i in L:
for j in i.split(): #splitting the name inorder to traverse letters
if j[0]=="a"or j[0]=="A":
print(i,end=",")
OUTPUT:
PROGRAM -5
AIM:
# input names in a list and display all names which start with A"""
CODE:
d={}
n=int(input("No. of employees :"))
for i in range(n):
name=input("Enter employee name :")
sal=int(input("Enter salary :"))
d[name]=sal #adding key and value to empty dictionary
t=tuple(d.values()) #it returns all the values in a tuple
for i in t :
mx=max(t)
for j in d:
if d[j]==mx:
print(j,"has salary Rs",mx,"which is the highest")
OUTPUT:
PROGRAM -6
AIM:
# WAP to input a four digit number. Write fourdigit() that calculates the
sum of squares of the first two digit number and the last two digit
numbers.
CODE:
def Four_Digit(n):
return int(n[:2])**2+int(n[2:])**2
n=input("Enter a four digit number :")
Num=Four_Digit(n)
print(Num)
OUTPUT:
PROGRAM -7
AIM:
CODE:
def Num(n):
tot=0
for i in str(n):
tot+=pow(int(i),2) # pow returns the value of i^2
return tot
num=int(input("Enter any value:"))
temp=num
while temp!=1 and temp!=4:
temp=Num(temp)
if temp==1:
print("Its a happy number ")
else:
print("Its not a happy number")
OUTPUT:
PROGRAM -8
AIM:
WAF tabulation() to open a file ‘result.txt’ in ‘w+’ mode to store index
number , name and marks of a few students.
CODE:
def Tabulation():
f=open("Result.txt","w+")
n=int(input("Enter no. of students':"))
for i in range(n):
rn=int(input("Enter student's roll no.':"))
nm=input("Enter student's name :")
mk=int(input("Enter student's mark :"))
rec=str(rn)+","+nm+","+str(mk)+"\n"
f.write(rec)
f.flush() #directly saves the data into the text file
print("Contents of file are : ")
f.seek(0) #brings file pointer to initial position
Rec=f.read() #reads the entire file
print(Rec)
print()
print(Tabulation())
OUTPUT:
PROGRAM -9
AIM:
CODE:
def Contacts():
f=open("Contact.txt","r")
rec=f.readlines() #returs a list where each line is element
print("Names starting with P :")
for i in rec:
for j in i.split():
if j[0]=="P" or j[0]=="p":
print(i)
Contacts()
OUTPUT:
Program -10
AIM:
#The file ‘city.txt’ stores the names of a few cities with the pincodes.
WAF DISPLAY() to read data and display only those cities with pincode
whose first letter is not a vowel.
CODE:
def Cities():
f=open("City.txt","w+")
n=int(input("Number of details :"))
for i in range(n):
cn=input("Enter city name :")
pc=int(input("Enter the pincode :"))
rec=cn+"-"+str(pc)+"\n"
f.write(rec)
f.flush()
vow=["a","e","i","o","u","A","E","I","O","U"] #list of vowles
f.seek(0) #initialising file pointer
Data=f.readlines()
for i in Data:
for j in i.split():
if j[0] not in vow:
print(i,sep=";")
else:
continue
Cities()
OUTPUT:
Program -11
AIM:
CODE:
def Lastline():
f=open("Paragraph.txt","w+")
l=[]
ln=int(input("Number of lines ? :"))
for i in range(ln):
snt=input("Enter any string :")
l.append(snt+"\n") #\n is a new line character
f.writelines(l)
f.seek(0)
rec=f.readlines()
lg=len(rec)
for a in range(5,0,-1):
print(rec[a*-1])
Lastline()
OUTPUT:
Program -12
AIM:
CODE:
import pickle
fwb=open("train.dat","wb")
n=int(input("Enter no. of details : "))
for i in range(n):
tno=int(input("Enter train no : "))
to=input("Enter train destination : ")
frm=input("Enter train starting station : ")
rec={"tno":tno,"to":to,"from":frm} #creating a dict and and storing it in file
pickle.dump(rec,fwb)
fwb.close()
def Search():
f=open("train.dat","rb")
for i in range(n):
rd=pickle.load(f)
if rd['to']=='Delhi' or rd['to']=='delhi':
print(rd)
else:
continue
Search()
OUTPUT:
PROGRAM -13
AIM:
CODE:
import pickle
def Search_data():
rec=open("Teacher.dat","rb")
trec=open("Temp.dat","wb")
name=input("Enter teacher name to be updated : ")
a=pickle.load(f) #used to read a binary file
for i in a :
if i[1]==name:
n=input("Enter updated name : ")
i[1]=n1
else:
continue
pickle.dump(a,trec)
trec.seek(0) #moving file pointer to initial position
b=pickle.load(trec)
print(b)
trec,close()
rec.close()
Search_data()
OUTPUT:
PROGRAM -14
AIM:
CODE:
import pickle
fwb=open("Item.dat","wb")
n=int(input("Enter no. of entries : "))
for i in range(n):
ino=int(input("Enter item no : "))
iname=input("Enter item name : ")
qty=int(input("Enter item quantity : "))
price=int(input("Enter price : "))
rec={"itemno":ino,"iname":iname,"Qty":qty,"Price":price}
pickle.dump(rec,fwb)
fwb.close()
f=open("Item.dat","rb")
for i in range(n):
a=pickle.load(f)
print("Itemno : ",a[ino])
print("Item name : ",a[iname])
print("Quantity : ",a[qty])
print("Amount : ",a[price])
f.close()
OUTPUT :
PROGRAM -15
AIM:
CODE :
import pickle
fwb=open("book.dat","wb")
n=int(input("enter no. of entries : "))
for i in range(n):
bno=int(input("enter book no. : "))
bname=input("enter book name : ")
author=input("enter author : ")
price=int(input("enter price of book : "))
rec=[str(bno),bname,author,str(price)]
pickle.dump(rec,fwb)
print("Data saved successfully")
fwb.close()
def Count(author):
f=open("book.dat","rb")
count=0
for i in range(n):
a=pickle.load(f)
b=a.count(author)
count+=b
print("No. of books by ", author, ":", count)
author=input("enter author name : ")
Count(author)
OUTPUT :
PROGRAM -16
AIM:
hotel=[]
def push_cust():
n=int(input("Enter no of customers :"))
for i in range(n):
name=str(input("Enter name of customer :"))
room=str(input("Enter room type :"))
if room=="Delux" :
hotel.append(name)
else:
continue
return n
print(hotel)
def pop_cust(p):
for i in range(p):
j=hotel.pop()
print(j)
if len(hotel)==0:
print("underflow")
break #comes out of loop body
p=push_cust()
pop_cust(p)
OUTPUT :
PROGRAM -17
AIM:
CODE :
import math
Numbers=[]
L=[1,2,3,4,5,6,7,8,9,10]
def Push():
for i in L:
a=math.sqrt(i) #finds square root of number i
if a == int(a):
Numbers.append(int(a)**2)
print("Stack : ", Numbers)
Push()
def Pop():
while len(Numbers)!=0:
d=Numbers.pop()
print(d)
print("Underflow")
Pop()
OUTPUT:
PROGRAM –MYSQL CONNECTIVITY
AIM :
#Mysql Connectivity ( Creating Database and Table )
CODE :
OUTPUT :
PROGRAM –INSERTING
AIM:
CODE:
import mysql.connector as mys
mycon=mys.connect(host='localhost',user='root',
passwd='mysql',database='12a_2023')
mycursor=mycon.cursor()
print("wlecome to employee data entry")
ans="y"
while ans=="y":
eno=int(input("enter employee no.:"))
nm=input("enter name:")
dp=input("enter dept:")
s=int(int(input("enter salary:"))
query="insert into emp values ({0},'{1}','{2}',{3})".format
(eno,nm,dp,s)
mycursor.execute(query)
mycon.commit()
print("##record saved...##")
ans=input("add more??")
OUTPUT :
PROGRAM -UPDATING
AIM:
CODE:
OUTPUT :
PROGRAM-DELETING
AIM:
CODE:
import mysql.connector as mys
mycursor=mych.cursor()
mycursor.execute(query)
data=mycursor.fetchone()
if data!=None:
print(data)
if ans=="y":
mycursor.execute(query)
mych.commit()
print("Record DELETED")
else:
mysql>select EMPLOYEEid,NAME,employee.JOBid,JOBtitle
from employee,job
where employee.JOBid=job.JOBid;
+------------+-------------------+-------+--------------------------+
| EMPLOYEEid | NAME | JOBid | JOBtitle |
+------------+-------------------+-------+--------------------------+
| E1 | Sumit Sinha | 102 | Vice President |
| E2 | Vijay Singh Tomar | 101 | President |
| E3 | Ajay Rajpal | 103 | Administration Assistant |
| E4 | Mohit Ramnani | 102 | Vice President |
| E5 | Shaija Singh | 103 | Administration Assistant |
+------------+-------------------+-------+--------------------------+
+--------------+---------+--------------------------+
| NAME | SALES | JOBtitle |
+--------------+---------+--------------------------+
| Ajay Rajpal | 1400000 | Administration Assistant |
| Shaija Singh | 1450000 | Administration Assistant |
+--------------+---------+--------------------------+
+------------+
| avg(price) |
+------------+
| 720.0000 |
+------------+
2. To display details of all the uniform from the
‘UNIFORM TABLE’ in the descending order of stock date.
+-------+-------+--------+------------+
| Ucode | Uname | Ucolor | StockDate |
+-------+-------+--------+------------+
| 1 | Shirt | White | 2021-03-31 |
| 3 | Skirt | Grey | 2021-02-18 |
| 2 | Pant | Black | 2020-01-01 |
| 5 | Socks | Blue | 2019-03-19 |
| 4 | Tie | Blue | 2019-01-01 |
| 6 | Belt | Black | 2017-12-09 |
+-------+-------+--------+------------+
+---------+------------+------------+
| company | max(price) | min(price) |
+---------+------------+------------+
| Galin | 830 | 830 |
| Mattex | 620 | 150 |
| Raymond | 940 | 500 |
| Yasin | 810 | 770 |
+---------+------------+------------+
mysql> select
cost.Ucode,Uname,Ucolor,cost.size,Company from
cost,uniform where cost.Ucode=uniform.Ucode;
+-------+-------+--------+------+---------+
| Ucode | Uname | Ucolor | size | Company |
+-------+-------+--------+------+---------+
| 1 | Shirt | White | M | Raymond |
| 1 | Shirt | White | L | Mattex |
| 2 | Pant | Black | XL | Mattex |
| 2 | Pant | Black | M | Yasin |
| 2 | Pant | Black | L | Raymond |
| 3 | Skirt | Grey | M | Yasin |
| 3 | Skirt | Grey | L | Galin |
| 4 | Tie | Blue | S | Mattex |
+-------+-------+--------+------+---------+
III)
+--------+----------+------------+
| Name | Dept | DOA |
+--------+----------+------------+
| Ankit | History | 1998-03-24 |
| Anu | Hindi | 1996-12-12 |
| Charan | Hindi | 1997-09-05 |
| Deepa | History | 1997-06-27 |
| Dinesh | Computer | 1997-02-25 |
| Usha | null | 1997-07-31 |
+--------+----------+------------+
2.WRITE A QUERY TO LIST THE NAMES OF STUDENTS WHOSE
AGE BETWEEN 18 AND 20?
mysql> select Name from student where Dept="Hindi" and
Gender="F";
+------+
| Name |
+------+
| Anu |
+------+
3. WRITE A QUERY TO DISPLAY NAME OF STUDENTS WHOSE
NAME IS STARTING WITH ‘A’?
+-------+
| Name |
+-------+
| Arun |
| Ankit |
| Anu |
+-------+
+-------+
| Name |
+-------+
| Ankit |
| Anu |
+-------+
IV)
+--------+--------+--------+------+----------+------------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees |
+--------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 21 | Computer | 1997-01-10 | 170 |
| 2 | Ankit | M | 21 | History | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | Hindi | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | null | 1999-07-01 | 400 |
| 5 | Charan | M | 18 | Hindi | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | History | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | Computer | 1997-02-25 | 210 |
+--------+--------+--------+------+----------+------------+------+
3. Write a Query to add a new column Area of type
varchar in table STU.
+--------+--------+--------+------+----------+------------+------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees | Area |
+--------+--------+--------+------+----------+------------+------+------+
| 1 | Arun | M | 21 | Computer | 1997-01-10 | 170 | NULL |
| 2 | Ankit | M | 21 | History | 1998-03-24 | 200 | NULL |
| 3 | Anu | F | 20 | Hindi | 1996-12-12 | 300 | NULL |
| 4 | Bala | M | 19 | null | 1999-07-01 | 400 | NULL |
| 5 | Charan | M | 18 | Hindi | 1997-09-05 | 250 | NULL |
| 6 | Deepa | F | 19 | History | 1997-06-27 | 300 | NULL |
| 7 | Dinesh | M | 22 | Computer | 1997-02-25 | 210 | NULL |
+--------+--------+--------+------+----------+------------+------+------+
+--------+
| Name |
+--------+
| Arun |
| Ankit |
| Anu |
| Bala |
| Charan |
| Deepa |
| Dinesh |
+--------+
+--------+--------+--------+------+----------+------------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees |
+--------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 21 | Computer | 1997-01-10 | 170 |
| 2 | Ankit | M | 21 | History | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | Hindi | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | null | 1999-07-01 | 400 |
| 5 | Charan | M | 18 | Hindi | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | History | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | Computer | 1997-02-25 | 210 |
+--------+--------+--------+------+----------+------------+------+