Write a Python Program to Find the Largest or Maximum Set Item. Here, we use the Set max function to print the largest Set item.
# Set Max Item mxSet = {25, 67, 36, 98, 11, 32, 19, 80, 91} print("Set Items = ", mxSet) print("Largest Item in mxSet Set = ", max(mxSet))
Python largest set number output
Set Items = {32, 98, 67, 36, 11, 80, 19, 25, 91}
Largest Item in mxSet Set = 98
Python Program to Find Largest Item in a Set
This Python example allows entering the set items. Next, we used the Set sorted function (sorted(mxSet)) to sort the Set in ascending order. Next, we are printing the last index position item. We can use the index value because the sorted function returns a list.
# Set Max Item mxSet = 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)) mxSet.add(value) print("Set Items = ", mxSet) sortedVal = sorted(mxSet) print("Largest Item in mxSet Set = ", sortedVal[len(mxSet) - 1]) print("Data Type of sortedVal = ", type(sortedVal))
In this Python Program, we created a SetLargest function to Print the Largest Set Number. The If statement (if(setLargest < i)) checks whether the setLargest value is less than any of the set items. If True, assign that item as the largest set item.
# Set Max Item def SetLargest(mxSet, setLargest): for i in mxSet: if(setLargest < i): setLargest = i return setLargest mxSet = 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)) mxSet.add(value) setLargest = value print("Set Items = ", mxSet) lar = SetLargest(mxSet, setLargest) print("Largest Item in mxSet Set = ", lar)
Python maximum set item output
Enter the Total Set Items = 4
Enter the 1 Set Item = 99
Enter the 2 Set Item = 122
Enter the 3 Set Item = 33
Enter the 4 Set Item = 100
Set Items = {33, 122, 99, 100}
Largest Item in mxSet Set = 122