max() python
时间: 2023-11-14 09:04:35 浏览: 54
The max() function in Python is used to return the largest element in an iterable or the largest of two or more arguments.
Syntax:
max(iterable, *iterables[, key, default])
Parameters:
- iterable: an iterable such as a list, tuple, set, or dictionary
- *iterables (optional): additional iterables to compare
- key (optional): a function to customize the sort order
- default (optional): a default value to return if the iterable is empty
Return value:
The max() function returns the largest element in the iterable or the largest of two or more arguments.
Example:
```
numbers = [3, 5, 1, 9, 2]
print(max(numbers)) # Output: 9
words = ['apple', 'banana', 'cherry', 'durian']
print(max(words)) # Output: 'durian'
print(max(10, 20, 30)) # Output: 30
```
In the first example, max() returns the largest number in the list 'numbers', which is 9. In the second example, max() returns the largest word in the list 'words', which is 'durian'. In the third example, max() returns the largest of the three arguments, which is 30.
阅读全文