Unit 4 Python
Unit 4 Python
output file should have only lower-case words, so any upper-case words from
source must be lowered
Ans:
try:
words = infile.read().split()
outfile.write(word + '\n')
except FileNotFoundError:
except Exception as e:
# Example usage
input_file = 'source.txt' # Replace with your input file name
sort_words_in_file(input_file, output_file)
content = file.read()
print(content)
output::
--------
source.txt
-----
Chintapaka
Dum
DuM
sorted_output.txt
------
chintapaka
Ans:
f1 = open("output.txt", "w")
data = myfile.read()
# the string
data_1 = data[::-1]
# following command
f1.write(data_1)
f1.close()
content = file.read()
print(content)
input:
-----
output:
-----
def counter(fname):
num_words = 0
num_lines = 0
num_charc = 0
# line by line
for line in f:
num_lines += 1
word = 'Y'
# count by 1
num_words += 1
word = 'N'
# count by 1
num_spaces += 1
word = 'Y'
for i in letter:
# incrementing character
# count by 1
num_charc += 1
num_words)
num_lines)
num_charc)
num_spaces)
# Driver Code:
if __name__ == '__main__':
fname = 'File1.txt'
try:
counter(fname)
except:
output::
-------
4.write a python program to create ,display, append, insert and reverse the
order of the items in the array
class ArrayManager:
def __init__(self):
self.array = []
def display(self):
self.array.append(item)
return
self.array.insert(index, item)
def reverse(self):
self.array.reverse()
def main():
manager = ArrayManager()
manager.append(1)
manager.append(2)
manager.append(3)
manager.display()
manager.append(4)
manager.append(5)
manager.display()
manager.insert(1, 'a')
manager.display()
# Reverse the array
manager.reverse()
manager.display()
if __name__ == "__main__":
main()
output:
Appended '1' to the array.
5. Write a python program to create add, transpose and multiply two matrices
def create_matrix(rows, cols):
for i in range(rows):
row = input(f"Row {i + 1}: ").split() # Prompt user for input and split it into a list
# Convert input strings to integers
return matrix
for i in range(len(A)):
return result
def transpose_matrix(matrix):
"""Transpose a matrix."""
return transposed
if len(A[0]) != len(B):
raise ValueError("Number of columns in A must be equal to number of rows in B.")
for i in range(len(A)):
for j in range(len(B[0])):
return result
def print_matrix(matrix):
"""Print a matrix."""
def main():
# Create Matrix A
rows_A = int(input("Enter number of rows for Matrix A: "))
A = create_matrix(rows_A, cols_A)
# Create Matrix B
B = create_matrix(rows_B, cols_B)
# Print matrices
print("\nMatrix A:")
print_matrix(A)
print("\nMatrix B:")
print_matrix(B)
# Addition
sum_matrix = add_matrices(A, B)
print("\nA + B:")
print_matrix(sum_matrix)
else:
# Transpose
transposed_A = transpose_matrix(A)
transposed_B = transpose_matrix(B)
print("\nTranspose of A:")
print_matrix(transposed_A)
print("\nTranspose of B:")
print_matrix(transposed_B)
# Multiplication
if cols_A == rows_B:
product_matrix = multiply_matrices(A, B)
print("\nA * B:")
print_matrix(product_matrix)
else:
if __name__ == "__main__":
main()
output:
Enter number of rows for Matrix A: 3
Row 1: 1 2 3
Row 2: 4 5 6
Row 3: 7 8 9
Row 1: 1 2 3
Row 2: 4 5 6
Row 3: 7 8 9
Matrix A:
123
456
789
Matrix B:
123
456
789
A + B:
246
8 10 12
14 16 18
Transpose of A:
147
258
369
Transpose of B:
147
258
369
A * B:
30 36 42
66 81 96