Professional Documents
Culture Documents
CODE:
def leexchange():
for i in range(0,num-1,2):
first=lst[i]
second=lst[i+1]
lst[i]=second
lst[i+1]=first
print("new list:",lst)
lst=[]
for n in range(num):
lst.append(numbers)
print("original list:",lst)
leexchange()
OUTPUT:
QUESTION 2:
QUESTION 3:
CODE:
import pickle
s={}
file2=open("student.dat",'wb')
N=input("Enter name")
M=float(input("Enter marks"))
s['Rollno']=R
s['Name']=N
s['Marks']=M
pickle.dump(s,file2)
file2.close()
OUTPUT:
QUESTION 4:
CODE:
def reverse():
for i in range(len(L)):
reversedList.append(L[len(L)-i-1])
print("reversedList",reversedList)
reversedList=[]
L=[]
for i in range(0,n):
L.append(ele)
print("original list",L)
reverse()
OUTPUT:
QUESTION 5:
CODE:
def cnt():
f=open("test.txt","r")
cont=f.read()
print(cnt)
v=0
lc=0
uc=0
cons=0
for i in cont:
if(i.islower()):
lc+=1
elif(i.isupper()):
uc+=1
if(i in ['a','e','i','o','u',]):
v+=1
elif(i.isalpha()):
cons+=1
f.close()
print("vowels=",v)
print("consonants=",cons)
print("lowercase=",lc)
print("uppercase=",uc)
cnt()
OUTPUT:
QUESTION 6:
CODE:
student = [{'id': 1, 'success': True, 'name': 'Shyana'},
OUTPUT:
QUESTION 7:
CODE:
f=open("test.txt","r")
f1=open("writetest2.txt","w")
l=f.readlines()
for i in l:
if 'a' in i:
f1.write(i)
f.close()
f1.close()
OUTPUT:
QUESTION 8:
CODE:
import csv
def write():
f=open("departments.csv","w",newline='')
wrt=csv.writer(f)
wrt.writerow(["Department_ID","Department_Name","Manager_ID"])
while True:
d_id=int(input("Enter department id:"))
data=[d_id,d_name,m_id]
wrt.writerow(data)
if ch in 'Nn':
break
f.close()
def read():
f=open("departments.csv","r",newline='\r\n')
s_reader=csv.reader(f)
for i in s_reader:
print(i)
write()
read()
OUTPUT:
QUESTION 9:
CODE:
import pickle
def Writerecord(sroll,sname):
srecord={"SROLL":sroll,"SNAME":sname}
pickle.dump(srecord,Myfile)
def Readrecord():
print()
while True:
try:
rec=pickle.load(Myfile)
except EOFError:
break
def Input():
def SearchRecord(roll):
while True:
try:
rec=pickle.load(Myfile)
if rec['SROLL']==roll:
print("Roll NO:",rec['SROLL'])
print("Name:",rec['SNAME'])
except EOFError:
print("Try Again..............")
break
def main():
while True:
print('1.Insert Records')
print('2.Dispaly Records')
print('3.Search Records (By Roll No)')
if ch==1:
Input()
elif ch==2:
Readrecord()
elif ch==3:
SearchRecord(r)
else:
break
main()
OUTPUT:
QUESTION 10:
CODE:
import random
min=1
max=6
roll_again="y"
val=random.randint(min,max)
print("You get...:",val)
OUTPUT:
QUESTION 11:
CODE:
emp_dict = { }
while True :
choice = input("Do you want to Enter another record Press 'y' if yes : ")
if choice != "y" :
break
print(emp_dict)
OUTPUT:
QUESTION 12:
CODE:
def PUSH(Arr):
s=[]
for i in range(0,len(Arr)):
if Arr[i]%5==0:
s.append(Arr[i])
if len(s)==0:
print("Stack is empty")
else:
print(s)
Arr=[25,10,14,30,15,9]
PUSH(Arr)
OUTPUT:
QUESTION 13:
CODE:
def RShift(L):
n=len(L)
last=L[n-1]
for i in range(n-2,-1,-1):
L[i+1]=L[i]
L[0]=last
L)
L=[]
L.append(ele)
RShift(L)
OUTPUT:
QUESTION 14:
QUESTION 15:
CODE:
import mysql.connector
mydb=mysql.connector.connect(host="localhost", user="root",
passwd="mysql",
database="real_estate")
mycursor=mydb.cursor()
mycursor.close()
mydb.close()
OUTPUT:
QUESTION 16:
CODE:
import mysql.connector
while True:
print("--------CLIENT PROFILE---------")
print("1-Create table")
print("2-Insert details")
print("3-Update details")
print("4-Deletion of records")
print("5-Display details")
print("6-Main menu")
while True:
if choice==1:
mydb=mysql.connector.connect(host="localhost",
user="root",
passwd="mysql",
database="real_estate")
mycursor=mydb.cursor()
mydb.close()
break
elif choice==2:
mydb=mysql.connector.connect(host="localhost",
user="root",
passwd="mysql",
database="real_estate")
mycursor=mydb.cursor()
adrs=str(input("Enter address"))
sql="INSERT INTO
client_detail(Id,Name,Address)VALUES({},'{}','{}')".format(id,name,adrs)
mycursor.execute(sql)
mydb.commit()
mydb.close()
break
elif choice==3:
mydb=mysql.connector.connect(host="localhost",
user="root",
passwd="mysql",
database="real_estate")
mycursor=mydb.cursor()
mycursor.execute(query)
mydb.commit()
break
elif choice==4:
mydb=mysql.connector.connect(host="localhost",
user="root",
passwd="mysql",
database="real_estate")
mycursor=mydb.cursor()
mycursor.execute(query)
mydb.commit()
print("Deletion sccessfully")
break
elif choice==5:
mydb=mysql.connector.connect(host="localhost",
user="root",
passwd="mysql",
database="real_estate")
mycursor=mydb.cursor()
query="SELECT*from client_detail"
mycursor.execute(query)
myrecords=mycursor.fetchall()
print("Id,Name,Address are:")
for x in myrecords:
print (x)
print(mycursor.rowcount,"record selected")
mycursor.close()
mydb.close()
break
else:
break
OUTPUT:
QUESTION 18:
CODE:
def square_cube(L,n):
h=int(n/2)
for i in range(0,h):
L[i]=L[i]*L[i]
L[j]=L[j]**3
L=[]
for i in range(0,n):
L.append(ele)
square_cube(L,n)
OUTPUT:
QUESTION 19:
CODE:
n = 10
print (list)
i=0
length = len(list)
while(i<length):
if(list[i]==n):
list.remove(list[i])
length=length -1
continue
i=i+1
print(list)
OUTPUT:
QUESTION 20:
CODE :
import pickle as p
def Createfile():
Author=input("Enter Author:")
Price=float(input("Enter Price"))
l=[BookNo,Book_Name,Author,Price]
f=open("Book.dat","ab")
p.dump(l,f)
f.close()
def Countrec(Author):
count=0
f=open("Book.dat","rb")
while True:
try:
l=p.load(f)
if l[2]==Author:
count=count+1
except EOFError:
print("EOF reached")
break
return count
OUTPUT: