Shashaa FINAL PRACTICAL
Shashaa FINAL PRACTICAL
CENTRE
SQL programs
20 SQL EXERCISE -1 30-31
21 SQL EXERCISE -2 32-33
22 SQL EXERCISE -3 34-35
23 SQL EXERCISE -4 36-37
24 SQL EXERCISE -5 38-40
1. Read a text file line by line and display each word and
separated by #
fp=open ("demo.txt","r")
lines=fp.readlines()
for line in lines:
words=line.split()
for word in words:
print(word+"#",end=" ")
print()
fp.close()
OUTPUT
1|P a ge
2. Read a text file and display the number of consonants vowels
,upper case, lower case
fp=open("sample text.txt","r")
v=c=low=up=0
word=fp.read()
for i in word :
if i.isalpha():
if i in 'aeiou AEIOU':
v=v+1
else:
c=c+1
if i.isupper():
up=up+1
else:
low=low+1
print("the number of vowels =",v)
print ("the number of consnants= ",c)
print("the number of lowercases= ",low)
print("the number of uppercase= ",up)
OUTPUT
2|P a ge
3. Remove all the lines that contain the character “a” in a file
and write it to another file
fp=open("sample text.txt","r")
lines=fp.readlines()
fp.close()
fp1=open("first.txt","w")
fp2=open("second.txt","w")
for line in lines:
if 'a' in line or 'A' in line:
fp2.write(line)
else:
fp1.write(line)
fp1.close()
fp2.close()
The given sample file
3|P a ge
4.(i) Create a binary file with a name and roll number search for a
given roll number and display the appropriate message(list)
import pickle
fp=open("student.dat","wb")
l=[ ]
while True:
rollno=int(input("enter the roll no"))
name=input("enter the name")
data=[rollno,name]
l.append(data)
pickle.dump(l,fp)
ch=input("enter Y for more records OR enter N or n for exit")
if ch in "Nn":
break
fp.close()
fp=open("student.dat","rb")
flag=0
try:
sno=int(input("enter the roll no to search"))
while True:
data=pickle.load(fp)
for i in data:
if i[0] == sno:
print("the name is ",i[1])
flag=1
break
except EOFError:
fp.close()
if (flag == 0):
print("nothing found")
4|P a ge
(ii) Create a binary file with a name and roll number search for a
given roll number and display the appropriate message(dictionary)
import pickle
fp=open("student.dat","wb")
d={ }
while True:
r=int(input("enter the roll no"))
n=input("enter the name")
d['roll']=r
d['name']=n
pickle.dump(d,fp)
ch=input("enter Y for more records OR enter N or n for exit")
if ch in "Nn":
break
fp.close()
fp=open("student.dat","rb")
flag=0
try:
sno=int(input("enter the roll no to search"))
while True:
data=pickle.load(fp)
if data['roll'] == sno:
print("the name is ",data['name'])
flag=1
break
except EOFError:
fp.close()
if (flag == 0):
print("nothing found")
5|P a ge
OUTPUT for the above code (both list and dictionary)
6|P a ge
5. (i) Create a binary file with a name and roll number search for a given
roll number and marks and input a roll number and update the marks
(list format)
import pickle
l=[ ]
fp=open("updatemarks.dat","wb")
while True:
r=int(input("Enter the roll no"))
n=input("Enter the name")
m=int(input("Enter the marks"))
data=[r,n,m]
l.append(data)
pickle.dump(l,fp)
ch=input("enter Y for more records OR enter N or n for exit")
if ch in "Vn":
break
fp.close()
fp=open("updatemarks.dat","rb")
f=0
try:
sno=int(input("Enter the Roll no to be searched"))
while True:
data=pickle.load(fp)
for i in data:
if i[0]==sno:
sm=int(input("Enter the new marks"))
i[2]=sm
print ("the updatedmarks",i)
f=1
break
except EOFError:
if(f==0):
print("Record is not found")
7|P a ge
(ii) Create a binary file with a name and roll number search for
a given roll number and marks and input a roll number and
update the marks(dictionary format)
import pickle
d={}
fp=open("updatemarks.dat","wb")
while True:
r=int(input("enter the roll number"))
n=input("enter the name")
m=int(input("enter the marks"))
d['roll']=r
d['name']=n
d['marks']=m
pickle.dump(d,fp)
ch=input("enter Y for more records OR enter N or n for exit")
if ch in 'Nn':
break
fp.close()
fp=open("updatemarks.dat","rb")
f=0
try:
sno=int(input("enter the roll no to search"))
while True:
data=pickle.load(fp)
if data['roll']==sno:
sm=int(input("enter marks new"))
data['marks']=sm
print(d,"update marks")
f=1
break
8|P a ge
except EOFError:
fp.close
if (f==0):
Print("record not found")
9|P a ge
6. Write a random number generator that generates
random numbers between 1 and 6 and stimulates a dice
import random
print("============ROLLING DICE ================")
while True:
num=random.randint(1,6)
if num == 6:
print("CONGRATS YOU GOT =",num)
else:
print("you have got",num)
ch=input("once again roll dice ")
if ch in 'Nn':
break
10 | P a g e
7. Write a python program to implement stack using list
stack = []
def push():
value = int(input("Enter an element: "))
stack.append(value)
def pop():
if len(stack) == 0:
print("Stack is empty")
else:
a = stack.pop()
print("The removed element is", a)
def peek():
if len(stack) == 0:
print("Stack is empty")
else:
index = len(stack) - 1
print("The last element is", stack[index])
def display():
if len(stack) == 0:
print("Stack is empty")
else:
for i in stack:
print(i)
while True:
op = int(input("Press 1 for push, 2 for pop, 3 for peek, 4 for display, 5 for exit: "))
11 | P a g e
if op == 1:
push()
elif op == 2:
pop()
elif op == 3:
peek()
elif op == 4:
display()
elif op == 5:
break
else:
print("Invalid option. Please try again.")
12 | P a g e
8. Create a csv file by entering user id and password ,read
and search the password for the given user id
import csv
with open("password.csv", "w", newline='') as fp:
w = csv.writer(fp)
w.writerow(["used Id", "password"])
while True:
username = input("Enter the username: ")
password = input("Enter the password: ")
w.writerow([username, password])
ch = input("Press 'y' for more records or 'n' to exit: ")
if ch.lower() == 'n':
break
f=0
with open("password.csv", "r") as fp:
vid = input("Enter username to search: ")
reader = csv.reader(fp)
next(reader)
for row in reader:
if row[0] == vid:
print("The given password is", row[1])
f=1
break
if f == 0:
print("Record not found")
13 | P a g e
OUTPUT for the given code
14 | P a g e
9. Prime number code
start=int(input("enter the start value"))
end=int(input("enter the end value"))
for i in range(start,end+1):
flag=0
if i == 1:
continue
for j in range(2,i):
if i%j == 0:
flag=1
break
if flag == 0:
print(i,"is prime number")
15 | P a g e
10. Fibonacci code
n=int(input("upto how many terms"))
a,b=0,1
count=0
if n<=0:
print("Error! enter positive number")
elif n == 1:
print("fibonacci series upto",n,".")
print(a)
else:
print("fibbonacci series ")
while count<n:
print(a,end=' ')
c=a+b
a=b
a=c
count+=1
OUTPUT
16 | P a g e
11. Armstrong number
n = int(input("Enter the number: "))
temp = n
arm = 0
while n > 0:
d = n % 10
arm += d ** 3
n //= 10
if arm == temp:
print("The number is an Armstrong number.")
else:
print("The number is not an Armstrong number.")
OUTPUT
17 | P a g e
12. Arithmetic operations
print("=======Arithmetic operations======")
def add():
a=int(input("enter first value="))
b=int(input("enter the second value="))
c=a+b
print("the addition of the given two numbers",c)
def sub():
a=int(input("enter first value="))
b=int(input("enter the second value="))
c=a-b
print("the subtracion of the given two numbers",c)
def mul():
a=int(input("enter first value="))
b=int(input("enter the second value="))
c=a*b
print("the multiplication of the given two numbers",c)
def div():
a=int(input("enter first value="))
b=int(input("enter the second value="))
try:
c=a/b
print("the divisions of the given two number=",c)
except:
print("please dont enter zero as a denominator value")
18 | P a g e
while True:
op=int(input("press 1 for addition,press 2 for subtraction,press 3 for
multiplication,4 for division,5 for exit"))
if op == 1:
add()
elif op == 2:
sub()
elif op == 3:
mul()
elif op == 4:
div()
elif op == 5:
break
else :
print("please enter valid option")
OUTPUT
19 | P a g e
13. List program
list1=[]
n=int(input("Enter the range:"))
for i in range(n):
element=int(input("Enter the element:"))
list1.append(element)
print("The given list is:",list1)
k=int(input("Enter the element to be searched:"))
for i in list1:
flag=0
if i==k:
flag=1
break
if flag==1:
print("Element is found")
else:
print("Element is not found")
OUTPUT
20 | P a g e
14. List program (swapping using list)
list1=[]
n=int(input("Enter the range:"))
for i in range(n):
element=int(input("Enter the element:"))
list1.append(element)
print("The given list before swapping is:",list1)
for i in range(0,len(list1)-1,2):
list1[i],list1[i+1]=list1[i+1],list1[i]
OUTPUT
21 | P a g e
15. Dictionary program
dict1={}
n=int(input("enter the range"))
for i in range(n):
Rollno=int(input("Enter the roll number"))
Name=input("Enter the name")
Marks=int(input("Enter the marks"))
dict1[Rollno]=[Name,Marks]
print("The Given Dictionary is",dict1)
for i in dict1:
if dict[i][1]>75:
print("The Name of the student who score more than 75 marks
is:",dict1[i][0])
OUTPUT
22 | P a g e
MySQL connectivity programs
16. creating a python program to integrate MySQL with python
(creating database and table )
import mysql.connector
def Create_DB():
Con=mysql.connector.connect(host="localhost",user="root",password="deeksha2024")
try:
if Con.is_connected():
cur=Con.cursor()
Q='create database employee'
cur.execute(Q)
print("employee database created successfully")
except:
print("database name already exists")
Con.close()
def Create_Table():
Con=mysql.connector.connect(host="localhost",user="root",password="deeksha2024")
try:
if Con.is_connected():
cur=Con.cursor()
Q="use employee"
cur.execute(Q)
Q="create table Emp(ENO integer primary key,ENAME varchar(20),GENDER varchar(5),SAL integer)"
cur.execute(Q)
print("emp table create successfully")
except:
print("Table name is already exists")
Con.close()
23 | P a g e
ch='y'
while ch=='y' or ch=='Y':
print("\nInterfacing Python with Mysql")
print("1. To create Database")
print("2.To create Table")
print("3.For exits")
opt=int(input("enter your choice:"))
if opt==1:
Create_DB()
elif opt==2:
Create_Table()
else:
break
OUTPUT
24 | P a g e
17.To write a python program to integrate MySQL with python
by inserting records to EMp table and display the records
import mysql.connector
con=mysql.connector.connect(host='localhost',username='root',password=“deeksha2024”,database='employee')
try:
if con.is_connected():
cur=con.cursor()
opt='y'
while opt=='y':
No=int(input("Enter Employee number"))
Name=input("Enter Employee name")
Gender=input("Enter Employee gender(M/F):")
Salary=int(input("Enter Employee Salary:"))
Query="insert into Emp values({},'{}','{}',{})".format(No,Name,Gender,Salary)
cur.execute(Query)
con.commit()
print("Record stored successfully")
opt=input("Do you want to add another employee details(y/N):")
except:
print("Records already exists")
25 | P a g e
OUTPUT
26 | P a g e
18. Creating a python program to integrate MySQL with python
(searching and displaying the records)
import mysql.connector
con=mysql.connector.connect(host='localhost',username='root',password=“deeksha2024”,database='employee')
if con.is_connected():
cur=con.cursor()
print("*******************************")
print("Welcome Employee Search Screen")
print("*******************************")
No=int(input("Enter the employee number to search:"))
Query='select *from emp where Empid={}'.format(No)
cur.execute(Query)
data=cur.fetchone()
if data!=None:
print(data)
else:
print("Record not found")
con.close()
OUTPUT
27 | P a g e
19.Creating python program to integrate MySQL with python
(updating records)
import mysql.connector
con=mysql.connector.connect(host='localhost',username='root',password=’deeksha2024’,database='employee' )
if con.is_connected():
cur=con.cursor()
print("****************************************************")
print("Welcome to Employee detail update screen")
print("****************************************************")
No=int(input("Enter Employee number to update"))
Query="Select *from Emp where empid={}".format(No)
cur.execute(Query)
data=cur.fetchone()
if data!=None:
print("Record found details are")
print(data)
ans=input("DO you want to update the salary of the employee(y/n):")
if ans=='y' or ans=='Y':
New_sal=int(input("Enter new salary of the employee"))
Q1="update Emp set sal={} where empid={}".format(New_sal,No)
cur.execute(Q1)
con.commit()
print("Employee Salary updated successsfully")
Q2='select *from Emp'
cur.execute(Q2)
data=cur.fetchall()
for i in data:
print(i)
else:
print("Record is not found")
28 | P a g e
OUTPUT
29 | P a g e
SQL PROGRAMES
20. To write Queries for the following Questions based on the given table.
AIM:
USE STUDENTS;
SHOW DATABASES;
30 | P a g e
e) Write a Query to List all the tables that exists in the current database.
SHOW TABLES;
31 | P a g e
21. To write Queries for the following Questions based on the given table.
AIM
a) Write a Query to insert all the rows of above table into Info table.
b) Write a Query to display all the details of the Student from the table “STU”.
32 | P a g e
d) Write a Query to select distinct Department from STU table.
33 | P a g e
22. To write Queries for the following Questions based on the given table.
AIM:
b) Write a Query to List name of the student whose age are between 18 to 20.
c) Write a Query to display the name of the students whose name starts with “A”.
34 | P a g e
d) Write a Query to list the names of those students whose name have second alphabet
‘n’ in their names.
35 | P a g e
23. To write Queries for the following Questions based on the given table.
AIM:
b) Write a Query to change the fees of a student to 170 whose Roll Number is 1,if the
existing fees is less than 130.
c) Write a Query to add new column AREA of type varchar in table “STU”.
36 | P a g e
e) Write a Query to delete Area column from the table STU.
37 | P a g e
AIM:-24
To write Queries for the following Questions based on the given table.
TABLE:UNIFORM
TABLE:COST
a) To Display the average price of all the Uniform of Raymond Company from table
Cost.
b) To display details of all the Uniform table in descending order of Stock date.
38 | P a g e
c) To display max price and min price of each company
d)To display the company were the number of uniform size is more than 2
39 | P a g e
e)to display ucode ,uname,ucolor,size and company of tables uniform and cost
select u.ucode,uname,ucolor,size,company from uniform u,cost c
where u.ucode=c.ucode;
40 | P a g e