Categories:Viewed: 7 - Published at: a few seconds ago

Write a python Class to calculate area of a rectangle and print the area

class rectangle():
    def __init__(self,breadth,length):
        self.breadth=breadth
        self.length=length
    def area(self):
        return self.breadth*self.length
a=6
b=4
obj=rectangle(a,b)
print("Area of rectangle:",obj.area())

Write a python Class to calculate area of a circle and print the vale for a radius

class CircleArea():
    def __init__(self,radius):
        self.radius=radius
    def area(self):
        return 3.14 * self.radius * self.radius
a=6
obj=CircleArea(a)
print("Area of rectangle:",obj.area())

Write a python Class to calculate Perimeter of a circle and print the vale for a radius

class CirclePerimeter():
    def __init__(self,radius):
        self.radius=radius
    def perimeter(self):
        return 2 * 3.14 * self.radius
a=6
obj=CirclePerimeter(a)
print("Perimeter of rectangle:",obj.perimeter())

Write a python Class to print All Possible Subsets from a Set of Distinct Integers

class sub:  
    def f1(self, s1):  
        return self.f2([], sorted(s1))  

    def f2(self, curr, s1):  
        if s1:  
            return self.f2(curr, s1[1:]) + self.f2(curr + [s1[0]], s1[1:])  
        return [curr]  
a=[2, 3, 5, 6, 4, 5]

print("Subsets: ")
print(sub().f1(a))

Write a python program to Read and print the Contents of a File

a=str(input("Enter file name .txt extension:"))
file2=open(a,'r')
line=file2.readline()
while(line!=""):
    print(line)
    line=file2.readline()
file2.close()

Write a python program to Count and print the Number of Words in a Text File

fname = input("Enter file name: ")

num_words = 0

with open(fname, 'r') as f:
    for line in f:
        words = line.split()
        num_words += len(words)
print("Number of words:")
print(num_words)

Write a python program to Count the Number of Lines in a Text File

fname = input("Enter file name: ")
num_lines = 0
with open(fname, 'r') as f:
    for line in f:
        num_lines += 1
print("Number of lines:")
print(num_lines)

Write a python program to Count the Occurrences of a Word in a Text File

fname = input("Enter file name: ")
word='the'
k = 0

with open(fname, 'r') as f:
    for line in f:
        words = line.split()
        for i in words:
            if(i==word):
                k=k+1
print(f"Frequency of Occurrences of the word {a} is:")
print(k)

Write a python function to Copy the Contents of One File into Another

def copy(from_file, to_file):
    with open(from_file) as f:
        with open(to_file, "w") as f1:
            for line in f:
                f1.write(line)

Write a python function that Counts the Number of Times a Certain Letter Appears in the Text File

def count_letter(fname, l):
    k = 0
    with open(fname, 'r') as f:
        for line in f:
            words = line.split()
            for i in words:
                for letter in i:
                    if(letter==l):
                        k=k+1
    return k

Write a python function that Print all the Numbers Present in the Text File

def print_number(fname):
    with open(fname, 'r') as f:
        for line in f:
            words = line.split()
            for i in words:
                for letter in i:
                    if(letter.isdigit()):
                        print(letter)

Write a python function that Counts the Number of Blank Spaces in a Text File

def count_blank_space(fname):
    k = 0
    with open(fname, 'r') as f:
        for line in f:
            words = line.split()
            for i in words:
                for letter in i:
                    if(letter.isspace):
                        k=k+1
    return k

Write a python function that Capitalize the First Letter of Every Word in the File

def capitalize(fname):
    with open(fname, 'r') as f:
        for line in f:
            l=line.title()
            print(l)

Write a python function that prints the Contents of a File in Reverse Order

def reverse_content(filename):
    for line in reversed(list(open(filename))):
        print(line.rstrip())

Write a python Program to Flatten and print a List

a=[[1,[[2]],[[[3]]]],[[4],5]]
flatten=lambda l: sum(map(flatten,l),[]) if isinstance(l,list) else [l]
print(flatten(a))