Max () function in Python
Python's built-in max () function returns the largest element in an iterable or the largest of the passed parameters.
If the values are strings, they will compare alphabetically.

Max () function syntax in Python
The max () function in Python has two forms:
max(iterable, *iterables[,key, default])
Or:
max(item1, item2, *item[, key])
Parameters of max function ()
The max () function works with two types of parameters corresponding to the two syntaxes mentioned above:
1. max(iterable, *iterables[, key, default])
iterable
: Required. The tuples, string, set, dictionary or iterator objects that you need to find the largest element in.*iterables
: Optional. The largest Iterable will be returned.key
: Optional. Key function, where the iterable goes through. Comparisons are made based on the results returned after passing the key function.default
: Optional. The default value when iterable is empty.
2. max(item1, item2, *item[, key])
item1
,item2
: Required. Object to compare, can be number, string .*item
: Optional. Other objects for comparison.
At least two objects are needed to make comparisons with the max () function.
key
: Optional. Key function, where items pass. The comparison is performed on the result returned after passing the key function.
Value returned from max ()
The max function returns different results corresponding to the two types as above:
1. max(iterable, *iterables[, key, default])
2. max(item1, item2, *item[, key])
Example 1: Find the largest element in the number passed
# su dung max(item1, item2, *item) print('So lon nhat la:', max(1, 3, 2, 5, 4)) # su dung max(iterable) num = [1, 3, 2, 8, 5, 10, 6] print('So lon nhat la:', max(num))
Run the program, the result is:
So lon nhat la: 5 So lon nhat la: 10
Example 2: Find the number that has the largest total digits using the key function
def sumDigit(num): sum = 0 while(num): sum += num % 10 num = int(num / 10) return sum # su dung max(item1, item2, *item, key) print('Ket qua lon nhat la:', max(100, 321, 267, 59, 40, key=sumDigit)) # su dung max(iterable, key) num = [15, 300, 2700, 821, 52, 10, 6] print('Ket qua lon nhat la:', max(num, key=sumDigit))
The return output is:
Ket qua lon nhat la: 267 Ket qua lon nhat la: 821
In this example, the parameters or each element in the iterable parameter are passed in turn to sumDigit()
to get the result that the number has the largest sum of digits.
Example 3: Find the list of the largest length using the key function
num = [15, 300, 2700, 821] num1 = [12, 2] num2 = [34, 567, 78] # su dung max(iterable, *iterables, key) print('List dai nhat la:', max(num, num1, num2, key=len))
Results returned:
List dai nhat la: [15, 300, 2700, 821]
In the above program, iterable num, num1 and num2 passed into the key function are built-in len () functions in Python. The result is iterable and the max function will show iterable with the largest length.
See also: Built-in Python functions
You should read it
May be interested
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
- The compile () function in Pythonthe compile () function returns a code object in python from the specified source. so how to use compile () function? please find out in this article.