Labprograms

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 114

1

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

AIM - Menu driven program to handle various function arguments

CODE-

def interest(prin,time = 2, rate = 0.09):


inter = round(prin * rate * time,2)

print("interest is ",inter)

def total_sum(a, *args):


k =sum(args)
print(a,k)

def stu_info(stu, **kwarg):

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

5.multiple return values


6

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

enter length of list: 3


enter lower limit: 100
enter upper limit: 200

list: [127, 197, 170]

menu
1.list generation
2.Guess number
3.Shuffle and predict
4.exit

enter option: 2

computer has made its choice


you have 3 chances to guess the correct number

enter your guess: 197


WRONG GUESS

enter your guess: 127


YOU GUESSED CORRECT

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 = []

length = int(input("enter length of list: "))


l = int(input("enter lower limit: "))
u = int(input("enter upper limit: "))
print()

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)

print("computer has made its choice")


print("you have 3 chances to guess the correct number")
print()
x=0

for i in range(3):
choice = int(input("enter your guess: "))
enter option: 2
10

computer has made its choice


you have 3 chances to guess the correct number

enter your guess: 1


WRONG GUESS

enter your guess: 1


WRONG GUESS

enter your guess: 1


WRONG GUESS

correct number was 127

menu
1.list generation
2.Guess number
3.Shuffle and predict
4.exit

enter option: 3

list is: [127, 197, 170]


11

if choice == chance:
x=1
print("YOU GUESSED CORRECT")
print()
break

elif choice != chance:


print("WRONG GUESS")
print()

if x == 0:
print("correct number was ",chance)
print()

else:
pass

def clear():
for i in range(1000):
print()

def shuffle_predict():
global lis

print("list is: ",lis)


t.sleep(2)

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

enter the correct list values: 170 127 197


YOUR PREDICTED LIST IS CORRECT
14

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

enter line: my name is arjun


do you want to continue y/n: y
enter line: hello how are you
do you want to continue y/n: n

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

count of three letter words is 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

with open('lab3.txt','r') as file:


r = file.read()
print()
print(r)

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

print("count of three letter words is ",count)


print()

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

elif i == 'u' or i == 'U':


countu +=1
else:
pass
print("count of e is ",counte," count of u is ",countu)
print()

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

option = int(input("enter option: "))


print()

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

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: 1

enter line: my name is arjun


do you want to continue y/n: y
enter line: i like to play
do you want to continue y/n: y
enter line: my name is tejas
do you want to continue y/n: n

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

condition in a text file

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

with open('lab4.txt','r') as file:


r = file.read()
print()
print(r)

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

enter letter that line starts with: m


24

my name is arjun

my name is tejas

number of lines starting with m is 2

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

letter = input("enter letter that line starts with: ")

count = 0

for i in range(len(r)):
if r[i][0] == letter:
print(r[i])
count+=1

else:
pass

print("number of lines starting with ",letter," is ",count)


print()

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]

reverse +=j + ' '

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

enter line: my name is sam


do you want to continue y/n: y
enter line: sam is from haryana
do you want to continue y/n: n

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

The most frequent word :


is : 2

The most frequent word :


sam : 2

MENU
1.create file
2.word with highest frequency
3.frequency of all words
4.exit

enter option: 3

LAB 5

AIM- Menu driven program to display frequency of word/words present in a


text file

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

with open('lab5.txt','r') as file:


r = file.read()
print()
print(r)

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

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

for key, values in freq.items():


print(key,":",values)

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

enter line: look at the preety sky


do you want to continue y/n: y
enter line: it is so beautiful.birds are chirping
do you want to continue y/n: n

look at the preety sky


it is so beautiful.birds are chirping

MENU
1.create file
2.capatilize
3.exit

enter option: 2

Look at the preety sky

It is so beautiful.Birds are chirping

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

