Member-only story
Hi everyone, welcome back. Today, we will be going over the max() function in Python and see how it works. The max() function is a built in function and it will return the highest value within an iterable. With this introduction out of the way, let’s get into it.
Max() Function
Let’s see how the max() function works:
list = [1, 2, 3]
print(max(list))Output:
3
As we can see in the example above, we created a list with 3 values and printed the max value within the list. Our output contains a ‘3’ which is our highest value.
The max() function works with strings as well, which will return return the last item in alphabetical order. Let’s see an example:
list = ["a", "b", "c"]
print(max(list))Output:
c
Additional Examples
Max() function being used with a tuple:
tuple = (1, 2, 3)
print(max(tuple))Output:
3
Max() function being used with a set:
set = {1, 2, 3}
print(max(set))Output:
3
Max() function being used with a dictionary:
dictionary = {1 : "one", 2 : "two", 3: "three"}
print(max(dictionary))Output:
3
Conclusion
This is it for the max() function example. It’s a fairly straight forward function that returns the highest value within a given iterable object. I hope this helps. Thanks for reading.