0% found this document useful (0 votes)
10 views

LabManual(1-13)

Uploaded by

selvaufo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

LabManual(1-13)

Uploaded by

selvaufo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Expt No: 1 R

TEXT FILE HANDLING – I

Write a python program to read a text file ‘file1.txt’ line by line


and display each word separated by “#”

Aim:

To write a python program to read a text file ‘file1.txt’ line


by line and display each word separated by ‘#’.

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

Name of the Text file created in notepad: file1.txt


Content of the file:
Learning programs in python develops our knowledge

Output:

Learning# programs# in# python# develops# our# knowledge#


>>>
Expt No: 2 R
TEXT FILE HANDLING – II

Write a python program to read a text file ‘file1.txt’ and display


the number of vowels/ consonants/ Lowercase/ Uppercase characters in
the file.

Aim:

To write a python program to read a text file ‘file1.txt’ and


display the number of vowels/ consonants/ Lowercase/ Uppercase
characters in the file.

Source Code:

f = open ('file1.txt', 'r')


contents = f.read()
vowels=0
consonants = 0
lower_case = 0
upper_case = 0
for ch in contents:
if ch in 'aeiouAEIOU':
vowels = vowels + 1
if ch not in 'aeiouAEIOU' and ch.isalpha():
consonants = consonants + 1
if ch.islower():
lower_case = lower_case + 1
if ch.isupper():
upper_case = upper_case + 1
f.close()
print('The total no of vowels in the file: ', vowels)
print('The total no of consonants in the file: ',
consonants)
print('The total no of uppercase letters in the file: ',
upper_case)
print('The total no of lowercase letters in the file: ',
lower_case)

Result:
Thus the program was executed successfully and the output is
verified
Expt No: 2 L

Name of the Text file created in notepad: file1.txt


Content of the file:
Learning programs in python develops our knowledge

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

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’.

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

Alex eagerly looks at outer space with his telescope.


The balloon popped.
Artists play guitars loudly during music shows.
The bat hung upside down in the tree.

Python executed program output:

All lines which are starting with character 'a' or 'A' has been
successfully copied into 'new.txt'

new.txt

Alex eagerly looks at outer space with his telescope.


Artists play guitars loudly during music shows.
EXPT NO: 4 R
Write a method Disp() in Python to read lines from sample.txt and
display those words which are less than 5 characters.

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

Alex eagerly looks at outer space with his telescope.


The balloon popped.
Artists play guitars loudly during music shows.
The bat hung upside down in the tree.

Python executed program output:

The following words are less than 5 characters


Alex at with his The play The bat hung down in the
EXPT NO: 5 R
Creating a Python Program to Create and Search in
Binary File
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.

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:

Enter the Roll Number: 202401


Enter the Name: Viknesh
Do you want to add another student detail(Y/N): Y
Enter the Roll Number: 202402
Enter the Name: Ganesh
Do you want to add another student detail(Y/N): Y
Enter the Roll Number: 202403
Enter the Name: Inigo
Do you want to add another student detail(Y/N): Y
Enter the Roll Number: 202404
Enter the Name: Packiaraj
Do you want to add another student detail(Y/N): N
Enter the roll number of the student to be searched: 202403
The searched Roll_No is found and details are [202403, 'Inigo']
EXPT NO: 6 R
Creating a Python Program to Create and Update/Modify
Records in Binary File
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.

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:

Enter the Roll Number: 202401


Enter the Name: Viknesh
Enter the Mark: 60
Do you want to add another student detail(Y/N): Y
Enter the Roll Number: 202402
Enter the Name: Ganesan
Enter the Mark: 65
Do you want to add another student detail(Y/N): Y
Enter the Roll Number: 202403
Enter the Name: Inigo
Enter the Mark: 62
Do you want to add another student detail(Y/N): Y
Enter the Roll Number: 202404
Enter the Name: Packiaraj
Enter the Mark: 45
Do you want to add another student detail(Y/N): N
Enter the roll number of the student to be Modify: 202404
The searched Roll_No is found and details are [202404, 'Packiaraj', '45']
Enter new mark to be updated: 68
Mark updated successfully and details are: [202404, 'Packiaraj', 68]
EXPT NO: 7 R
CSV File Handling
Creating a Python program to create and search employee’s record in
CSV file.

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:

