Wonder of Modern Science
Wonder of Modern Science
PRACTICAL FILE
Made By
Manit Jawa 12A
Roll No. 14677109
1
INDEX
SNo Program Description Page Teacher’s
. No. Signature
1. Write a program to implement all user defined functions of a list. 5
2. Write a program to implement all user defined functions of a Dictionary. 7
3. Write a program to implement all user defined functions of a String. 9
4. Write a program to implement all user defined functions of a Tuple. 12
5. Write a program to generate random numbers between 1 to 6 and check 13
whether a user won a lottery or not.
6. Write a menu driven program to perform the following: 14
i. To remove all even numbers from a list.
ii. To display frequencies of all the element of a list.
iii. To find and display the sum of all the values which are ending with 3
from a list.
7. Write a menu driven program to implement Linear and Binary Search. 16
8. Write a menu driven program for to implement Bubble Sort, Insertion 18
Sort.
9. Write a menu driven program (use functions)to find whether a number is: 20
a. Prime or not
b. Even or odd
c. Positive or Negative
d. Armstrong Number or Not
e. Palindrome or Not
10. Write a menu driven program (use functions)to calculate the following in a 23
range entered by the user:
a. Factorial
b. Fibonacci Series
c. Prime numbers
d. Even and Odd Numbers
11. Write a menu driven program to find out sum of two numbers using: 27
a. Positional Arguments
b. Default Arguments
c. Keyword Arguments
d. Variable-Length Arguments
12. Write a program to read a text file line by line and display each word 29
separated by “#”.
13. Write a Program to read a text file “Welcome.txt” and count the following: 30
Lines, Words, Characters, Upper/ Lower Characters, Digits, Symbols,
Vowels, Consonants.
14. Write a Program to read text from One.txt and 32
i. copy in Two.txt
ii. Rewrite in the same file One.txt
Note: Copy only those lines that contain a word student (ignore Case
2
Sensitivity)
15. Write a Program in Python to create two text files -myfile7_I.txt, 34
myfile7_II.txt containing Name of students (entered by the user) as per the
following:
myfile7_I.txt -Name of students starting from “A” to “M”
myfile7_II.txt -Name of students starting from “N” to “Z”
Note: Ignore Case Sensitivity
16. Write a function disp_wl() in python to read lines from a text file 36
“SAMPLE.TXT” and display those words which are less than 4 characters
long and display the number of lines starting with “M”.
17. Create a binary file with roll number, name and marks. Input a roll number 38
and update the marks.
18. Write a program to read data from a text file and copy the contents in a 39
new file.
Note: Copy only those words that start with either “A” or “K”.
19. Write a Program to read text from file One.txt and replace the occurrence 40
of “this” with “that” and restore the result in two.txt.
20. Write a program to read data from a csv file and write data to a csv file. 42
21. Write a program to create a library in python and import it in a program. 44
22. Write a menu based program to perform the operation on Stack in Python. 45
23. Take a sample of ten phishing e-mails (or any text file) and find most 47
commonly occurring word(s).
24. Write a Program in Python to enter roll number, name and marks of 49
students in physics, chemistry and maths, and calculate percentage.
i. Write complete data in a binary file (mymarks.dat) and display the
details.
ii. Search for a given roll number and display the name, if not found
display appropriate message.
25. Given a binary file “EMP.DAT”, containing records of the following type: 51
[E_NO,E_NAME,SAL]
Where these three values are:
E_NO - Employee Number(string)
E_NAME - Employee Name(string)
SAL-Salary of Employee(float)
Write a Python function for the following:
i. Write the records of the employees and display all the records.
ii. Read the contents of the file “EMP.DAT” and display the details of
those employees whose salary is above 25000.
iii. Read the employee no and update the employee name.
iv. Read the employee no and delete the record of employee.
26. Write a menu driven program (use functions): 53
i. To create the database Cust_Details with table name Customer having
following fields:
CustNO, CustName, City, Email, Amount
ii. Add new records.
iii. Display all the records
iv. Display first two records.
27. Write a menu driven program (use functions): 56
i. To create the database Product_Details with table name Product having
3
following fields:
Product_NO, Product_Name,Quantity, Amount, Date of Purchase
ii. Add new records.
iii. Modify a record on the basis of Product Number entered by the user
and display the updated record.
28. Write program to delete a record from the Customer Table on the basis of 59
the Customer Number entered by the user and display all the records after
deleting it.
29. Write a Python program to perform the following on Product Table: 60
i. Display the total price of all the products.
ii. Display the products with price in the range of 1500 and 2500.
iii. Display total products.
30. Write a Python Program to perform the following: 61
i. To create the database Student_Details with table name Student having
following fields:
S_NO, S_Name,, Class, Date of Admsn
ii. Display the Structure of the table
iii. Change the column name S_No to Roll No. and display the new
structure
4
Q1)Write a program to implement all user defined functions of a list.
Code:
l=list(input ("Enter the list separated by a space:").split())
for k in range(len(l)):
l[k]=int(l[k])
#APPENDING THE LIST
a=int(input("Enter the element to be appended"))
l.append(a)
print ("Appended list:",l)
#INSERTING INTO THE LIST
b=int(input("Enter the element to be inserted"))
c=int(input("Enter the index where the element is to be inserted"))
l.insert(c, b)
print (l)
#FINDING THE LENGTH OF THE LIST
print ("The length of the list is:",len (l))
#COUNTING THE OCCURCENCE OF AN ELEMENT
d=int(input("Enter the element whose occurence is to be counted"))
print("The number of times", d, "occurs is", l.count(d))
#EXTENDING A LIST
l2=list(input("Enter the list that is to be extended").split())
l.extend(l2)
print("Extended list:",l)
for t in range(len(l)):
l[t]=int(l[t])
#FINDING SUM OF ALL ELEMENTS
sum=0
for i in l:
sum=sum+int(i)
print("The sum of the elements is:",sum)
5
#TO POP AN ELEMENT
m=int(input("Enter the index the element which has to be deleted"))
l.pop(m)
print("New list:", l)
#TO REMOVE AN ELEMENT
n=int(input ("Enter the element which has to be deleted"))
l.remove(n)
print("New list:",l)
#TO REVERSE A LIST
l.reverse()
print("List after reversal: ",l)
#TO SORT A LIST
l.sort()
print("Sorted 1ist:", l)
Output:
6
Q2) Write a program to implement all user defined functions of a
Dictionary.
Code:
dic={"Name":"Manit","Class":12,"Roll Number":22}
#UPDATING A DICTIONARY
updated_dic={"Name":"Rashmi"}
dic.update(updated_dic)
print(dic)
#COPY FUNCTION
dic.copy()
#FROMKEY FUNCTION
a=dic.fromkeys(dic)
print("Fromkey Function:",a)
7
dic.popitem()
print(dic)
#CLEAR FUNCTION
dic.clear()
Output:
8
Q3) Write a program to implement all user defined functions of a String.
Code:
a="This is a sample string."
#length
print(len(a))
#alphanumeric
if a.isalnum():
print("Contains only alphanumeric charachters")
else:
print("Contains charachters other than alphanumeric also.")
#alphabets
if a.isalpha():
print("contains only alphabets.")
else:
print("contains charachters other than alphabets also.")
#numeric values
if a.isdigit():
print("contains only digits")
else:
print("contains charachters others than digits also.")
#spaces
if a.isspace():
print("contains only spaces")
else:
print("contains charachters other than spaces also.")
#upper case
if a.isupper():
print("contains only upper case charachters")
else:
print("contains charachters in lower cases also.")
9
#lower case
if a.islower():
print("contains only lower case charachters")
else:
print("contains upper case charachters also.")
#first letter capital or not
if a[0].isupper():
print("First charachter is upper case")
else:
print("First charachter is not upper case.")
#convert to upper
print(a.upper())
#convert to lower
print(a.lower())
#capitalize firt letter
a[0].upper()
print(a)
#count occurence of substring
c=0
x=input("enter string you want to count:")
for i in a:
if i==x:
c=c+1
print(c)
#first occurence of substring
for i in range(len(a)):
if a[i]==x:
print("found at" , i+1 , "th place")
break
#replace
10
a.replace("This" , "this")
#swapcase
print(a.swapcase())
#partition
print(a.partition(" "))
#join list and string
print(a+"[2,3,4,5]")
#split
print(a.split(" "))
#lstrip, rstrip, strip
print(a.lstrip())
print(a.rstrip())
print(a.strip())
Output:
11
Q4) Write a program to implement all user defined functions of a Tuple.
Code:
t=(1,2,5,6,1,8,2,3,10)
t=t+(3,)
print(t)
d=int(input ("Enter the element whose occurence is to be counted"))
print("The number of times", d, "occurs is", t.count (d))
sum=0
for i in t:
sum=sum+int(i)
print("The sum of the elements is:",sum)
Output:
12
Q5) Write a program to generate random numbers between 1 to 6 and
check whether a user won a lottery or not.
Code:
import random
a=random.randint(1,6)
b=int(input("Enter a number between 1 to 6 for the lotery"))
if a==b:
print("Congratulations!You won the lottery")
else:
print("Better luck next time")
Output:
13
Q6) Write a menu driven program to perform the following:
I. To remove all even numbers from a list.
II. To display frequencies of all the element of a list.
III. To find and display the sum of all the values which are ending with 3
from a list.
Code:
l=list(input("Enter the list").split())
print("1.Remove all even numbers 2.Display frequencies")
print("3.Sum of all the values ending with 3")
ch=int(input("Enter your choice"))
for i in range(0,len(l)):
l[i]=int(l[i])
if ch==1:
for j in l:
if (j%2)==0:
l.remove(j)
print("The new list is",l)
elif ch==2:
for k in l:
c=l.count(k)
print("The frequency of",k,"is",c)
elif ch==3:
sum=0
for m in l:
m=str(m)
if m[-1]=='3':
m=int(m)
sum=sum+m
print("The sum of all numbers ending with 3 is:",sum)
else:
print("Invalid choice")
14
Output:
15
Q7) Write a menu driven program to implement Linear and Binary
Search.
Code:
def bsearch(list, beg, end, x):
while beg<=end:
mid=beg+(end-beg)//2
if x==list[mid]:
return mid
elif x>list[mid]:
beg=mid+1
else:
end=mid-1
else:
return -1
while True:
print("menu: 1.Linear Search 2.Binary Search")
c=input("enter choice:")
if c=="1":
d=[4,2,3,7,8,6]
x=int(input("enter element you want to search:"))
f=False
for i in d:
if i==x:
f=True
print(x , "is found")
break
if f=="False":
print(x, "not found.")
16
if c=="2":
list=[2,3,4,10,30]
x=10
pos=bsearch(list,0,len(list)-1 , x)
if pos!=-1:
print("element found at index", pos)
else:
print("element not found")
e=input("Do you want to continue? ")
if e=="n":
exit()
Output:
17
Q8) Write a menu driven program for to implement Bubble Sort, Insertion
Sort.
Code:
while True:
print("menu: 1.Bubble Sort 2.Insertion Sort")
c=input("enter choice:")
if c=="1":
d=[4,2,3,7,8,6]
a=len(d)
for i in range(a-1):
for j in range(a-1-i):
if d[j]>d[j+1]:
d[j],d[j+1]=d[j+1],d[j]
print(d)
if c=="2":
list=[2,3,4,10,30]
for k in range(len(list)):
list[k]=int(list[k])
18
exit()
Output:
19
Q9)Write a menu driven program (use functions)to find whether a number
is:
a. Prime or not
b. Even or odd
c. Positive or Negative
d. Armstrong Number or Not
e. Palindrome or Not
Code:
while True:
print("menu: 1.Prime/Not 2.Even/Odd 3.Positive/Negative 4.ArmstrongNo. 5.
Palindrome/Not")
c=input("enter choice:")
if c=="1":
n=int(input("enter a number"))
if n==0 or n==1:
print("Neither Prime nor composite")
elif n==2:
print("Even prime")
else:
f=0
for i in range(2,(n//2)+1):
if n%i==0:
f=1
if f==1:
print("Not Prime")
else:
print("Prime")
elif c=="2":
n=int(input("enter a number:"))
20
if n%2==0:
print("even")
else:
print("odd")
elif c=="3":
n=int(input("enter a number:"))
if n>0:
print("Positive")
else:
print("Negative")
elif c=="4":
n=int(input("enter a number:"))
num=n
s=0
while num>0:
d=num%10
s=s+(d**3)
num=num//10
if s==n:
print("ArmstrongNo")
else:
print("Not")
elif c=="5":
n=int(input("enter a number:"))
num=n
s=0
while num>0:
s=(s*10) + (num%10)
21
num=num//10
if n==s:
print("Palindrome")
else:
print("Not")
e=input("Do you want to continue? ")
if e=="n":
exit()
Output:
22
Q10) Write a menu driven program (use functions)to calculate the
following in a range entered by the user:
a. Factorial
b. Fibonacci Series
c. Prime numbers
d. Even and Odd Numbers
Code:
def prime(n):
if n==0 or n==1:
print("Neither Prime nor composite")
elif n==2:
print("Even prime")
else:
f=0
for i in range(2,(n//2)+1):
if n%i==0:
f=1
if f==1:
return "Not Prime"
else:
return "Prime"
def evenodd(n):
if n%2==0:
return "even"
else:
return "odd"
def fibo(n):
i=1
a=0
23
b=1
print(a,b)
while i<n-2:
s=a+b
print(s)
a=b
b=s
i=i+1
return "The above is a Fibonacci series"
def factorial(n):
i=1
f=1
while i<=n:
f=f*i
i=i+1
return f
while True:
print("menu: 1.Prime 2.Even/Odd 3.Factorial 4.Fibonacci : ")
n1=int(input("enter starting number:"))
n2=int(input("enter last number:"))
c=input("enter choice:")
if c=="1":
for i in range(n1,n2+1):
print(i ,"is" ,prime(i))
elif c=="2":
24
for i in range(n1,n2+1):
print(i ,"is" , evenodd(i))
elif c=="3":
for i in range(n1, n2+1):
print("factorial of " , i , "is" , factorial(i))
elif c=="4":
for i in range(n1, n2+1):
print(fibo(i))
Output:
25
26
Q11) Write a menu driven program to find out sum of two numbers using:
a. Positional Arguments
b. Default Arguments
c. Keyword Arguments
d. Variable-Length Arguments
Code:
while True:
print("Menu: 1.Positional Arguments 2.Default Arguments 3.Keyword Arguments
4.Variable lenght Arguments 5.exit")
c=input("enter your choice:")
def posadd(num1, num2):
return num1+num2
def defadd(num1=2,num2=5):
return num1+num2
def keyadd(num1,num2):
return num1+num2
def varadd(*num1):
return sum(num1)
if c=="1":
print(posadd(1,2))
if c=="2":
print(defadd(5))
if c=="3":
print(keyadd(num1=5,num2=2))
if c=="4":
print(varadd(5,10,15))
if c=="5":
exit()
27
Output:
28
Q12) Write a program to read a text file line by line and display each word
separated by '#‟.
Code:
f=open("f.txt" , "r")
text=f.read()
text=text.split(" ")
for i in range(len(text)):
if text[i]=="#":
print(text[i-1] , text[i+1])
f.close()
Output:
29
Q13. Write a Program to read a text file “Welcome.txt” and count the
following:
Lines, Words, Characters, Upper/ Lower Characters, Digits, Symbols,
Vowels, Consonants
Code:
f=open("Welcome.txt","r")
l=w=ch=up=low=d=S=v=cons=0
s=f.readline()
while s:
l=l+1
for i in s.split(" "):
w=w+1
for j in i:
ch=ch+1
if j.isupper()==True:
up=up+1
if j in "AEIOU":
v=v+1
elif j.islower()==True:
low=low+1
if j in "aeiou":
v=v+1
elif j.isdigit()==True:
d=d+1
elif j.isspace()==False:
S=S+1
s=f.readline()
30
print("Lines:",l,"Words",w,"Characters:",ch,"Vowels:",v,"Consonants:",ch-v-d-
S,"Symbols:",S,"Digits:",d)
print("upp case:",up,"lower case:",low)
f.close()
Output:
31
Q14) Write a Program to read text from One.txt and
i. copy in Two.txt
ii. Rewrite in the same file One.txt
Note: Copy only those lines that contain a word student (ignore Case
Sensitivity)
Code:
import os
f=open("One.txt" , "r+")
text=f.readlines()
f.seek(0)
g=open("Two.txt" , "w")
for i in text:
for j in i.split():
if j=="student":
g.write(i)
f.write(i)
g.close()
f.close()
os.remove("One.txt")
os.rename("Two.txt" , "One.txt")
Output:
32
33
Q15) Write a Program in Python to create two text files -myfile7_I.txt,
myfile7_II.txt containing Name of students (entered by the user) as per the
following:
myfile7_I.txt -Name of students starting from “A‟ to “M‟
myfile7_II.txt -Name of students starting from “N‟ to “Z‟
Note: Ignore Case Sensitivity
Code:
f=open("myfile7_I.txt" , "w")
g=open("myfile7_II.txt" , "w")
t="abcdefghijklm"
while True:
name=input("enter name:")
if name[0] in t:
f.write(name)
else:
g.write(name)
c=input("continue?")
if c=="n":
exit()
g.close()
f.close()
Output:
34
35
Q16) Write a function disp_wl() in python to read lines from a text file
“SAMPLE.TXT” and display those words which are less than 4 characters
long and display the number of lines starting with “M”.
Code:
def disp_wl():
c=0
f=open("SAMPLE.txt" , "r")
text=f.readlines()
for i in text:
if i[0]=="M":
c=c+1
for j in i.split():
if len(j)<4:
print(j)
print(c)
f.close()
disp_wl()
Output:
36
37
Q17) Create a binary file with roll number, name and marks. Input a roll
number and update the marks.
Code:
import pickle
f=open("t.dat" , "rb+")
rec=[12, "Manit" , 98]
pickle.dump(rec,f)
f.seek(0)
data=pickle.load(f)
rec=data[1]
rec=[rec[0], rec[1] ,rec[2]]
rec[2]=99
pickle.dump(rec,f)
f.close()
Output:
38
Q18) Write a program to read data from a text file and copy the contents in
a new file.
Note: Copy only those words that start with either “A” or “K”.
Code:
f=open("One.txt" , "r")
g=open("Two.txt" , "r+")
for i in f.read().split():
if i[0]=="A" or i[0]=="K":
g.write(i)
g.close()
f.close()
Output:
39
Q19) Write a Program to read text from file One.txt and replace the
occurrence of “this” with “that” and restore the result in two.txt.
Code:
f=open("One.txt" , "r")
text=f.read()
text=text.replace("This" , "That")
f.close()
g=open("two.txt" , "r+")
g.write(text)
g.close()
Output:
40
41
Q20) Write a program to read data from a csv file and write data to a csv
file.
Code:
import csv
f=open("a.csv" , "r+")
print("The file contains the following:")
csv_reader=csv.reader(f)
for i in csv_reader:
if i==[]:
pass
else:
print(i)
f.close()
f=open("a.csv" , "w+")
csv_w=csv.writer(f)
csv_w.writerow(["145" , "Jasmine"])
csv_w.writerow(["14" , "Tina"])
f.close()
f=open("a.csv" , "r+")
print("The file contains the following:")
csv_reader=csv.reader(f)
for i in csv_reader:
if i==[]:
pass
else:
print(i)
f.close()
42
Output:
43
Q21) Write a program to create a library in python and import it in a
program.
Code:
import mymodule
mymodule.mymodule()
Output:
44
Q22) Write a menu based program to perform the operation on Stack in
python.
Code:
a=[5,6,7,8,19,34,2,546,23]
while True:
print("1. Push 2. Pop 3.Display 4. Exit")
c=input("enter choice:")
if c=="1":
n=input("enter element:")
a.append(n)
elif c=="2":
print("element deleted" , a.pop())
elif c=="3":
for i in range(len(a)-1, -1, -1):
print(a[i])
elif c=="4":
exit()
c=input("do you want to continue ?")
if c=="n":
exit()
Output:
45
46
Q23) Take a sample of ten phishing e-mails (or any text file) and find most
commonly occurring word(s).
Code:
f=open("One.txt" , "r")
d=f.read().split()
c=0
f.seek(0)
for i in d:
c=0
x=i
for j in d:
if j==i:
c=c+1
print(x, "occured" , c, "times")
f.close()
Output:
47
48
Q24) Write a Program in Python to enter roll number, name and marks of
students in physics, chemistry and maths, and calculate percentage.
i. Write complete data in a binary file (mymarks.dat) and display the
details.
ii. Search for a given roll number and display the name, if not found
display appropriate message.
Code:
import pickle
g=open("mymarks.dat" , "wb+")
for i in range(1,4):
name=input("enter name:")
roll=input("enter roll no:")
pmarks=int(input("enter marks in physics"))
mmarks=int(input("enter marks in maths:"))
cmarks=int(input("enter marks in chemistry:"))
percent=(mmarks+pmarks+cmarks)/3
rec=[name, roll, pmarks, mmarks, cmarks, percent]
pickle.dump(rec,g)
g.seek(0)
newrec=[]
while True:
try:
rec=pickle.load(g)
newrec.append(rec)
print(rec)
except:
break
49
for t in newrec:
if t[1]==r:
print("roll no found!")
print(t[0])
f=1
break
if f==0:
print("not found!")
g.close()
Output:
50
Q25) Given a binary file “EMP.DAT”, containing records of the following
type:
[E_NO,E_NAME,SAL]
Where these three values are:
E_NO - Employee Number(string)
E_NAME - Employee Name(string)
SAL-Salary of Employee(float)
Write a Python function for the following:
i. Write the records of the employees and display all the records.
ii. Read the contents of the file “EMP.DAT” and display the details of those
employees whose salary is above 25000.
iii. Read the employee no and update the employee name.
iv. Read the employee no and delete the record of employee.
Code:
import pickle
f=open("EMP.dat" , "rb+")
data=[[1,"Name1" , 67000] , [2,"Name2", 89009] , [3,"Name3" , 87288] , [4,"Name4",
12333]]
for i in data:
pickle.dump(i,f)
f.seek(0)
while True:
try:
print(pickle.load(f))
except:
break
f.seek(0)
print("Salary greater than 25000:")
while True:
try:
51
rec=pickle.load(f)
if rec[2]>25000:
print(rec)
except:
break
f.seek(0)
f.close()
Output:
52
Q26) Write a menu driven program (use functions):
i. To create the database Cust_Details with table name Customer having
following fields:
CustNO, CustName, City, Email, Amount
ii. Add new records.
iii. Display all the records
iv. Display first two records.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost" , user="root" , passwd="root")
mycursor=mydb.cursor()
def crdatab():
mycursor.execute("create database cust_details")
mycursor.execute("use cust_details")
mycursor.execute("create table customer(CustNO int, CustName char(50), City char(50),
Email char(50), Amount int)")
mydb.commit()
def addrec():
custno=int(input("enter custno"))
custName=input("enter CustName")
city=input("enter city")
email=input("enter email")
amount=int(input("enter amount:"))
d=[custno , custName , city , email , amount]
mycursor.execute("insert into customer values(%s,%s,%s,%s,%s)" , d)
mydb.commit()
def disall():
53
mycursor.execute("select * from customer")
records=mycursor.fetchall()
for i in records:
print(i)
def distwo():
mycursor.execute("select * from customer")
records=mycursor.fetchall()
print(records[0])
print(records[1])
while True:
print("menu 1.create database and table 2.add data 3.display all data 4. display only first
two rows")
c=input("enter choice")
if c=="1":
crdatab()
elif c=="2":
addrec()
elif c=="3":
disall()
elif c=="4":
distwo()
c=input("do you want to continue ?")
if c=="n":
exit()
Output:
54
55
Q27) Write a menu driven program (use functions):
i. To create the database Product_Details with table name Product having
following fields:
Product_NO, Product_Name,Quantity, Amount, Date of Purchase
ii. Add new records.
iii. Modify a record on the basis of Product Number entered by the user
and display the updated record.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost" , user="root" , passwd="root")
mycursor=mydb.cursor()
def crdatab():
mycursor.execute("create database Product_Details")
mycursor.execute("use Product_Details")
mycursor.execute("create table Product(Product_NO int, Product_Name char(50) ,Quantity
int, Amount int, Date_of_Purchase date)")
mydb.commit()
def addrec():
Product_NO=int(input("enter Product_NO"))
productName=input("enter product name")
Quantity=input("enter Quantity")
amount=int(input("enter amount:"))
dop=input("enter date of Purchase")
d=[Product_NO , productName , Quantity, amount , dop]
mycursor.execute("insert into Product values(%s,%s,%s,%s,%s)" , d)
mydb.commit()
def modify():
oProduct_NO=int(input("enter Product_NO which you would like to modify:"))
56
Product_NO=int(input("enter new Product_NO"))
d=[Product_NO , oProduct_NO]
mycursor.execute("update Product set Product_NO=%s where Product_NO=%s" , d)
mydb.commit()
mycursor.execute("select * from Product where Product_NO=%s" , [Product_NO])
for j in mycursor:
print(j)
while True:
print("menu 1.create database and table 2.add data 3.modify")
c=input("enter choice")
if c=="1":
crdatab()
elif c=="2":
addrec()
elif c=="3":
modify()
c=input("do you want to continue ?")
if c=="n":
exit()
Output:
57
58
Q28) Write program to delete a record from the Customer Table on the
basis of the Customer Number entered by the user and display all the
records after deleting it.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost" , user="root" , passwd="root")
mycursor=mydb.cursor()
Output:
59
Q29) Write a Python program to perform the following on Product Table:
i. Display the total price of all the products.
ii. Display the products with price in the range of 1500 and 2500.
iii. Display total products.
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost" , user="root" , passwd="root")
mycursor=mydb.cursor()
mycursor.execute("use product_details")
mycursor.execute("select sum(amount) from product")
rec=mycursor.fetchone()
print(rec)
Output:
60
Q30) Write a Python Program to perform the following:
i. To create the database Student_Details with table name Student having
following fields:
S_NO, S_Name,, Class, Date of Admsn
ii. Display the Structure of the table
iii. Change the column name S_No to Roll No. and display the new
structure
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost" , user="root" , passwd="root")
mycursor=mydb.cursor()
Output:
61
62