42 of 151 menu

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

max(a group of numbers or a sequence)

Example

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

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

Result of code execution:

10

Example

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

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

Result of code execution:

-1

Example

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

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

Result of code execution:

10

Example

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

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

Result of code execution:

9

See also

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