Python Program to Find Set Length

Write a Python Program to Find the length of a set. In this example, we declared different types of sets and used the Python len function to find those sets’ lengths.

# Set Length

intSet = {10, 25, 136, 44, 150}
print("Set Items = ", intSet)

intSetLength = len(intSet)
print("Set Length = ", intSetLength)

strSet = {'apple', 'banana', 'Mango', 'kiwi'}
print("String Set Items = ", strSet)

strSetLength = len(strSet)
print("String Set Length = ", strSetLength)

mSet = {'Apple', 2.46, 45, 'Banana', (2, 4, 6, 8), 1}
print("Mixed Set Items = ", mSet)

mSetLength = len(mSet)
print("Mixed Set Length = ", mSetLength)
Set Items =  {150, 136, 25, 10, 44}
Set Length =  5
String Set Items =  {'kiwi', 'Mango', 'apple', 'banana'}
String Set Length =  4
Mixed Set 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 calculates the length.

# Set 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)
Python Program to Find Set Length 2