with open('lab6.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

with open('lab6.txt','r') as file:


r = file.read()
print()
print(r)

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

enter line: my name is arun


do you want to continue y/n: y
enter line: i play football
do you want to continue y/n: n

my name is arun
i play football

MENU
1.create file
2.seperate words by your choice
3.exit

enter option: 2

Enter the character you want it to be separated by : @


my@name@is@arun
i@play@football

MENU
1.create file
2.seperate words by your choice
3.exit

enter option: 3

LAB 7

AIM- Menu driven program to separate words in a text file

CODE –

def new_file():
41

with open('lab7.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

with open('lab7.txt','r') as file:


r = file.read()
print()
print(r)

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

Current working directory:


44

/Users/arjun/Desktop/python/LAB PROGRAMS

enter file name: lab8


File: /Users/arjun/Desktop/python/LAB PROGRAMS\lab8.txt

enter line: my name is arjun


do you want to continue y/n: n

my name is arjun

MENU
1.create file
2.segregate
3.exit

enter option: 2

new working directory:


/Users/arjun/Desktop/python/LAB PROGRAMS\lab8.txt

enter file name: lab8_new


File: C:\Users\Student\Desktop\ARJUN SYAM 12B\LAB PROGRAMS\
LP_8_2_dir\lab8_new.txt

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 = r"C:\Users\Student\Desktop\ARJUN SYAM 12B\LAB


PROGRAMS\LP_8_2_dir"
os.mkdir(new_file_path)
os.chdir(new_file_path)

print("new working directory:\n" +file_path+ "\n")


new_file_name = input("enter file name: ")

new_file_path += "\\"+new_file_name+".txt"
print("File: ",new_file_path,"\n")

with open(file_path) as file1:


with open(new_file_path,"w+") as file2:
with open("file3.txt","w+") as file3:
for i in file1.read().split():
if len(i) == 3:
file2.write(i+" ")

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

file not created please create file

enter line: my Name Is Arjun


48

do you want to continue y/n: y


enter line: HellO
do you want to continue y/n: n

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

AIM - Menu driven program to toggle case in a text file

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

with open('lab9.txt','r') as file:


r = file.read()
print()
print(r)

def toggle_Case():
with open('Lab9.txt','r') as file1:
a = file1.read().swapcase()
print(a)
print()

with open('Lab9.txt','w') as file2:


file2.write(a)

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

enter student number: 1


enter student name: arjun
enter student marks: 99
52

do you want to continue y/n: y

enter student number: 2


enter student name: sam
enter student marks: 78

do you want to continue y/n: y

enter student number: 3


enter student name: bob
enter student marks: 56

do you want to continue y/n: n

+------------+--------------+---------------+
| 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-

from tabulate import tabulate

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)

c = input("do you want to continue y/n: ")


print()
pickle.dump(l,file)

if c == "y":
pass

elif c == "n":
break

with open("lab10.dat","rb") as file:

3. delete
4. update
5. Exit

Enter option : 2

enter student number you want to search: 4


name not found

menu
1. insert
2. search
3. delete
4. update
5. Exit
54

Enter option : 2

enter student number you want to search: 2


+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 2 | sam | 78 |
+------------+--------------+---------------+

menu
1. insert
2. search
3. delete
4. update
5. Exit

Enter option : 3

enter student number you want to delete: 6


name not found
menu
1. insert
2. search
3. delete

try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
while True:

k = pickle.load(file)
data.append(k)

except:
pass

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

print()
55

def search():
with open("lab10.dat","rb") as file:

no = int(input("enter student number you want to search: "))


x=0

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

enter student number you want to delete: 3


+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 1 | arjun | 99 |
| 2 | sam | 78 |
+------------+--------------+---------------+

menu
1. insert
56

2. search
3. delete
4. update
5. Exit

Enter option : 4

Do you want to update existing file y/n: y


enter roll number u want to update: 2

do you want to update roll number y/n: y


enter new roll number: 4

do you want to update student name y/n: n

do you want to update marks y/n: y


enter new marks: 89

+------------+--------------+---------------+
| 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:

with open("lab10.dat","wb") as file:


for i in l:
pickle.dump(i,file)

with open("lab10.dat","rb") as file:


try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
while True:

k = pickle.load(file)
data.append(k)

except:
pass

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

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

elif yes_no == 'y':


ename = int(input("enter roll number u want to update: "))
print()

x=0
l =[]
59

try:
while True:

k = pickle.load(file)
if k[0] == ename:
x=1

stn_y = input("do you want to update roll number y/n: ")


if stn_y == 'y':
stn_c = int(input("enter new roll number: "))
k[0] = stn_c

elif stn_y == 'n':


pass

print()

name_y = input("do you want to update student name y/n: ")


if name_y == 'y':
name_c = input("enter new name: ")
k[1] = name_c
60

elif stn_y == 'n':


pass

print()

stm_y = input("do you want to update marks y/n: ")


if stm_y == 'y':
stm_c = int(input("enter new marks: "))
k[2] = stm_c

elif stn_y == 'n':


pass

print()
l.append(k)
61

elif k[0] != ename:


l.append(k)

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)

with open("lab10.dat","rb") as file:


try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT
MARKS"]]
62

while True:

