EN
Python - max() method example
0
points
The max()
function returns the highest number from given numbers.
x = max(2, 5)
print(x) # 5
print(max(1, 2)) # 2
print(max(1.5, 2.0)) # 2.0
1. Documentation
Syntax |
or
|
Parameters | x1 , x2 , xN - values to compare |
Result |
Maximal It returns |
Description | max is a method that takes number arguments and returns the biggest value. |
2. Getting max value from array examples
2.1. With max()
method
array = [1, 3, 7, 5]
print(max(array)) # 7
2.2. By comparing all the elements in the array
import math
def max_value(array):
result = -math.inf
for item in array:
if item > result:
result = item
return result
numbers = [2, 3, 8, 1, 5]
print(max_value(numbers)) # 8