Write a Python Program to Find the length of a set. In this example, we declared different types of sets and used the len function to find those sets’ lengths.
intSet = {10, 25, 136, 44, 150} print("Items = ", intSet) intSetLength = len(intSet) print("Set Length = ", intSetLength) strSet = {'apple', 'banana', 'Mango', 'kiwi'} print("String Items = ", strSet) strSetLength = len(strSet) print("String Length = ", strSetLength) mSet = {'Apple', 2.46, 45, 'Banana', (2, 4, 6, 8), 1} print("Mixed Items = ", mSet) mSetLength = len(mSet) print("Mixed Set Length = ", mSetLength)
Items = {150, 136, 25, 10, 44}
Set Length = 5
String Items = {'kiwi', 'Mango', 'apple', 'banana'}
String Length = 4
Mixed Items = {1, 2.46, 'Apple', (2, 4, 6, 8), 45, 'Banana'}
Mixed Set Length = 6
In this Python Program, we declared an empty set and allowed entering the set items and calculating the length.
intSet = set() number = int(input("Enter the Total Set Items = ")) for i in range(1, number + 1): value = int(input("Enter the %d Set value = " %i)) intSet.add(value) print("Set Items = ", intSet) intSetLength = len(intSet) print("Set Length = ", intSetLength)