CS_practical_programs-1
CS_practical_programs-1
2) Write a python program to create a csv file for storing the user id and password and display the
contents of the file
import csv
myfile = open('user.csv','w',newline='')
u_writer= csv.writer(myfile)
u_writer.writerow(['USER ID','PASSWORD'])
ch='y'
while ch=='y':
user_id = input('Enter the User id : ')
password= input('Enter the Password : ')
user_detail=[user_id,password]
u_writer.writerow(user_detail)
ch=input("Do you want to enter more record?(y/n)...")
print('File created successfully')
myfile.close()
with open('user.csv','r', newline='\n') as fh:
u_reader= csv.reader(fh)
for rec in u_reader:
print("Password for the user id",rec[0],'is--->',rec[1])
3) Write a Program that reads a text file line by line and prints its statistics like:
Number of uppercase letters:
Number of lowercase letters:
Number of vowels:
Number of consonants:
fh=open('myfile.txt','r')
line=fh.read()
lcount=ucount=0
vcount=ccount=0
for a in line:
if a.isalpha():
if a.islower():
lcount+=1
else:
ucount+=1
if a in 'aeiouAEIOU':
vcount+=1
else:
ccount+=1
print("THE FILE CONTENT")
print(line)
print('-----------------------------------------------------------')
print("Number of uppercase letters:",ucount)
print("Number of lowercase letters:",lcount)
print("Number of vowels:",vcount)
print("Number of consonants:",ccount)
4) Write a menu based program to implement a stack for these book-details (bookno, book
name). That is, now each item node the stack contains two types of information - a bookno
and its name. Just implement Push, Pop and Display operations.
stack=[]
def display():
if(stack==[]):
print("Stack is empty")
else:
print("Stack contents")
for x in range(len(stack)-1,-1,-1):
print(stack[x])
def push():
bdetails=[]
bno=int(input("Enter Book no: "))
bname=input("Enter the Book Name:")
bdetails=[bno,bname]
stack.append(bdetails)
def pop( ):
if(stack==[]):
print("Stack is empty")
else:
item=stack.pop(-1)
print("Deleted element:",item)
print("Stack operation")
print("************")
print("1.PUSH")
print("2.POP")
print("3.DISPLAY")
print("4.EXIT")
while True:
print("------------------------")
choice=int(input("Enter your choice :"))
if choice==1:
push()
elif choice==2:
pop()
elif choice==3:
display()
elif choice==4:
print("Exiting the program...")
break
else:
print("Wrong choice")
5) Write a python program to copy all the lines that start with the character `T' from a text
file and write it to another text file.
Step 1: Open a notepad file, type some content in the notepad file and save the file in the name
“original”.
Step 2:
fin=open("original.txt","r")
fout=open("copied.txt","w")
l1=fin.readlines()
for line in l1:
if line[0] in 'tT':
fout.write(line)
fin.close()
fout.close()
print("Work completed successfully")
6) Write a python program to write the employee details in the binary file and display the
contents of the binary file.
import pickle
emp={}
empfile=open('emp.dat','wb')
ans='y'
while ans.lower()=='y':
eno=int(input("Enter employee number :"))
ename=input("Enter employee name:")
designation=input("Enter designation:")
emp[eno]=[ename,designation]
ans=input("Do you want to enter more record?(y/n)...")
pickle.dump(emp,empfile)
empfile.close()
empfile=open('emp.dat','rb')
emp=pickle.load(empfile)
for i in emp:
print("The details of the employee ",i," is:")
print("Employee Name ------>",emp[i][0])
print("Designation--------->",emp[i][1])
empfile.close()
MYSQL QUESTIONS
1) Consider the table HOSTEL given below and write the output of the SQL queries that follow.
Table: HOSTEL
H_NO CATEGORY WARDER_NAME CHARGES FACILITY STU_NAME
Based on the given table. Write SQL statement for the following:
(i) To create the above table using primary key constraint.
(ii) Delete records of female candidates
(iii) Display all the records of candidate whose stipend is more than 44000.
Based on the given table. Write SQL statement for the following:
(i) To create the above table using primary key constraint.
(ii) Display the details of all activities in which prize money is more than 9000 (including 9000)
(iii) Increase the prize money by 5% of those activities whose schedule date is after 1st of March
2023.
(iv) Delete the record of activity where participants are less than 12.