-
Notifications
You must be signed in to change notification settings - Fork 2
Python Function MAX
max()
is a built-in function in Python 3. It returns the largest item in an iterable or the largest of two or more arguments.
This function takes two or more numbers or any kind of iterable as an argument. While giving an iterable as an argument we must make sure that all the elements in the iterable are of the same type. This means that we cannot pass a list which has both string and integer values stored in it.
Valid Arguments:
max(2, 3)
max([1, 2, 3])
max('a', 'b', 'c')
Invalid Arguments:
max(2, 'a')
max([1, 2, 3, 'a'])
max([])
The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments
is returned. If the iterable is empty and default is not provided, a ValueError
is raised.
print(max(2, 3)) # Returns 3 as 3 is the largest of the two values
print(max(2, 3, 23)) # Returns 23 as 23 is the largest of all the values
list1 = [1, 2, 4, 5, 54]
print(max(list1)) # Returns 54 as 54 is the largest value in the list
list2 = ['a', 'b', 'c' ]
print(max(list2)) # Returns 'c' as 'c' is the largest in the list in alphabetical order
list3 = [1, 2, 'abc', 'xyz']
print(max(list3)) # Gives TypeError as values in the list are of different type
#Fix the TypeError mentioned above first before moving on to next step
list4 = []
print(max(list4)) # Gives ValueError as the argument is empty
🚀 Run Code
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links