The max function

The max function returns the maximum number from a group of numbers specified in the parameters. You can specify a group of numbers by listing them separated by commas, or a sequence: list, tuple, set.

Syntax

pythonmax(a group of numbers or a sequence)

Example

Let's derive the maximum number from a group of numbers:

pythonnum1 = 2
num2 = 5
num3 = 10
print(max(num1, num2, num3))

Result of code execution:

python10

Example

Now let's find the maximum number from the group of negative numbers:

pythonnum1 = -1
num2 = -3
num3 = -6
print(max(num1, num2, num3))

Result of code execution:

python-1

Example

The function also works with lists. Let's find the maximum number by passing a list to the function parameter max:

pythonlst = [10, 5, 4]
print(max(lst))

Result of code execution:

python10

Example

Let's use the function to find the maximum number in a tuple:

pythontlp = (-2, 6, 9)
print(max(tlp))

Result of code execution:

python9

See also

  • function min,
    which returns the minimum number
  • function abs,
    which returns the absolute value of a number