This article will explain how to find the maximum and minimum values from a tuple in python using max()
and min()
functions.
max()
function accepts an iterable as argument and returns the highest value in the iterable. An iterable is a sequence that can be iterated or loop over such as a list, tuple or a string. If the tuple is of numbers, then it returns the number with highest value. If the tuple is of strings, then it returns the string whose characters appear last in the alphabetic order.
Example,
numbers = (1, 2, 3, 4, 5) print('Largest number is:',max(numbers)) strings = ('axe', 'zebra', 'orange', 'brown') print('Highest string is:',max(strings))
This prints
Largest number is: 5
Highest string is: zebra
Similar to
max()
, min()
function also accepts an iterable and returns the item with minimum value in the iterable.For a sequence of numbers, the returned value is the minimum number while for sequence of strings, the returned value is a string that appears first in alphabetic order.
Example,
numbers = (1, 2, 3, 4, 5) print('Largest number is:',min(numbers)) strings = ('axe', 'zebra', 'orange', 'brown') print('Highest string is:',min(strings))
Output is
Largest number is: 1
Highest string is: axe
Hope the article was useful.
Also check: Find maximum item in a list in python