Example of Using the max() Function in Python
In the following example, we use the built-in max() function to find the maximum value.
Example (Python 3.0+)
# Basic usage
print(max(1, 2)) # Outputs: 2
print(max('a', 'b')) # Outputs: 'b'
# You can also use max() on lists and tuples
print(max([1, 2])) # Outputs: 2
print(max((1, 2))) # Outputs: 2
# More examples
print("The maximum of 80, 100, 1000 is: ", max(80, 100, 1000))
print("The maximum of -20, 100, 400 is: ", max(-20, 100, 400))
print("The maximum of -80, -20, -10 is: ", max(-80, -20, -10))
print("The maximum of 0, 100, -400 is:", max(0, 100, -400))Output:
2 b 2 2 The maximum of 80, 100, 1000 is: 1000 The maximum of -20, 100, 400 is: 400 The maximum of -80, -20, -10 is: -10 The maximum of 0, 100, -400 is: 100
Introduction to the max() Function:
The max() function in Python returns the largest item from the given arguments. It works with numbers, strings, lists, tuples, and more.
For instance:
max(1, 2)returns 2.max('a', 'b')returns 'b' because 'b' has a higher ASCII value than 'a'.