Python Slip 13

83d4d817b04a47848977c0f9bc30070f

Q1. Write a python program to accept n elements in a set and find the length of a set, maximum, minimum value and the sum of values in a set.

import math
intSet = set()
n=int(input("Enter the Number of Elements in the Set\t"))
for i in range(1, n+1):
    value = int(input("Enter the %d Set value = " %i))
    intSet.add(value)
print("Set Items = ", intSet)
length=0
maximum= 0
minimum= math.inf #Set the initial min to infinity
sumSet=0
for i in intSet:
    length+= 1
    sumSet+= i
    if maximum < i:
        maximum = i
    if minimum > i:
        minimum = i
print("Set Length = ", length)
print("Maximum in Set = ", maximum)
print("Minimum in Set = ", minimum)
print("Sum of Set Elements = ", sumSet)

OUTPUT

Enter the Number of Elements in the Set	6
Enter the 1 Set value = 2
Enter the 2 Set value = 3
Enter the 3 Set value = 1
Enter the 4 Set value = 5
Enter the 5 Set value = 4
Enter the 6 Set value = 6
Set Items =  {1, 2, 3, 4, 5, 6}
Set Length =  6
Maximum in Set =  6
Minimum in Set =  1
Sum of Set Elements =  21

Q2. Write a python program that accepts a sentence and calculate the number of letters and digits in it.

string = input("Input a string\t")
digit=letter=0
for c in string:
    if c.isdigit():
        digit=digit+1
    elif c.isalpha():
        letter=letter+1
    else:
        pass
print("Given String is :-", s)
print("Number of Letters :-", l)
print("Number of Digits :-", d)

OUTPUT

Input a string	Rahul Sonawane 7744075076 Nashik 422003
Given String is :- Rahul Sonawane 7744075076 Nashik 422003
Number of Letters :- 19
Number of Digits :- 16

Q2. Write a python program to create a class Circle and compute the area and the circumference of the Circle. (Use parameterized constructor).

import math
class circleClass():
    def __init__(self, radius):
        self.radius = radius
    def printArea(self):
        return math.pi*(self.radius**2)

    def printPerimeter(self):
        return 2*math.pi*self.radius

radius = float(input('Enter some random radius of the circle = '))
circleobj = circleClass(radius)
print("The Perimeter of circle with given radius",radius, '=', round(circleobj.printPerimeter(), 4))
print("The Area of circle with given radius", radius,'=', round(circleobj.printArea(), 4))

OUTPUT

Enter some random radius of the circle = 4
The Perimeter of circle with given radius 4.0 = 25.1327
The Area of circle with given radius 4.0 = 50.2655

0 Comments:

Post a Comment