Def Print
Def Print
def print_lines_in_reverse(file_path):
try:
with open(file_path, 'r') as file:
lines = file.readlines()
for line in reversed(lines):
print(line.strip())
except FileNotFoundError:
print(f"The file {file_path} does not exist.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
file_path = input("Enter the path to the file: ")
print_lines_in_reverse(file_path)
PYTHON UNIT 4
try:
print(file_path)
except FileNotFoundError:
except Exception as e:
Program:
# Initialize an empty list (array)
array = []
def display_array(arr):
while True:
print("\nMenu:")
print("6. Exit")
if choice == '1':
print("Array created.")
display_array(array)
array.append(item)
array.insert(position, item)
else:
print("Invalid position.")
array.reverse()
print("Array reversed.")
# Exit
print("Exiting program.")
break
else:
Program:
def get_matrix_input(rows, cols):
matrix = []
for i in range(rows):
matrix.append(row)
return matrix
def transpose_matrix(matrix):
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
def display_matrix(matrix):
# Main program
while True:
print("\nMenu:")
print("4. Exit")
if choice == '1':
print("Matrix A:")
A = get_matrix_input(rows, cols)
print("Matrix B:")
B = get_matrix_input(rows, cols)
result = add_matrices(A, B)
print("Result of addition:")
display_matrix(result)
PYTHON UNIT 4
elif choice == '2':
print("Matrix:")
result = transpose_matrix(matrix)
print("Transposed matrix:")
display_matrix(result)
print("Matrix A:")
A = get_matrix_input(rows_A, cols_A)
if cols_A != rows_B:
continue
print("Matrix B:")
B = get_matrix_input(rows_B, cols_B)
result = multiply_matrices(A, B)
PYTHON UNIT 4
print("Result of multiplication:")
display_matrix(result)
print("Exiting program.")
break
else:
# Base class
class Shape:
def area(self):
def perimeter(self):
# Circle subclass
class Circle(Shape):
self.radius = radius
def area(self):
def perimeter(self):
# Triangle subclass
class Triangle(Shape):
self.base = base
self.height = height
PYTHON UNIT 4
self.side1 = side1
self.side2 = side2
def area(self):
def perimeter(self):
# Square subclass
class Square(Shape):
self.side = side
def area(self):
return self.side ** 2
def perimeter(self):
return 4 * self.side
# Main program
def main():
shapes = []
# Create a circle
shapes.append(Circle(radius))
# Create a triangle
# Create a square
shapes.append(Square(side))
print(f"\nShape: {shape.__class__.__name__}")
print(f"Area: {shape.area():.2f}")
print(f"Perimeter: {shape.perimeter():.2f}")
if __name__ == "__main__":
main()