Friday, November 15, 2024
Google search engine
HomeLanguagesPython – max() function

Python – max() function

Python max() function returns the largest item in an iterable or the largest of two or more arguments.

It has two forms.

  • max() function with objects
  • max() function with iterable

max() function with objects

Unlike the max() function of C/C++, the max() function in Python can take any type of object and return the largest among them. In the case of strings, it returns the lexicographically largest value.

Syntax : max(arg1, arg2, *args[, key]) 

Parameters : 

  • arg1, arg2 : objects of the same datatype
  • *args : multiple objects
  • key : function where comparison of iterable is performed based on its return value

Returns : The maximum value 

Example of Python max() function

Example 1: Finding the maximum of 3 integer variables

Python3




var1 = 4
var2 = 8
var3 = 2
 
max_val = max(var1, var2, var3)
print(max_val)


Output : 

8

Example 2: Finding the maximum of 3 string variables

 By default, it will return the string with the maximum lexicographic value. 

Python3




var1 = "Lazyroar"
var2 = "for"
var3 = "geek"
 
max_val = max(var1, var2, var3)
print(max_val)


Output : 

Lazyroar

Example 3: Finding the maximum of 3 string variables according to the length

We will be passing a key function in the max() method. 

Python3




var1 = "Lazyroar"
var2 = "for"
var3 = "geek"
 
max_val = max(var1, var2, var3,
            key=len)
print(max_val)


Output : 

Lazyroar

Example 4: Python max() exception

If we pass parameters of different datatypes, then an exception will be raised.

Python3




integer = 5
string = "geek"
 
max_val = max(integer, string)
print(max_val)


Output : 

TypeError: '>' not supported between instances of 'str' and 'int'

Example 5: Python max() float

Python3




list = [1.2, 1.3, 0.1]
max_value = max(list)
print(max_value)


Output:

1.3

Example 6: Python max() index

Python3




# function to find minimum and maximum position in list
def maximum(a, n):
 
    # inbuilt function to find the position of maximum
    maxpos = a.index(max(a))
 
    # printing the position
    print ("The maximum is at position", maxpos + 1)
 
# driver code
a = [3, 4, 1, 3, 4, 5]
maximum(a, len(a))


Output:

The maximum is at position 6

max() function with iterable

When an iterable is passed to the max() function it returns the largest item of the iterable. 

Syntax : max(iterable, *iterables[, key, default]) 
Parameters : 

  • iterable : iterable object like list or string.
  • *iterables : multiple iterables
  • key : function where comparison of iterable is performed based on its return value
  • default : value if the iterable is empty

Returns : The maximum value. 

Example 1: Finding the lexicographically maximum character in a string

Python3




string = "Lazyroar"
 
max_val = max(string)
print(max_val)


Output : 

s

Example 2: Finding the lexicographically maximum string in a string list

Python3




string_list = ["Geeks", "for", "Geeks"]
 
max_val = max(string_list)
print(max_val)


Output : 

for

Example 3: Finding the longest string in a string list.

Python3




string_list = ["Geeks", "for", "Geek"]
 
max_val = max(string_list, key=len)
print(max_val)


Output : 

Geeks

Example 4: If the iterable is empty, the default value will be displayed

Python3




dictionary = {}
 
max_val = max(dictionary,
            default={1: "Geek"})
print(max_val)


Output : 

{1: 'Geek'}

RELATED ARTICLES

Most Popular

Recent Comments