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
= set()
intSet =int(input("Enter the Number of Elements in the Set\t"))
nfor i in range(1, n+1):
= int(input("Enter the %d Set value = " %i))
value
intSet.add(value)print("Set Items = ", intSet)
=0
length= 0
maximum= math.inf #Set the initial min to infinity
minimum=0
sumSetfor i in intSet:
+= 1
length+= i
sumSetif maximum < i:
= i
maximum if minimum > i:
= i
minimum 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.
= input("Input a string\t")
string =letter=0
digitfor c in string:
if c.isdigit():
=digit+1
digitelif c.isalpha():
=letter+1
letterelse:
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
= float(input('Enter some random radius of the circle = '))
radius = circleClass(radius)
circleobj 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