LabManual(1-13)
LabManual(1-13)
Aim:
Source Code:
f = open('file1.txt', 'r')
content = f.readlines()
for line in content:
words = line.split(' ')
for i in words:
print(i+'#', end=' ')
f.close()
Result:
Thus the program was executed successfully and the output is
verified
Expt No: 1 L
Output:
Aim:
Source Code:
Result:
Thus the program was executed successfully and the output is
verified
Expt No: 2 L
Output:
The total no of vowels in the file: 15
The total no of consonants in the file: 29
The total no of uppercase letters in the file: 1
The total no of lowercase letters in the file: 43
>>>
EXPT NO: 3 R
TEXT FILE HANDLING – III
Aim:
To write a python program to read lines from a text file
‘sample.txt’ and copy those lines into another file which are
starting with the alphabet ‘a’ or ‘A’.
Source Code:
f1 = open ('sample.txt', 'r')
f2 = open ('new.txt', 'w')
while True:
line = f1.readline()
if line == '':
break
if line[0] == 'a' or line[0] == 'A':
f2.write(line)
print ("All lines which are starting with character 'a' or 'A' has
been successfully copied into 'new.txt'")
f1.close()
f2.close()
Result:
Thus the above Python program is executed successfully and the
output is verified.
Expt No: 3 L
OUTPUT:
Sample.txt
All lines which are starting with character 'a' or 'A' has been
successfully copied into 'new.txt'
new.txt
AIM:
To write a method Disp() in Python to read lines from
sample.txt and display those words which are less than 5 characters.
Source Code:
def Disp():
f = open ('sample.txt')
s = f.read()
w = s.split()
print('The following words are less than 5 characters')
for i in w:
if len(i)<5:
print(i, end = ' ')
f.close()
Disp()
Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 4 L
OUTPUT:
Sample.txt
AIM:
To write a program in Python to create a binary file with roll
number and name, search for a given roll number and display the
name. If not found, display appropriate message.
Source Code:
import pickle
def create():
f = open ('students.dat', 'ab')
opt = 'Y'
while opt == 'Y':
Roll_No = int(input('Enter the Roll Number: '))
Name = input('Enter the Name: ')
L = [Roll_No, Name]
pickle.dump(L,f)
opt=input('Do you want to add another student detail(Y/N):')
f.close()
def search():
f = open('students.dat', 'rb')
no=int(input('Enter the roll number of the student to be searched:'))
found = 0
try:
while True:
s = pickle.load(f)
if s[0] == no:
print('The searched Roll_No is found and details are', s)
found = 1
break
except:
f.close()
if found == 0:
print('The searched Roll_No is not found')
create()
search()
Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 5 L
OUTPUT:
AIM:
To write a program in Python to create a binary file with roll
number and name, mark and update/modify the mark for a given roll
number.
Source Code:
import pickle
def create():
f = open ('marks.dat', 'ab')
opt = 'Y'
while opt == 'Y':
Roll_No = int(input('Enter the Roll Number: '))
Name = input('Enter the Name: ')
Mark = input('Enter the Mark: ')
L = [Roll_No, Name, Mark]
pickle.dump(L,f)
opt = input('Do you want to add another student detail(Y/N): ')
f.close()
def update():
f = open('marks.dat', 'rb+')
no = int(input('Enter the roll number of the student to be Modify: '))
found = 0
try:
while True:
pos = f.tell()
s = pickle.load(f)
if s[0] == no:
print('The searched Roll_No is found and details are', s)
s[2] = int(input('Enter new mark to be updated: '))
f.seek(pos)
pickle.dump(s,f)
found = 1
f.seek(pos)
print('Mark updated successfully and details are: ', s)
break
except:
f.close()
if found == 0:
print('The searched Roll_No is not found')
create()
update()
Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 6 L
OUTPUT:
AIM:
To write a Python program to create a CSV file to store Empno,
Name, Salary and search any Empno and display Name, Salary and if
not found display appropriate message.
Source Code:
import csv
def Create():
F = open('Emp.csv', 'a', newline = '')
W = csv.writer(F)
opt = 'Y'
while opt == 'Y':
No = int(input('Enter the Employee Number: '))
Name = input('Enter the Employee Name: ')
Sal = float(input('Enter the Employee Salary: '))
L = [No, Name, Sal]
W.writerow(L)
opt = input('Do you want to continue(Y/N)?: ')
F.close()
def Search():
F = open('Emp.csv', 'r', newline = '\r\n')
No = int(input('Enter the Employee Number to Search: '))
found = 0
row = csv.reader(F)
for data in row:
if data[0] == str(No):
print('\n Employee Details are: ')
print('=======================')
print('Name: ', data[1])
print('Salary: ', data[2])
print('=======================')
found = 1
break
if found == 0:
print('The searched Employee Number is not found')
F.close()
Create()
Search()
Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 7 L
OUTPUT:
AIM:
To write a menu driven python program to perform Arithmetic
opherations (+, -, *, /) based on the users choice.
Source Code:
import csv
def Create():
F = open('Emp.csv', 'a', newline = '')
W = csv.writer(F)
opt = 'Y'
while opt == 'Y':
No = int(input('Enter the Employee Number: '))
Name = input('Enter the Employee Name: ')
Sal = float(input('Enter the Employee Salary: '))
L = [No, Name, Sal]
W.writerow(L)
opt = input('Do you want to continue(Y/N)?: ')
F.close()
def Search():
F = open('Emp.csv', 'r', newline = '\r\n')
No = int(input('Enter the Employee Number to Search: '))
found = 0
row = csv.reader(F)
for data in row:
if data[0] == str(No):
print('\n Employee Details are: ')
print('=======================')
print('Name: ', data[1])
print('Salary: ', data[2])
print('=======================')
found = 1
break
if found == 0:
print('The searched Employee Number is not found')
F.close()
Create()
Search()
Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 7 L
OUTPUT:
AIM:
To write a menu driven python program to find factorial and
sum of list of numbers using function.
Source Code:
def factorial(no):
f=1
if no <0:
print('Sorry, we cannot take factorial for Negative Numbers')
elif no == 0:
print('The factorial of 0 is 1')
else:
for i in range (1, no+1):
f = f*i
print('The factorial of ', no , ' is ',f)
def sum_list(L):
sum = 0
for i in range(n):
sum = sum + L[i]
print ('The sum of List is: ', sum)
print('1. To find Factorial')
print('2. To find Sum of List of Elements')
opt = int(input('Enter your Choice: '))
if opt == 1:
n = int(input('Enter a number to find Factorial: '))
factorial(n)
elif opt == 2:
L = []
n = int(input('Enter how many elements you want to store in List?: '))
for i in range(n):
ele = int(input('Enter Element: '))
L.append(ele)
sum_list(L)
Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 11 L
OUTPUT:
AIM:
To write a menu driven python program to implement
Mathematical Functions.
Source Code:
import math
def Square(num):
s = math.pow(num,2)
return s
def Log(num):
s = math.log10(num)
return s
def Quad(x,y):
s = math.sqrt(x**2 + y**2)
return s
print ('The Square of a Number is: ', Square(5))
print ('The Log of a Number is: ', Log(10))
print ('The Quad of a Number is: ', Quad(5, 2))
Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 12 L
OUTPUT:
AIM:
To write python program to implement Stack Operations using a
List data structure, to perform the following operations.
i) To push an object containing Doc_ID and Doc_Name of doctors
who specialize in ‘ENT’ to the stack.
ii) To Pop the objects from the stack and display them.
iii) To find the position of Stack Top.
iv) To display the elements of the Stack (after performing PUSH
and POP).
Source Code:
def Push():
Doc_ID = int(input('Enter the Doctor ID: '))
Doc_Name = input('Enter the Name of the Doctor: ')
Mob = int(input('Enter the Mobile Number of the Doctor: '))
Special = input('Enter the Specialization: ')
if Special == 'ENT':
stack.append([Doc_ID, Doc_Name])
def Pop():
if stack == []:
print('Stack is Empty')
else:
print('The deleted doctor detail is: ', stack.pop())
def Peek():
if stack == []:
print('Stack is Empty')
else:
top = len(stack)-1
print('The top of the stack is:', stack[top])
def Disp():
if stack == []:
print('The Stack is Empty')
else:
top = len(stack)-1
for i in range(top, -1, -1):
print(stack[i])
stack = []
ch = 'y'
print('Performing Stack Operations Using List \n')
while ch=='y' or ch=='Y':
print()
print('1. PUSH')
print('2. POP')
print('3. PEEK')
print('4. DISP')
opt = int(input('Enter your Choice: '))
if opt == 1:
Push()
elif opt == 2:
Pop()
elif opt == 3:
Peek()
elif opt == 4:
Disp()
else:
print('Invalid Choice, Try Again!!!')
ch = input('\n Do you want to perform another operation (Y/N)?: ')
Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 13 L
OUTPUT:
1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 1
Enter the Doctor ID: 01
Enter the Name of the Doctor: Mathew
Enter the Mobile Number of the Doctor: 32654
Enter the Specialization: Cardio
1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 1
Enter the Doctor ID: 02
Enter the Name of the Doctor: Arul
Enter the Mobile Number of the Doctor: 132654
Enter the Specialization: Neuro
1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 1
Enter the Doctor ID: 03
Enter the Name of the Doctor: Pravin
Enter the Mobile Number of the Doctor: 654987
Enter the Specialization: ENT
1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 1
Enter the Doctor ID: 04
Enter the Name of the Doctor: Ibrahim
Enter the Mobile Number of the Doctor: 565487
Enter the Specialization: Ortho
1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 4
[3, 'Pravin']
1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 3
The top of the stack is: [3, 'Pravin']
1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 2
The deleted doctor detail is: [3, 'Pravin']
1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 4
The Stack is Empty