k = pickle.load(file)
data.append(k)

except:
pass

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

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

enter student number: 1


enter student name: arjun
enter student marks: 99

do you want to continue y/n: y

enter student number: 2


enter student name: sam
enter student marks: 34
66

do you want to continue y/n: y

enter student number: 3


enter student name: bob
enter student marks: 72

do you want to continue y/n: n

+------------+--------------+---------------+
| 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-

from tabulate import tabulate

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)

c = input("do you want to continue y/n: ")


print()
pickle.dump(l,file)

if c == "y":
pass

elif c == "n":
break

with open("lab11.dat","rb") as file:


try:
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
2. search
3. delete
4. update
5. Exit

Enter option : 2

enter student number you want to search: 5


name not found

menu
1. insert
2. search
3. delete
4. update
5. Exit

Enter option : 2

enter student number you want to search: 2


+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
68

+------------+--------------+---------------+
| 2 | sam | 34 |
+------------+--------------+---------------+

menu
1. insert
2. search
3. delete
4. update
5. Exit

Enter option : 3

Enter roll number to delete : 5

+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+

while True:

k = pickle.load(file)
data.append(k)

except:
pass

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

print()

def search():
with open("lab11.dat","rb") as file:

no = int(input("enter student number you want to search: "))


x=0

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

Enter roll number to delete : 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

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

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

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

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

Enter your choice : 1

Enter Student Name : arjun


Enter Student Class : 12
Enter Student Age : 15

do you want to continue y/n : y

Enter Student Name : sam


Enter Student Class : 1
Enter Student Age : 6

do you want to continue y/n : y

Enter Student Name : bob


Enter Student Class : 8
Enter Student Age : 14

do you want to continue y/n : n


78

+--------------+---------------+-------------+
| Student Name | Student Class | Student Age |
+--------------+---------------+-------------+
| arjun | 12 | 15 |
| sam | 1 | 6 |
| bob | 8 | 14 |
+--------------+---------------+-------------+

MENU
1. Insert
2. Search

LAB 12

AIM- Program to update a binary file using file pointers

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

Enter your choice : 2

Enter name of student whose details you want to find : c


Name not found in file

MENU
1. Insert
2. Search
3. Delete
4. Update
5. Exit

Enter your choice : 2

Enter name of student whose details you want to find : bob


+---------------+----------------+--------------+
| Student Name: | Student Class: | Student Age: |
+---------------+----------------+--------------+
| bob | 8 | 14 |
+---------------+----------------+--------------+

MENU
1. Insert
2. Search
3. Delete
4. Update
80

5. Exit

Enter your choice : 3

Enter name of student whose details you want to delete : sam


+-----------------+---------------+----------------+
| Student Number: | Student Name: | Student Marks: |
+-----------------+---------------+----------------+
| arjun | 12 | 15 |
| bob | 8 | 14 |

print(tabulate(main,headers=["Student Name", "Student Class",


"Student Age"],tablefmt = "pretty"))
except:
pass
def delete():
try:
info = {}
file = open("lab12.dat","rb")
filetemp = open("lab12temp.dat","wb")
nval = input("Enter name of student whose details you want to delete : ")
data = pickle.load(file)
for i in data.keys():
if nval == i:
continue
else:
info[i] = data[i]
pickle.dump(info,filetemp)
file.close()
infolist = []
for i in info.keys():
infolist.append(info[i])

print(tabulate(infolist, headers = ["Student Number:", "Student Name:",


"Student Marks:"],tablefmt = "pretty"))
filetemp.close()
os.remove("lab12.dat")
os.rename("lab12temp.dat","lab12.dat")
except:
print("File not created")
def search():
81

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

Enter your choice : 4

+---------------+----------------+--------------+
| Student Name: | Student Class: | Student Age: |
+---------------+----------------+--------------+
| arjun | 12 | 17 |
| bob | 8 | 14 |
+---------------+----------------+--------------+

MENU
1. Insert
2. Search
3. Delete
4. Update
5. Exit

Enter your choice : 5


82

