Python max() function
Python max() function
The max() function in Python returns the largest item in an iterable or the largest of two or more arguments.
Syntax
max(iterable, *[, key, default]) or max(arg1, arg2, *args[, key])
Parameter
arg1, arg2, *args: This parameter represents one or more items to compare
Or
iterable: It represents an iterable, with one or more items to compare
Return
This function returns the item with the highest value or the item with the highest value in an iterable.
Example 1
# Python program explaining # the max() function val1= 45 val2=10 max_num = max(val1, val2) print("The max number between",val1,"and ",val2," is ",max_num)
Output
The max number between 45 and 10 : 45
Example 2
# Python program explaining # the max() function #passing string value str1= "Apple" str2="Banana" str3="Orange" #For string values, an alphabetical comparison is done. max_num = max(str1, str2,str3) print("The max value between",str1,", ",str2,"and ",str3," is ",max_num)
Output
The max value between Apple , Banana and Orange is Orange