Python Program to Find Smallest Set Item

Write a Python Program to Find the Smallest or Maximum Set Item. Here, we use the Set max function to print the Smallest Set item. 

# Set Min Item

smtSet = {98, 11, 44, 32, 7, 19, 65, 80, 91}
print("Set Items = ", smtSet)

print("Smallest Item in mxSet Set = ", min(smtSet))

Python set smallest number output

Set Items =  {32, 65, 98, 7, 11, 44, 80, 19, 91}
Smallest Item in mxSet Set =  7

Python Program to Find Smallest Item in a Set

This Python example allows entering the set items. Next, we used the Set sorted function (sorted(smtSet)) to sort the Set in ascending order. Next, we are printing the item at the first index position.

# Set Min Item

smtSet = set()

number = int(input("Enter the Total Set Items = "))
for i in range(1, number + 1):
    value = int(input("Enter the %d Set Item = " %i))
    smtSet.add(value)

print("Set Items = ", smtSet)

sortVals = sorted(smtSet)
print("Smallest Item in smtSet Set = ", sortVals[0])
print("Data Type of sortVals = ", type(sortVals))
Python Program to Find Smallest Set Item 2

Python Program to Print the Smallest Set Number using Functions

The If statement (if setSmallest > i) checks the setSmallest value is greater than any of the set items. If True, assign that item to the Smallest set item.

# Set Min Item

def SetSmallest(smtSet, setSmallest):
    for i in smtSet:
        if(setSmallest > i):
            setSmallest = i
    return setSmallest

smtSet = set()

number = int(input("Enter the Total Set Items = "))
for i in range(1, number + 1):
    value = int(input("Enter the %d Set Item = " %i))
    smtSet.add(value)

setSmallest = value
print("Set Items = ", smtSet)

lar = SetSmallest(smtSet, setSmallest)
print("Smallest Item in smtSet Set = ", lar)

Python set minimum item output

Enter the Total Set Items = 5
Enter the 1 Set Item = 11
Enter the 2 Set Item = 65
Enter the 3 Set Item = 56
Enter the 4 Set Item = 2
Enter the 5 Set Item = 88
Set Items =  {65, 2, 11, 56, 88}
Smallest Item in smtSet Set =  2