Home » Python

Find maximum value from given parameters using max() in Python

Python max() function with Example: In this article, we are going to learn with an example about max() function which is an in-built function in Python.
Submitted by IncludeHelp, on January 19, 2018

Python - max() function

max() is an in-built function in Python, which may take N number of arguments and returns maximum value of its arguments.

For example, if we provide 3 arguments with the values 20, 10 and 30. max() will return 30 as it is the maximum value among these 3 arguments.

Syntax:

max(arg1, arg2, arg3, ...)

Here, arg1, arg2 and arg3 are the arguments which may integer, string etc.

Example:

    Input arguments are: 20, 10, 30
    Output: 30
    Because 10 is the maximum argument here.

    Input arguments are: “ABC”, “PQR”, “Hello”
    Output: “PQR”
    Because “ABC” is the maximum argument according 
    to its length and alphabetically sequences.

Source code:

#Simple example to get max value
#from given arguments 

#this will print 30
print "max(20, 10, 30) : ", max(20, 10, 30)
#this will print PQR
print "max('ABC', 'PQR', 'HELLO') : ", max('ABC', 'PQR', 'HELLO')

Output

max(20, 10, 30) :  30
max('ABC', 'PQR', 'HELLO') :  PQR

Finding largest/maximum alphabet from given string

Yes! It can also be used to get the alphabet of maximum ASCII value from given string.

Syntax:

max(string)

In this case, string is an argument. Here, max() will return maximum/largest alphabet which has highest ASCII value.

Source code:

#program to get maximum/largest
#character from given string

#this will return 'o'
print "max('Hello') : ", max('Hello')
#this will return 'O'
print "max('HELLO') : ", max('HELLO')
#this will return 'o'
print "max('hello') : ", max('hello')

Output

max('Hello') :  o
max('HELLO') :  O
max('hello') :  o


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.