Experiment No.: - 6.
6 (a). Write a program to print each line of a file in reverse order.
1. User must enter a file name.
2. The file is opened using the open() function and all lines are stored in a list.
3. reversed() function produces a reverse iterator.
4. All the lines are then printed in reverse order using a for loop and rstrip() function strips all the blank
spaces/white spaces from the end of the line.
Program:
s = "Hello Python! "
res = [Link]()
print(res)
Output:
Hello Python!
Create a text file ([Link]) containing following text:--
1. Python is an high level programming language.
2. Python is interpreted and object-oriented.
3. Python is simple and easy to learn syntax.
4. Python supports several modules and packages.
Program :
def read_file_reverse(filename):
try:
with open(filename, 'r') as file:
lines = [Link]()
# Sorting based on the serial number (assuming "N. " format)
[Link](key=lambda x: int([Link]('.')[0]), reverse=True)
print("File content in reverse serial order:")
for line in lines:
print([Link]())
except Exception as e:
print(f"Error: {e}")
# Provide the filename here
filename = "[Link]"
read_file_reverse(filename)
Output :
File content in reverse serial order:
4. Python supports several modules and packages.
3. Python is simple and easy to learn syntax.
2. Python is interpreted and object-oriented.
1. Python is an high level programming language.
Program :
def print_lines_in_reverse_order(filename):
try:
with open(filename, 'r') as file:
lines = [Link]()
for line in lines:
print([Link]()[::-1])
except FileNotFoundError:
print(f"Error: The file '{filename}' was not found.")
except Exception as e:
print(f"An error occurred: {e}")
filename = "[Link]" # Replace with your file name
print_lines_in_reverse_order(filename)
Output:
.egaugnal gnimmargorp level hgih na si nohtyP .1
.detneiro-tcejbo dna deterpretni si nohtyP .2
.xatnys nrael ot ysae dna elpmis si nohtyP .3
.segakcap dna seludom lareves stroppus nohtyP .4
6 (b). Write a program to compute distance between two points taking input from the user.
Distance can be calculated using the two points (x1, y1) and (x2, y2), the distance d between these points is
given by the formula:
The formula of distance between two point (x1, y1) and (x2, y2) is as follows:--
Distance= √ (x2−x1) +(y2−y1)
2 2
for e.g :
let x1=4, y1= 1 and x2 = 10, y2=9 then (x2-x1)2=(10-4)2 = 62 = 36 and (y2-y1)2= (9-1)2 = 82 = 64 now 64 +
36 =100 and 100 is square root of 10. Therefore, distance between (4,1) and (10,9) is 10 .
Program-1
x1=int(input("enter x1 : "))
y1=int(input("enter y1 : "))
x2=int(input("enter x2 : "))
y2=int(input("enter y2 : "))
result= ((((x2 - x1 )**2) + ((y2-y1)**2) )**0.5)
print("distance between",(x1,x2),"and",(y1,y2),"is : ",result)
Output
enter x1 : 4
enter y1 : 1
enter x2 : 10
enter y2 : 9
distance between (4, 1) and (10, 9) is : 10.0
Program -2
import math
a=input("enter first coordinate : ")
p1 = [Link](",")
b=input("enter second coordinate : ")
p2 = [Link](",")
distance = [Link]( ((int(p1[0])-int(p2[0]))**2)+((int(p1[1])-int(p2[1]))**2) )
print("distance between ", a,"and", b, "is",distance)
Output:
enter first coordinate : 4,1
enter second coordinate : 10,9
distance between 4,1 and 10,9 is 10.0
6 (c). Write a function nearly equal to test whether two strings are nearly equal. Two strings a and
b are nearly equal when a can be generated by a single mutation on.
string : A string is defined as a collection of one or more characters. It is putted in single quotes or double
quotes or triple quotes i.e (' ') , (" ") or (' ' ' ' ' ') .
for eg: s="GITCollege"
Program :
string1 = input("Enter first string: ")
string2 = input("Enter second string: ")
if string1 == string2:
print("\nBoth strings are equal to each other.")
print(string1,"==",string2);
else:
print("\nStrings are not equal.")
print(string1,"!=",string2);
Output
Enter first string: abcd
Enter second string: abcd
Both strings are equal to each other.
abcd == abcd
Enter first string: '''GIT Collge. Jaipur City. Rajasthan.'''
Enter second string: '''GIT Collge. Jaipur City. Rajasthan.'''
Both strings are equal to each other.
'''GIT Collge. Jaipur City. Rajasthan.''' == '''GIT Collge. Jaipur City. Rajasthan.'''
Enter first string: Ankita
Enter second string: Ankit
Strings are not equal.
Ankita != Ankit
Viva Questions:
1. What are Dict and List comprehensions?
2. Is multiple inheritance supported in Python? Explain.
3. What is the difference between range & xrange?
4. What is pickling and unpickling?
5. Is Python fully object oriented? Explain.