python unit-4

 Unit-4 programs

Program-1:

Aim: Write a python program to sort words in a file and put them in another file. The output file should have only lowercase words, so any uppercase words from source must be lowered.

Program:

def sort_words(input_file,output_file):

    with open(input_file,'r') as file:

        words=file.read().split()

    words=sorted([word.lower() for word in words])

    with open(output_file,'w') as file:

        file.write("\n".join(words))

input_file='input.txt'

output_file='output.txt'

sort_words(input_file,output_file)

print(f"words sorted and saved to'{output_file}'")

 

output:







Program-2:

Aim: Python program to print each line of a file in reverse order.

Program:

def reverse_lines(input_file):

    with open(input_file,'r')as file:

        lines=file.readlines()

    for line in reversed(lines):

        print(line.rstrip())

 

input_file='input.txt'

reverse_lines(input_file)

 

output:






Program-3:

Aim: Python program to compute the number of characters, words and lines in a file.

Program:

def count_chars_words_lines(input_file):

    with open(input_file,'r')as file:

        lines=file.readlines()

    num_lines=len(lines)

    num_words=sum(len(line.split())for line in lines)

    num_chars=sum(len(line)for line in lines)

    print(f"Number of lines:{num_lines}")

    print(f"Number of words:{num_words}")

    print(f"Number of characters:{num_chars}")

input_file='input.txt'

count_chars_words_lines(input_file)

 

output:








Program-4:

Aim: Write a program to create, display, append, insert and reverse the order of the items in the array.

Program:

class ArrayOperations:

    def __init__(self):

        self.array=[]

    def create_array(self,elements):

        self.array=elements

    def display_array(self):

        print("Array:",self.array)

    def append_element(self,element):

        self.array.append(element)

    def insert_element(self,index,element):

        self.array.insert(index,element)

    def reverse_array(self):

        self.array.reverse()

arr=ArrayOperations()

arr.create_array([1,2,3,4,5])

arr.display_array()

arr.append_element(6)

arr.display_array()

arr.insert_element(2,10)

arr.display_array()

arr.reverse_array()

arr.display_array()

 

output:










Program-5:

Aim: Write a program to add, transpose, and multiply two matrices.

Program:

matrix1 = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]

matrix2 = [[9, 8, 7],[6, 5, 4],[3, 2, 1]]

add_r=[[matrix1[i][j] + matrix2[i][j] for j in range(len(matrix1[0]))] for i in range(len(matrix1))]

transpose_r=[[matrix1[j][i] for j in range(len(matrix1))] for i in range(len(matrix1[0]))]

multiply_r=[[sum(matrix1[i][k] * matrix2[k][j] for k in range(len(matrix1[0]))) for j in range(len(matrix2[0]))] for i in range(len(matrix1))]

print("Matrix Addition:")

for row in add_r:

  print(row)

print("\nMatrix Transpose:")

for row in transpose_r:

  print(row)

print("\nMatrix Multiplication:")

for row in multiply_r:

  print(row)

 

output:







Program-6:

Aim: Write a python program to create a class that represents a shape. Include methods to calculate its area and perimeter. Implement subclasses for different shapes like circle, triangle, and square.

Program:

import math

class Shape:

    def area(self):

        pass

    def perimeter(self):

        pass

class Circle(Shape):

    def __init__(self,radius):

        self.radius=radius

    def area(self):

        return math.pi*self.radius

class Triangle(Shape):

    def __init__(self,base,height,side1,side2):

        self.base=base

        self.height=height

        self.side1=side1

        self.side2=side2

    def area(self):

        return 0.5*self.base*self.height

    def perimeter(self):

        return self.base+self.side1+self.side2

class Square(Shape):

    def __init__(self,side):

        self.side=side

    def area(self):

        return self.side**2

    def perimeter(self):

        return 4*self.side

circle=Circle(5)

print("Circle Area:",circle.area())

print("Circle Perimeter:",circle.perimeter())

triangle=Triangle(3,4,5,5)

print("\n Triangle Area:",triangle.area())

print("Triangle Perimeter:",triangle.perimeter())

square=Square(4)

print("\nSquare Area:",square.area())

print("Square Perimeter:",square.perimeter())

 

output:













Comments

Popular posts from this blog

python unit-3 programs

Unit-5 programs