Python Slip 16

29c983dabffe4188b25c8fd6dcbd336e

Q.1 Write a python program that prints out all the elements of the list that are less than 25.

list1 = [10, 50, 60, 80, 20, 15]

print("Element in this List are : ")
print(list1)
print("Element in this List less than 25 are : ")
for i in range(len(list1)):
    if list1[i] < 25:
        print(list1[i])

OUTPUT

Element in this List are : 
[10, 50, 60, 80, 20, 15]
Element in this List less than 25 are : 
10
20
15

Q2. Create a class named Circle constructed by a radius and two methods which will compute the area and the perimeter of a circle.

class Circle():
    def __init__(self, r):
        self.radius = r

    def area(self):
        return self.radius**2*3.14
    
    def perimeter(self):
        return 2*self.radius*3.14

NewCircle = Circle(8)
print(NewCircle.area())
print(NewCircle.perimeter())

OUTPUT

200.96
50.24

Q2. For given a .txt file that has a list of a bunch of names, count how many of each name there are in the file and print count.

text = open("test.txt", "r")
d = dict()
for line in text:
    line = line.strip()
    line = line.lower()
    words = line.split(" ")
    for word in words:
        if word in d:
            d[word] = d[word] + 1
        else:
            d[word] = 1
for key in list(d.keys()):
    print(key, ":", d[key])

OUTPUT

rahul : 3
sonawane : 1
november : 1
july : 1
may : 1
march : 1
jun : 1
tal : 1
nsk : 1
dist : 1
nashik : 1

0 Comments:

Post a Comment