Enter the Employee Number: 202401


Enter the Employee Name: Viknesh
Enter the Employee Salary: 25000
Do you want to continue(Y/N)?: Y
Enter the Employee Number: 202402
Enter the Employee Name: Ganesan
Enter the Employee Salary: 25000
Do you want to continue(Y/N)?: Y
Enter the Employee Number: 202403
Enter the Employee Name: Inigo
Enter the Employee Salary: 20000
Do you want to continue(Y/N)?: Y
Enter the Employee Number: 202404
Enter the Employee Name: Packiaraj
Enter the Employee Salary: 18000
Do you want to continue(Y/N)?: N
Enter the Employee Number to Search: 202402
The searched Employee Number is not found

Employee Details are:


=======================
Name: Ganesan
Salary: 25000.0
=======================
EXPT NO: 8 R
Arithmetic Operation
Creating a menu driven python program to perform Arithmetic
operations (+, -, *, /) based on the users choice.

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:

Enter the Employee Number: 202401


Enter the Employee Name: Viknesh
Enter the Employee Salary: 25000
Do you want to continue(Y/N)?: Y
Enter the Employee Number: 202402
Enter the Employee Name: Ganesan
Enter the Employee Salary: 25000
Do you want to continue(Y/N)?: Y
Enter the Employee Number: 202403
Enter the Employee Name: Inigo
Enter the Employee Salary: 20000
Do you want to continue(Y/N)?: Y
Enter the Employee Number: 202404
Enter the Employee Name: Packiaraj
Enter the Employee Salary: 18000
Do you want to continue(Y/N)?: N
Enter the Employee Number to Search: 202402
The searched Employee Number is not found

Employee Details are:


=======================
Name: Ganesan
Salary: 25000.0
=======================
EXPT NO: 11 R
Factorial Using Functions
Creating a menu driven python program to find factorial and sum of
list of numbers using function.

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:

For First Choice:


1. To find Factorial
2. To find Sum of List of Elements
Enter your Choice: 1
Enter a number to find Factorial: 5
The factorial of 5 is 120

For Second Choice:


1. To find Factorial
2. To find Sum of List of Elements
Enter your Choice: 2
Enter how many elements you want to store in List?: 5
Enter Element: 10
Enter Element: 20
Enter Element: 30
Enter Element: 40
Enter Element: 50
The sum of List is: 150
EXPT NO: 12 R
Mathematical Functions
Creating a menu driven python program to implement Mathematical
Functions.

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:

The Square of a Number is: 25.0


The Log of a Number is: 1.0
The Quad of a Number is: 5.385164807134504
EXPT NO: 13 R
Stack Operations
Creating a python program to implement Stack Operations Using List.

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:

Performing Stack Operations Using List

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

Do you want to perform another operation (Y/N)?: Y

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

Do you want to perform another operation (Y/N)?: y

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

Do you want to perform another operation (Y/N)?: y

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

Do you want to perform another operation (Y/N)?: Y

1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 4
[3, 'Pravin']

Do you want to perform another operation (Y/N)?: y

1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 3
The top of the stack is: [3, 'Pravin']

Do you want to perform another operation (Y/N)?: Y

1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 2
The deleted doctor detail is: [3, 'Pravin']

Do you want to perform another operation (Y/N)?: Y

1. PUSH
2. POP
3. PEEK
4. DISP
Enter your Choice: 4
The Stack is Empty

Do you want to perform another operation (Y/N)?: N

You might also like