Professional Documents
Culture Documents
Labprograms
Labprograms
Labprograms
OUPUT LAB1
menu
1.positional and default arguments
2.keyword arguments
3.variable length arguments
4.variable length keyword arguments
5.multiple return values
6.exit
enter option: 1
enter principle: 5
enter time: 2
interest is 0.9
menu
1.positional and default arguments
2.keyword arguments
3.variable length arguments
4.variable length keyword arguments
5.multiple return values
6.exit
enter option: 2
enter principle: 3
enter rate: 4
interest is 24
menu
1.positional and default arguments
2.keyword arguments
3.variable length arguments
4.variable length keyword arguments
5.multiple return values
6.exit
3
LAB 1
CODE-
print("interest is ",inter)
print(stu )
for i,j in kwarg.items():
print(i,j)
def sum_avg(*avg):
k=0
for i in avg:
k += i
average = k/len(avg)
print("sum is ",k)
print("average is ",average)
while True:
print("""menu
1.positional and default arguments
2.keyword arguments
3.variable length arguments
4.variable length keyword arguments
5.multiple return values
6.exit""")
4
enter option: 3
the sum is 6
the sum is 28
menu
1.positional and default arguments
2.keyword arguments
3.variable length arguments
4.variable length keyword arguments
5.multiple return values
6.exit
enter option: 4
student information:
Roll_no 1
Name Arjun
Grade 12
Gender Female
Score [98, 97, 92]
menu
1.positional and default arguments
2.keyword arguments
3.variable length arguments
4.variable length keyword arguments
5.multiple return values
6.exit
enter option: 5
sum is 21
average is 3.5
menu
1.positional and default arguments
2.keyword arguments
3.variable length arguments
4.variable length keyword arguments
5
print()
option = int(input("enter option: "))
print()
if option == 1:
prin = int(input("enter principle: "))
time = int(input("enter time: "))
print()
interest(prin, time = time)
print()
elif option == 2:
prin = int(input("enter principle: "))
rate = int(input("enter rate: "))
print()
interest(prin = prin , rate = rate)
print()
elif option == 3:
total_sum("the sum is ",1,2,3)
total_sum("the sum is",5,7,12,3,1)
print()
elif option == 4:
stu_info("student information:", Roll_no = 1, Name = 'Arjun', Grade = 12,
Gender = 'Female', Score = [98,97,92])
print()
elif option == 5:
sum_avg(1,2,3,4,5,6)
print()
elif option == 6:
break
6.exit
enter option: 6
7
OUPUT LAB2
8
menu
1.list generation
2.Guess number
3.Shuffle and predict
4.exit
enter option: 1
menu
1.list generation
2.Guess number
3.Shuffle and predict
4.exit
enter option: 2
menu
1.list generation
2.Guess number
3.Shuffle and predict
4.exit
9
LAB 2
AIM- Menu driven program to create a guessing game using random module
CODE-
import random as r
import time as t
def create_list():
global lis
lis = []
n=0
for i in range(length):
n = r.randint(l,u)
lis.append(n)
print("list: ",lis)
print()
def guess():
chance = r.choice(lis)
for i in range(3):
choice = int(input("enter your guess: "))
enter option: 2
10
menu
1.list generation
2.Guess number
3.Shuffle and predict
4.exit
enter option: 3
if choice == chance:
x=1
print("YOU GUESSED CORRECT")
print()
break
if x == 0:
print("correct number was ",chance)
print()
else:
pass
def clear():
for i in range(1000):
print()
def shuffle_predict():
global lis
clear()
r.shuffle(lis)
print("shuffled list is: ",lis)
t.sleep(2)
clear()
predict = [int(i) for i in input("enter the correct list values: ").split(" ")]
if predict == lis:
print("YOUR PREDICTED LIST IS CORRECT")
shuffled list is: [170, 127, 197]
12
print()
13
else:
print("your predicted list is wrong correct list is ",lis)
print()
while True:
print("""menu
1.list generation
2.Guess number
3.Shuffle and predict
4.exit""")
print()
option = int(input("enter option: "))
print()
if option == 1:
create_list()
elif option == 2:
guess()
elif option == 3:
shuffle_predict()
elif option == 4:
break
menu
1.list generation
2.Guess number
3.Shuffle and predict
4.exit
enter option: 4
15
OUTPUT - LAB3
16
menu
1.Create file
2.Count 'my'
3.Count 3 letter words
4.Count letters 'e' and 'u'
5.exit
enter option: 1
my name is arjun
hello how are you
menu
1.Create file
2.Count 'my'
3.Count 3 letter words
4.Count letters 'e' and 'u'
5.exit
enter option: 2
count of my is 1
menu
1.Create file
2.Count 'my'
3.Count 3 letter words
4.Count letters 'e' and 'u'
5.exit
enter option: 3
LAB 3
17
AIM – Menu driven program to count specific words and letters in a text file
CODE –
def new_file():
with open('lab3.txt','w') as file:
while True:
line = input("enter line: ")
file.write(line+"\n")
continu = input("do you want to continue y/n: ")
if continu == 'n' or continu == 'no':
break
elif continu == 'y' or continu == 'yes':
pass
def my_count():
with open('lab3.txt','r') as file:
read = file.readlines()
count = 0
for i in read:
for j in i.strip("\n").split():
if j.lower() == 'my':
count += 1
print("count of my is ",count)
print()
def three_count():
with open('lab3.txt','r') as file:
menu
1.Create file
18
2.Count 'my'
3.Count 3 letter words
4.Count letters 'e' and 'u'
5.exit
enter option: 4
count of e is 3 count of u is 2
menu
1.Create file
2.Count 'my'
3.Count 3 letter words
4.Count letters 'e' and 'u'
5.exit
enter option: 5
read = file.readlines()
19
count = 0
for i in read:
for j in i.strip("\n").split():
if len(j) == 3:
count += 1
def e_u_count():
with open('lab3.txt','r') as file:
counte = 0
countu = 0
while True:
x = file.readline()
if x=="":
break
else:
for i in x:
if i == 'e' or i == 'E':
counte +=1
while True:
print("""menu
1.Create file
2.Count 'my'
3.Count 3 letter words
4.Count letters 'e' and 'u'
5.exit""")
20
print()
21
if option == 1:
new_file()
if option == 2:
try:
my_count()
except:
print("file not created please create file")
print()
new_file()
elif option == 3:
try:
three_count()
except:
print("file not created please create file")
print()
new_file()
elif option == 4:
try:
e_u_count()
except:
print("file not created please create file")
print()
new_file()
elif option == 5:
break
OUTPUT LAB4
MENU
1.create file
22
enter option: 1
my name is arjun
i like to play
my name is tejas
MENU
1.create file
2.find the longest line
3.count and display line starting with specific letter
4.reverse all words starting with 'i'
5.exit
enter option: 2
my name is arjun
my name is tejas
MENU
1.create file
2.find the longest line
3.count and display line starting with specific letter
4.reverse all words starting with 'i'
LAB 4
AIM- Menu driven program to count and display lines according to the given
23
CODE-
def new_file():
with open('lab4.txt','w') as file:
while True:
line = input("enter line: ")
file.write(line+"\n")
continu = input("do you want to continue y/n: ")
if continu == 'n' or continu == 'no':
break
elif continu == 'y' or continu == 'yes':
pass
def long_line():
with open('lab4.txt','r') as file:
read = file.readlines()
for i in range(len(read)):
for j in range(0,len(read)-i-1):
if len(read[j]) > len(read[j+1]):
read[j],read[j+1] = read[j+1],read[j]
for i in range(len(read)):
if len(read[-1]) == len(read[i]):
print(read[i])
else:
5.exit
enter option: 3
my name is arjun
my name is tejas
MENU
1.create file
2.find the longest line
3.count and display line starting with specific letter
4.reverse all words starting with 'i'
5.exit
enter option: 4
my name si arjun
i like to play
my name si tejas
MENU
1.create file
2.find the longest line
3.count and display line starting with specific letter
4.reverse all words starting with 'i'
5.exit
enter option: 5
pass
def count_letter():
with open('lab4.txt','r') as file:
r = file.readlines()
25
count = 0
for i in range(len(r)):
if r[i][0] == letter:
print(r[i])
count+=1
else:
pass
def i_reverse():
with open('lab4.txt','r') as file:
read = file.readlines()
for i in read:
reverse = ''
for j in i.strip("\n").split():
if j[0] == 'i':
j= j[::-1]
print(reverse)
print()
26
while True:
print("""MENU
1.create file
2.find the longest line
3.count and display line starting with specific letter
4.reverse all words starting with 'i'
27
5.exit""")
print()
option = int(input("enter option: "))
print()
if option == 1:
new_file()
elif option == 2:
try:
long_line()
except:
print("file not created please create file")
print()
new_file()
elif option == 3:
try:
count_letter()
except:
print("file not created please create file")
print()
new_file()
elif option == 4:
try:
i_reverse()
except:
print("file not created please create file")
print()
new_file()
elif option == 5:
28
break
29
OUTPUT LAB5
MENU
1.create file
2.word with highest frequency
3.frequency of all words
4.exit
30
enter option: 1
my name is sam
sam is from haryana
MENU
1.create file
2.word with highest frequency
3.frequency of all words
4.exit
enter option: 2
MENU
1.create file
2.word with highest frequency
3.frequency of all words
4.exit
enter option: 3
LAB 5
CODE-
31
def new_file():
with open('lab5.txt','w') as file:
while True:
line = input("enter line: ")
file.write(line+"\n")
continu = input("do you want to continue y/n: ")
if continu == 'n' or continu == 'no':
break
elif continu == 'y' or continu == 'yes':
pass
def frequency_word():
with open('lab5.txt','r') as file:
global freq
r = file.read()
freq = {}
for i in r.strip("\n").split():
count = 0
for k in r.strip("\n").split():
if i == k:
count += 1
freq[i] = count
l = list(freq.values())
mex = max(l)
my : 1
name : 1
is : 2
sam : 2
from : 1
haryana : 1
32
MENU
1.create file
2.word with highest frequency
3.frequency of all words
4.exit
enter option: 4
for x in freq.keys():
if freq[x]==mex:
print("The most frequent word : ")
print(x ,":", freq[x])
print()
def frequency_all():
33
for i in r.strip("\n").split():
count = 0
for k in r.strip("\n").split():
if i == k:
count += 1
freq[i] = count
print()
while True:
print("""MENU
1.create file
2.word with highest frequency
3.frequency of all words
4.exit""")
print()
option = int(input("enter option: "))
print()
if option == 1:
new_file()
34
if option == 2:
try:
frequency_word()
except:
print("file not created please create file")
print()
new_file()
35
elif option == 3:
try:
frequency_all()
except:
print("file not created please create file")
print()
new_file()
elif option == 4:
break
OUTPUT LAB6
MENU
1.create file
2.capatilize
3.exit
enter option: 1
36
MENU
1.create file
2.capatilize
3.exit
enter option: 2
MENU
1.create file
2.capatilize
3.exit
enter option: 3
LAB 6
AIM – Menu driven program to capitalize the first character of each sentence.
CODE –
def new_file():
37
def cap():
with open('lab6.txt','r') as file:
while True:
x = file.readline()
if x=="":
break
else:
j = x.split(".")
stri = ''
for i in range(len(j)):
c = j[i].capitalize()
try:
y = j[i+1]
stri += c + "."
except:
38
stri += c
print(stri)
while True:
print("""MENU
1.create file
2.capatilize
39
3.exit""")
print()
option = int(input("enter option: "))
print()
if option == 1:
new_file()
if option == 2:
#try:
cap()
"""except:
print("file not created please create file")
print()
new_file()"""
elif option == 3:
break
OUPUT LAB7
MENU
1.create file
2.seperate words by your choice
3.exit
enter option: 1
40
my name is arun
i play football
MENU
1.create file
2.seperate words by your choice
3.exit
enter option: 2
MENU
1.create file
2.seperate words by your choice
3.exit
enter option: 3
LAB 7
CODE –
def new_file():
41
def separate():
with open('Lab7.txt','r') as file1:
inp = input("Enter the character you want it to be separated by : ")
while True:
x = file1.readline()
if len(x)==0:
break
else:
for i in x.replace(" ",inp):
print(i,end='')
print()
while True:
print("""MENU
42
1.create file
2.seperate words by your choice
3.exit""")
print()
option = int(input("enter option: "))
print()
if option == 1:
43
new_file()
if option == 2:
""" try:"""
separate()
"""except:
print("file not created please create file")
print()
new_file()"""
elif option == 3:
break
OUTPUT LAB8
MENU
1.create file
2.segregate
3.exit
enter option: 1
/Users/arjun/Desktop/python/LAB PROGRAMS
my name is arjun
MENU
1.create file
2.segregate
3.exit
enter option: 2
Original file:
my name is arjun
second file:
LAB 8
AIM- Menu driven program to create and segregate words in a text file using
relative and absolute path
CODE –
def segregate():
45
new_file_path += "\\"+new_file_name+".txt"
print("File: ",new_file_path,"\n")
else:
file3.write(i +" ")
file1.seek(0)
print("Original file:\n" +file1.read()+"\n")
file2.seek(0)
print("second file:\n" +file2.read()+"\n")
file3.seek(0)
print("third file:\n" +file3.read()+"\n")
third file:
my name is arjun
MENU
1.create file
2.segregate
3.exit
enter option: 3
46
while True:
print("""MENU
1.create file
2.segregate
3.exit""")
print()
option = int(input("enter option: "))
print()
47
if option == 1:
new_file()
if option == 2:
#try:
segregate()
"""except:
print("file not created please create file")
print()
new_file()"""
elif option == 3:
break
OUTPUT LAB9
MENU
1.create file
2.Toggle case
3.exit
enter option: 2
my Name Is Arjun
HellO
MENU
1.create file
2.Toggle case
3.exit
enter option: 2
MY nAME iS aRJUN
hELLo
MENU
1.create file
2.Toggle case
3.exit
enter option: 3
LAB 9
CODE –
def new_file():
with open('lab9.txt','w') as file:
while True:
line = input("enter line: ")
49
file.write(line+"\n")
continu = input("do you want to continue y/n: ")
if continu == 'n' or continu == 'no':
break
elif continu == 'y' or continu == 'yes':
pass
def toggle_Case():
with open('Lab9.txt','r') as file1:
a = file1.read().swapcase()
print(a)
print()
while True:
print("""MENU
1.create file
2.Toggle case
3.exit""")
50
print()
option = int(input("enter option: "))
print()
if option == 1:
new_file()
if option == 2:
try:
toggle_Case()
except:
print("file not created please create file")
print()
51
new_file()
elif option == 3:
break
OUTPUT LAB10
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 1
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
| 2 | sam | 78 |
| 3 | bob | 56 |
+------------+--------------+---------------+
menu
1. insert
2. search
LAB 10
AIM- Menu driven program to insert, search, update and delete records in a
binary file without using temp file
CODE-
import pickle
import os
53
def insert():
with open("lab10.dat","wb") as file:
while True:
l = []
stu_no = int(input("enter student number: "))
stu_name = input("enter student name: ")
stu_marks = int(input("enter student marks: "))
print()
l.append(stu_no)
l.append(stu_name)
l.append(stu_marks)
if c == "y":
pass
elif c == "n":
break
3. delete
4. update
5. Exit
Enter option : 2
menu
1. insert
2. search
3. delete
4. update
5. Exit
54
Enter option : 2
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 3
try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
while True:
k = pickle.load(file)
data.append(k)
except:
pass
print()
55
def search():
with open("lab10.dat","rb") as file:
try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
while True:
k = pickle.load(file)
if k[0] == no:
data.append(k)
x=1
except:
if x == 0:
print("name not found")
elif x == 1:
table = tabulate(data,headers = "firstrow",tablefmt = "pretty")
print(table)
print()
4. update
5. Exit
Enter option : 3
menu
1. insert
56
2. search
3. delete
4. update
5. Exit
Enter option : 4
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
| 4 | sam | 89 |
+------------+--------------+---------------+
def delete():
with open("lab10.dat","rb") as file:
no = int(input("enter student number you want to delete: "))
l =[]
x=0
try:
while True:
k = pickle.load(file)
if k[0] != no:
l.append(k)
else:
x=1
57
except:
if x == 0:
print("name not found")
elif x == 1:
k = pickle.load(file)
data.append(k)
except:
pass
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 5
58
print()
def up():
with open("lab10.dat","rb") as file:
yes_no = input("Do you want to update existing file y/n: ")
if yes_no == 'n':
print()
pass
x=0
l =[]
59
try:
while True:
k = pickle.load(file)
if k[0] == ename:
x=1
print()
print()
print()
l.append(k)
61
except:
if x == 0:
print("name not found")
print()
elif x == 1:
with open("lab10.dat","wb") as file:
for i in range(len(l)):
pickle.dump(l[i],file)
while True:
k = pickle.load(file)
data.append(k)
except:
pass
print()
while True :
print("""menu
1. insert
2. search
63
3. delete
4. update
5. Exit""")
print()
option = int(input('Enter option : '))
print()
if option == 1:
insert()
if option == 2:
if os.path.exists("lab10.dat"):
search()
else:
print("file not created: ")
print()
insert()
if option == 3:
64
if os.path.exists("lab10.dat"):
delete()
else:
print("file not created: ")
print()
insert()
if option == 4:
if os.path.exists("lab10.dat"):
up()
else:
print("file not created: ")
print()
insert()
if option == 5:
break
65
OUTPUT LAB11
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 1
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
| 2 | sam | 34 |
| 3 | bob | 72 |
+------------+--------------+---------------+
menu
1. insert
LAB 11
AIM- Menu driven program to insert, search, update and delete records in a
binary file without using temp file(os module)
CODE-
import pickle
import os
def insert():
with open("lab11.dat","wb") as file:
while True:
l = []
stu_no = int(input("enter student number: "))
stu_name = input("enter student name: ")
stu_marks = int(input("enter student marks: "))
67
print()
l.append(stu_no)
l.append(stu_name)
l.append(stu_marks)
if c == "y":
pass
elif c == "n":
break
Enter option : 2
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 2
+------------+--------------+---------------+
| 2 | sam | 34 |
+------------+--------------+---------------+
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 3
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
while True:
k = pickle.load(file)
data.append(k)
except:
pass
print()
def search():
with open("lab11.dat","rb") as file:
try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
69
while True:
k = pickle.load(file)
if k[0] == no:
data.append(k)
x=1
except:
if x == 0:
print("name not found")
elif x == 1:
table = tabulate(data,headers = "firstrow",tablefmt = "pretty")
print(table)
print()
def delete():
| 1 | arjun | 99 |
| 2 | sam | 34 |
| 3 | bob | 72 |
+------------+--------------+---------------+
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 3
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
| 2 | sam | 34 |
+------------+--------------+---------------+
70
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 4
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
| 2 | sam | 39 |
+------------+--------------+---------------+
menu
l1 = []
with open('Lab11.dat','rb') as file:
with open("temp.dat","wb") as file1:
file.seek(0)
rolldel = int(input("Enter roll number to delete : "))
print()
while True:
try:
a=pickle.load(file)
if a[0]==rolldel:
l1.append(a)
else:
pickle.dump(a,file1)
except:
break
os.remove("lab11.dat")
71
os.rename("temp.dat","lab11.dat")
with open("lab11.dat","rb") as file:
try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
while True:
k = pickle.load(file)
data.append(k)
except:
pass
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 5
72
print()
def up():
with open('Lab11.dat','rb') as file:
with open('temp.dat','wb') as file1:
file.seek(0)
while True:
try:
a = pickle.load(file)
if int(a[2])<=35:
(a[2]) = str(int(a[2])+5)
pickle.dump(a,file1)
else:
pickle.dump(a,file1)
except:
break
73
os.remove("lab11.dat")
os.rename("temp.dat","lab11.dat")
with open("lab11.dat","rb") as file:
try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
while True:
k = pickle.load(file)
data.append(k)
74
except:
pass
print()
while True :
print("""menu
1. insert
2. search
3. delete
4. update
5. Exit""")
print()
option = int(input('Enter option : '))
print()
if option == 1:
insert()
elif option == 2:
if os.path.exists("lab11.dat"):
search()
75
else:
print("file not created: ")
print()
insert()
elif option == 3:
if os.path.exists("lab11.dat"):
delete()
else:
print("file not created: ")
print()
76
insert()
elif option == 4:
if os.path.exists("lab11.dat"):
up()
else:
print("file not created: ")
print()
insert()
elif option == 5:
break
77
OUTPUT LAB12
MENU
1. Insert
2. Search
3. Delete
4. Update
5. Exit
+--------------+---------------+-------------+
| Student Name | Student Class | Student Age |
+--------------+---------------+-------------+
| arjun | 12 | 15 |
| sam | 1 | 6 |
| bob | 8 | 14 |
+--------------+---------------+-------------+
MENU
1. Insert
2. Search
LAB 12
CODE-
import pickle
import os
from tabulate import tabulate
def create():
with open("lab12.dat","wb") as file:
d = {}
while True:
name = input("Enter Student Name : ")
grade = int(input("Enter Student Class : "))
age = int(input("Enter Student Age : "))
details = [name,grade,age]
d[name] = details
print()
a = input("do you want to continue y/n : ")
if a == 'n':
break
elif a == 'y':
pass
print()
79
pickle.dump(d,file)
print()
def display():
try:
with open("lab12.dat", "rb") as file:
file.seek(0)
main = []
temp = pickle.load(file)
for i in temp.keys():
main.append(temp[i])
3. Delete
4. Update
5. Exit
MENU
1. Insert
2. Search
3. Delete
4. Update
5. Exit
MENU
1. Insert
2. Search
3. Delete
4. Update
80
5. Exit
try:
file = open("lab12.dat","rb")
data = pickle.load(file)
nval = input("Enter name of student whose details you want to find : ")
l = []
for i in data.keys():
if nval == i:
l.append(data[i])
if l:
+-----------------+---------------+----------------+
MENU
1. Insert
2. Search
3. Delete
4. Update
5. Exit
+---------------+----------------+--------------+
| Student Name: | Student Class: | Student Age: |
+---------------+----------------+--------------+
| arjun | 12 | 17 |
| bob | 8 | 14 |
+---------------+----------------+--------------+
MENU
1. Insert
2. Search
3. Delete
4. Update
5. Exit
while True:
print()
print("MENU")
print("1. Insert")
print("2. Search")
print("3. Delete")
print("4. Update")
print("5. Exit")
print()
84
OUTPUT LAB13
LAB 13
AIM- Program to create and read a CSV file with book details using ‘\t’ as a
delimiter.
CODE-
import csv
from tabulate import tabulate
main.append(l)
if y_n == 'y':
pass
a.writerows(main)
OUTPUT LAB14
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 1
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
| 2 | sam | 34 |
| 3 | bob | 56 |
+------------+--------------+---------------+
88
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 2
LAB 14
AIM- Menu driven program to insert, search, update and delete records in a
CSV file.
CODE-
import csv
import os
from tabulate import tabulate
def insert():
with open("lab14.csv",'w',newline = "") as file:
main = []
a = csv.writer(file)
while True:
l = [i for i in input("enter student number, student name and marks:
").split(" ")]
print()
main.append(l)
if y_n == 'y':
pass
break
a.writerows(main)
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 2
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 3
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 3
print()
def search():
with open("lab14.csv",'r') as file:
a = csv.reader(file)
no = int(input("enter student number you want to search for: "))
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
x=0
for i in a:
if no == int(i[0]):
data.append(i)
x=1
else:
pass
if x == 1:
table = tabulate(data,headers = "firstrow",tablefmt = "pretty")
print(table)
print()
else:
91
def delete():
with open("lab14.csv","r") as file:
no = int(input("enter student number you want to delete: "))
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
a = csv.reader(file)
enter student number you want to delete: 3
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
| 2 | sam | 34 |
+------------+--------------+---------------+
menu
1. insert
2. search
3. delete
4. update
5. Exit
Enter option : 4
+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
92
| 6 | sam | 32 |
+------------+--------------+---------------+
menu
1. insert
2. search
3. delete
4. update
l =[]
x=0
for i in a:
if no != int(i[0]):
l.append(i)
else:
x=1
if x == 1:
with open("lab14.csv",'w',newline = "") as file:
a = csv.writer(file)
a.writerows(l)
print()
elif x == 0:
print("name not found")
print()
93
def up():
with open("lab14.csv","r") as file:
a = csv.reader(file)
yes_no = input("Do you want to update existing file y/n: ")
if yes_no == 'n':
print()
pass
5. Exit
Enter option : 5
94
x=0
l =[]
for i in a:
if int(i[0]) == ename:
x=1
print()
print()
95
print()
96
l.append(i)
if x == 0:
print("name not found")
print()
elif x == 1:
with open("lab14.csv",'w',newline = "") as file:
a = csv.writer(file)
a.writerows(l)
print()
while True :
97
print("""menu
1. insert
2. search
3. delete
4. update
5. Exit""")
print()
98
if option == 1:
insert()
elif option == 2:
if os.path.exists("lab14.csv"):
search()
else:
print("file not created: ")
print()
insert()
elif option == 3:
if os.path.exists("lab14.csv"):
delete()
else:
print("file not created: ")
print()
insert()
elif option == 4:
if os.path.exists("lab14.csv"):
up()
else:
print("file not created: ")
print()
99
insert()
elif option == 5:
break
OUTPUT LAB15
menu
1. insert
2. display specific column
3. Exit
Enter option : 1
+--------+-------+---------+
| NUMBER | NAME | SALARY |
+--------+-------+---------+
| 1 | arjun | 100000 |
| 2 | sam | 30000 |
| 3 | bob | 5600600 |
+--------+-------+---------+
menu
1. insert
2. display specific column
3. Exit
100
Enter option : 2
menu
LAB 15
AIM- Menu driven program to insert and display specific columns in a CSV file.
CODE-
import csv
import os
from tabulate import tabulate
def insert():
with open("lab15.csv",'w',newline = "") as file:
main = []
a = csv.writer(file)
while True:
l = [i for i in input("enter employee no, employee name and salary: ").split("
")]
print()
main.append(l)
if y_n == 'y':
pass
a.writerows(main)
101
Enter option : 2
menu
1. insert
2. display specific column
3. Exit
Enter option : 3
102
print(table)
print()
def display():
with open("lab15.csv",'r') as file:
a = csv.reader(file)
no = input("enter column you want to search: ")
l = []
x=0
for i in a:
if no == 'number' or no == 'NUMBER':
l.append([i[0]])
x=1
else:
pass
if x == 1:
data = []
103
for i in l:
data.append(i)
print()
elif x == 2:
data = []
for i in l:
data.append(i)
table = tabulate(l,headers =["NAME"],tablefmt = "pretty")
print(table)
print()
elif x == 3:
data = []
for i in l:
data.append(i)
table = tabulate(l,headers = ["SALARY"],tablefmt = "pretty")
print(table)
print()
else:
print("name not found")
print()
while True :
print("""menu
1. insert
2. display specific column
3. Exit""")
105
print()
option = int(input('Enter option : '))
print()
if option == 1:
insert()
106
elif option == 2:
if os.path.exists("lab15.csv"):
display()
else:
print("file not created: ")
print()
insert()
elif option == 3:
break
107
OUTPUT LAB21
menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit
Enter option : 1
menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit
Enter option : 2
LAB 21
AIM- Write a menu driven program to perform various DDL commands using
Python MySQL interface
CODE –
import mysql.connector as m
from tabulate import tabulate
def creation():
global name
global table_name
def show():
x = my.fetchall()
109
menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit
Enter option : 3
menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit
110
Enter option : 4
my.execute("desc {}".format(table_name))
x = my.fetchall()
def add():
global col
x = my.fetchall()
print("structure of the table: ")
table = tabulate(x, headers = ["Field","Type","Null","Key","Default","Extra"],
tablefmt = 'pretty')
print(table)
print()
def modify():
modify_col = input("enter column name you want to modify: ")
modify_col_type = input("enter new column type: ")
x = my.fetchall()
111
menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit
Enter option : 5
menu
1. create database
112
def drop():
drop_col = input("enter column you want to drop: ")
x = my.fetchall()
print("structure of the table: ")
table = tabulate(x, headers = ["Field","Type","Null","Key","Default","Extra"],
tablefmt = 'pretty')
print(table)
print()
while True :
print("""menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit""")
print()
option = int(input('Enter option : '))
print()
if option == 1:
creation()
elif option == 2:
show()
elif option == 3:
add()
113
elif option == 4:
modify()
6. Exit
Enter option : 6
114
elif option == 5:
drop()
elif option == 6:
break