Python max() Function with Examples

In the previous article, we have discussed Python len() Function with Examples
max() Function in Python:

The max() function returns the item with the highest value or the highest value in an iterable.

If the values are strings, they are compared alphabetically.

Syntax:

max(iterable)

or

max(n1, n2, n3,........)

Parameters

iterable: An iterable that compares one or more items.

n1, n2, n3,……..: a single or multiple items to compare.

Return Value:

max() returns the iterable’s largest element.

Examples:

Example1:

Input:

Given List = [1, 2, 5, 6, 15, 7]

Output:

The greatest number in the given list = 15

Example2:

Input:

Given List = ["hello", "this", "is", "btechgeeks"]

Output:

The largest string in the given list (alphabetically ordered) is :
this

Note:

If the items in an iterable are strings, the largest (alphabetically ordered) 
item is returned.

max() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)For Number List

Approach:

  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the max() function to get the greatest or the largest element in the given list.
  • Store it in another variable.
  • Print the greatest element(number) in the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = [1, 2, 5, 6, 15, 7]
# Pass the given list as an argument to the max() function to get the greatest
# or the largest element in the given list.
# Store it in another variable.
greatst_numb = max(gvn_lst)
# Print the greatest element(number) in the given list.
print("The greatest number in the given list =", greatst_numb)

Output:

The greatest number in the given list = 15
2)For String List

Similarly, do the same to get the largest string in the given list.

Note: If the items in an iterable are strings, the largest (alphabetically ordered) item is returned.

Approach:

  • Give the list (string list)as static input and store it in a variable.
  • Pass the given list as an argument to the max() function to get the largest string in the given list. If the items in an iterable are strings, the largest (alphabetically ordered) item is returned.
  • Store it in another variable.
  • Print the largest string in the given list (alphabetically ordered).
  • The Exit of the program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_strlst = ["hello", "this", "is", "btechgeeks"]
# Pass the given list as an argument to the max() function to get the largest
# string in the given list.If the items in an iterable are strings,
# the largest (alphabetically ordered) item is returned.
# Store it in another variable.
largst_str = max(gvn_strlst)
# Print the largest string in the given list (alphabetically ordered).
print("The largest string in the given list (alphabetically ordered) is :")
print(largst_str)

Output:

The largest string in the given list (alphabetically ordered) is :
this
3)Passing multiple arguments

To get the greatest number from the given numbers.

# Give some random numbers as arguments to the max() function.
largst_numb = max(5, 10, -2, 9, 6)
# Print the greatest number from the given numbers
print("The greatest number from the given numbers = ", largst_numb)

Output:

The greatest number from the given numbers =  10

Method #2: Using Built-in Functions (User Input)

1)For Number List

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the max() function to get the greatest or the largest element in the given list.
  • Store it in another variable.
  • Print the greatest element(number) in the given list.
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the max() function to get the greatest
# or the largest element in the given list.
# Store it in another variable.
greatst_numb = max(gvn_lst)
# Print the greatest element(number) in the given list.
print("The greatest number in the given list =", greatst_numb)

Output:

Enter some random List Elements separated by spaces = 34 50 68 12 100
The greatest number in the given list = 100
2)For String List

Approach:

  • Give the list (string list) as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the max() function to get the largest string in the given list. If the items in an iterable are strings, the largest (alphabetically ordered) item is returned.
  • Store it in another variable.
  • Print the largest string in the given list (alphabetically ordered).
  • The Exit of the program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_strlst = list(input(
   'Enter some random List Elements separated by spaces = ').split())
# Pass the given list as an argument to the max() function to get the largest
# string in the given list.If the items in an iterable are strings,
# the largest (alphabetically ordered) item is returned.
# Store it in another variable.
largst_str = max(gvn_strlst)
# Print the largest string in the given list (alphabetically ordered).
print("The largest string in the given list (alphabetically ordered) is :")
print(largst_str)

Output:

Enter some random List Elements separated by spaces = good morning btechgeeks
The largest string in the given list (alphabetically ordered) is :
morning

Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.