Languages
[Edit]
EN

Python - max() method example

0 points
Created by:
Admin Dirask Community
1270

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
max(x1, x2, ... , xN)

or

max(iterable)
Parametersx1, x2, xN - values to compare
Result

Maximal number value.

It returns TypeError if at least one of the provided values is not a number.

Descriptionmax 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

References

  1. Maxima and minima - Wikipedia

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join