print(tabulate(l, headers = ["Student Name:", "Student Class:", "Student


Age:"],tablefmt = "pretty"))
else:
print("Name not found in file")
except:
print("File not created")
def update():
try:
info = {}
file = open("lab12.dat","rb")
filetemp = open("lab12temp.dat","wb")
data = pickle.load(file)
for i in data.keys():
if data[i][1] == 12:
data[i][2] = 17
info[i] = data[i]
pickle.dump(info,filetemp)
file.close()
infolist = []
for i in info.keys():
infolist.append(info[i])

print(tabulate(infolist, headers = ["Student Name:", "Student Class:",


"Student Age:"],tablefmt = "pretty"))
filetemp.close()
os.remove("lab12.dat")
os.rename("lab12temp.dat","lab12.dat")
except:
print("File not created")
83

while True:
print()
print("MENU")
print("1. Insert")
print("2. Search")
print("3. Delete")
print("4. Update")
print("5. Exit")
print()
84

choice = int(input("Enter your choice : "))


print()
if choice == 1:
create()
display()
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
update()
elif choice == 5:
break
85

OUTPUT LAB13

enter book number, book name and author: 1 a aa

do you want to continue y/n: y

enter book number, book name and author: 2 b bb

do you want to continue y/n: n

content of the file is [book no., book name, book author]:


['1\ta\taa']
['2\tb\tbb']
86

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

with open("lab13.csv",'w',newline = "") as file:


main = []
a = csv.writer(file,delimiter = "\t")
while True:
l = [i for i in input("enter book number, book name and author: ").split(" ")]
print()

y_n = input("do you want to continue y/n: ")


print()

main.append(l)
if y_n == 'y':
pass

elif y_n == 'n':


break
87

a.writerows(main)

with open("lab13.csv",'r') as file:


a = csv.reader(file)
print("content of the file is [book no., book name, book author]: ")
for i in a:
print(i)

OUTPUT LAB14

menu
1. insert
2. search
3. delete
4. update
5. Exit

Enter option : 1

enter student number, student name and marks: 1 arjun 99

do you want to continue y/n: y

enter student number, student name and marks: 2 sam 34

do you want to continue y/n: y

enter student number, student name and marks: 3 bob 56

do you want to continue y/n: n

+------------+--------------+---------------+
| 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()

y_n = input("do you want to continue y/n: ")


print()

main.append(l)
if y_n == 'y':
pass

elif y_n == 'n':


89

break

a.writerows(main)

with open("lab14.csv",'r') as file:


a = csv.reader(file)
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
for i in a:
data.append(i)

enter student number you want to search for: 5


name not found

menu
1. insert
2. search
3. delete
4. update
5. Exit

Enter option : 2

enter student number you want to search for: 2


+------------+--------------+---------------+
| STUDENT NO | STUDENT NAME | STUDENT MARKS |
+------------+--------------+---------------+
| 2 | sam | 34 |
+------------+--------------+---------------+

menu
1. insert
2. search
3. delete
4. update
5. Exit

Enter option : 3

enter student number you want to delete: 5


name not found
90

menu
1. insert
2. search
3. delete
4. update
5. Exit

Enter option : 3

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

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

print("name not found")


print()

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

Do you want to update existing file y/n: y


enter roll number u want to update: 2

do you want to update roll number y/n: y


enter new roll number: 6

do you want to update student name y/n: n

do you want to update marks y/n: y


enter new marks: 32

+------------+--------------+---------------+
| 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)

with open("lab14.csv",'r') as file:


a = csv.reader(file)
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
for i in a:
data.append(i)

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

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

elif yes_no == 'y':


ename = int(input("enter roll number u want to update: "))
print()

x=0
l =[]

for i in a:
if int(i[0]) == ename:
x=1

stn_y = input("do you want to update roll number y/n: ")


if stn_y == 'y':
stn_c = input("enter new roll number: ")
i[0] = stn_c

elif stn_y == 'n':


pass

print()

name_y = input("do you want to update student name y/n: ")


if name_y == 'y':
name_c = input("enter new name: ")
i[1] = name_c

elif stn_y == 'n':


pass

print()
95

stm_y = input("do you want to update marks y/n: ")


if stm_y == 'y':
stm_c = input("enter new marks: ")
i[2] = stm_c

elif stn_y == 'n':


pass

print()
96

l.append(i)

elif int(i[0]) != ename:


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)

with open("lab14.csv",'r') as file:


a = csv.reader(file)
data = [["STUDENT NO", "STUDENT NAME", "STUDENT MARKS"]]
for i in a:
data.append(i)

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


print(table)

print()

while True :
97

print("""menu
1. insert
2. search
3. delete
4. update
5. Exit""")

print()
98

option = int(input('Enter option : '))


print()

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

enter employee no, employee name and salary: 1 arjun 100000

do you want to continue y/n: y

enter employee no, employee name and salary: 2 sam 30000

do you want to continue y/n: y

enter employee no, employee name and salary: 3 bob 5600600

do you want to continue y/n: n

+--------+-------+---------+
| 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

enter column you want to search: g


name not found

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()

y_n = input("do you want to continue y/n: ")


print()

main.append(l)
if y_n == 'y':
pass

elif y_n == 'n':


break

a.writerows(main)
101

with open("lab15.csv",'r') as file:


a = csv.reader(file)
data = [["NUMBER", "NAME", "SALARY"]]
for i in a:
data.append(i)

table = tabulate(data,headers = "firstrow",tablefmt = "pretty")


1. insert
2. display specific column
3. Exit

Enter option : 2

enter column you want to search: salary


+---------+
| SALARY |
+---------+
| 100000 |
| 30000 |
| 5600600 |
+---------+

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

elif no == 'name' or no == 'NAME':


l.append([i[1]])
x=2

elif no == 'salary' or no == 'SALARY':


l.append([i[2]])
x=3

else:
pass

if x == 1:
data = []
103

for i in l:
data.append(i)

table = tabulate(l,headers = ["NUMBER"],tablefmt = "pretty")


print(table)
104

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

enter database name: lab21


enter table name: lab21_table

menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit

Enter option : 2

the database created: lab21


+-----------------+
| Tables_in_lab21 |
+-----------------+
| lab21_table |
+-----------------+

structure of the table:


+----------+----------+------+-----+---------+-------+
108

| Field | Type | Null | Key | Default | Extra |


+----------+----------+------+-----+---------+-------+
| eno | int | YES | | | |
| ename | char(25) | YES | | | |
| dept | char(25) | YES | | | |

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

nightwing = m.connect(host = 'localhost', user = 'root',password = "Batman12")


my = nightwing.cursor()

def creation():
global name
global table_name

name = input("enter database name: ")


my.execute("create database if not exists {}".format(name))
my.execute("use {}".format(name))

table_name = input("enter table name: ")


my.execute("create table if not exists {} (eno int, ename char(25), dept
char(25), title char(25), salary int)".format(table_name))
print()

def show():

print("the database created: ",name)


my.execute("use {}".format(name))
my.execute("show tables")

x = my.fetchall()
109

table = tabulate(x, headers = ["Tables_in_lab21"], tablefmt = 'pretty')


print(table)
print()

| title | char(25) | YES | | | |


| salary | int | YES | | | |
+----------+----------+------+-----+---------+-------+

menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit

Enter option : 3

enter column name: doj


enter column type: date
structure of the table:
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| eno | int | YES | | | |
| ename | char(25) | YES | | | |
| dept | char(25) | YES | | | |
| title | char(25) | YES | | | |
| salary | int | YES | | | |
| doj | date | YES | | | |
+----------+----------+------+-----+---------+-------+

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

enter column name you want to modify: doj


enter new column type: char(20)

my.execute("desc {}".format(table_name))
x = my.fetchall()

print("structure of the table: ")


table = tabulate(x, headers = ["Field","Type","Null","Key","Default","Extra"],
tablefmt = 'pretty')
print(table)
print()

def add():
global col

col =input("enter column name: ")


col_type = input("enter column type: ")

my.execute("alter table {} add column {}


{}".format(table_name,col,col_type))
my.execute("desc {}".format(table_name))

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: ")

my.execute("alter table {} modify {}


{}".format(table_name,modify_col,modify_col_type))
my.execute("desc {}".format(table_name))

x = my.fetchall()
111

print("structure of the table: ")


table = tabulate(x, headers = ["Field","Type","Null","Key","Default","Extra"],
tablefmt = 'pretty')
print(table)
print()
structure of the table:
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| eno | int | YES | | | |
| ename | char(25) | YES | | | |
| dept | char(25) | YES | | | |
| title | char(25) | YES | | | |
| salary | int | YES | | | |
| doj | char(20) | YES | | | |
+----------+----------+------+-----+---------+-------+

menu
1. create database
2. show created databases and tables
3. add attribute
4. modify attribute
5. drop attribute
6. Exit

Enter option : 5

enter column you want to drop: doj


structure of the table:
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| eno | int | YES | | | |
| ename | char(25) | YES | | | |
| dept | char(25) | YES | | | |
| title | char(25) | YES | | | |
| salary | int | YES | | | |
+----------+----------+------+-----+---------+-------+

menu
1. create database
112

2. show created databases and tables


3. add attribute
4. modify attribute
5. drop attribute

def drop():
drop_col = input("enter column you want to drop: ")

my.execute("alter table {} drop column {}".format(table_name,drop_col))


my.execute("desc {}".format(table_name))

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

You might also like