Write a Python Program to Find the Largest and Smallest Item in a Tuple. Here, we use the Tuple max and min functions to return the largest and smallest Tuple items.
# Tuple Largest and Smallest Item lgsmTuple = (78, 67, 44, 9, 34, 88, 11, 122, 23, 19) print("Tuple Items = ", lgsmTuple) print("Largest Item in lgsmTuple Tuple = ", max(lgsmTuple)) print("Smallest Item in lgsmTuple Tuple = ", min(lgsmTuple))
Python Program to Find Largest and Smallest Item in a Tuple
Here, we used the Tuple sorted function to sort the Tuple in ascending order. Next, we are printing the first (smallest) and last (largest) index position items.
# Tuple Largets and Smallest Item lgsmTuple = (15, 76, 34, 79, 89, 33, 14, 10, 65, 55) print("Tuple Items = ", lgsmTuple) lgsmTuple = sorted(lgsmTuple) print("Largest Item in lgsmTuple Tuple = ", lgsmTuple[len(lgsmTuple) - 1]) print("Smallest Item in lgsmTuple Tuple = ", lgsmTuple[0])
Tuple Items = (15, 76, 34, 79, 89, 33, 14, 10, 65, 55)
Largest Item in lgsmTuple Tuple = 89
Smallest Item in lgsmTuple Tuple = 10
In this Python example, we assigned the Tuple first value to the largest and smallest variables. The first if condition (if(tupLargest < lgsmTuple[I])) examines whether the current Tuple item is less than the tupLargest. If True, assign that Tuple value to the largest item and also save the index value. Similarly, the second if statement (if(tupSmallest > lgsmTuple[I])) checks the tuple item is greater than tupSmallest. If it is True, assign that Tuple value to the smallest and save the index position.
# Tuple Largets and Smallest Item lgsmTuple = (25, 17, 33, 89, 77, 10, 64, 11, 55) print("Tuple Items = ", lgsmTuple) tupLargest = lgsmTuple[0] tupSmallest = lgsmTuple[0] for i in range(len(lgsmTuple)): if(tupLargest < lgsmTuple[i]): tupLargest = lgsmTuple[i] tupLargestPos = i if(tupSmallest > lgsmTuple[i]): tupSmallest = lgsmTuple[i] tupSmallestPos = i print("Largest Item in lgsmTuple Tuple = ", tupLargest) print("Largest Tuple Item index Position = ", tupLargestPos) print("Smallest Item in lgsmTuple Tuple = ", tupSmallest) print("Smallest Tuple Item index Position = ", tupSmallestPos)
Tuple Items = (25, 17, 33, 89, 77, 10, 64, 11, 55)
Largest Item in lgsmTuple Tuple = 89
Largest Tuple Item index Position = 3
Smallest Item in lgsmTuple Tuple = 10
Smallest Tuple Item index Position = 5
In this Python Program, we created two different functions that return the Tuples Largest and Smallest Number.
# Tuple Max Item def tupleLargest(lgsmTuple): tupLargest = lgsmTuple[0] for i in lgsmTuple: if(tupLargest < i): tupLargest = i return tupLargest def tupleSmallest(lgsmTuple): tupSmallest = lgsmTuple[0] for i in lgsmTuple: if(tupSmallest > i): tupSmallest = i return tupSmallest lgsmTuple = (33, 56, 22, 18, 2, 76, 45, 95, 77, 15) print("Tuple Items = ", lgsmTuple) lar = tupleLargest(lgsmTuple) print("Largest Item in lgsmTuple Tuple = ", lar) smt = tupleSmallest(lgsmTuple) print("Smallest Item in lgsmTuple Tuple = ", smt)
Tuple Items = (33, 56, 22, 18, 2, 76, 45, 95, 77, 15)
Largest Item in lgsmTuple Tuple = 95
Smallest Item in lgsmTuple Tuple = 2