Python max() Function

Python max() Function

Description

The max() function returns the maximum value of its arguments, or the maximum value of an iterable (a list or tuple).

Syntax

Below is the syntax of the max() function −

max( x, y, z, .... )
max([x,y,z, . . ])

Parameters

  • x – This is a numeric expression.
  • y – This is also a numeric expression.

  • z – This is also a numeric expression.

Return Value

This method returns the maximum value of its arguments.

Example

The following example demonstrates the use of the max() method.

x = -45
y = 90
z = 55
largest = max(x,y,z)
print ("x: ",x,"y:",y, "z:",z, "max(x,y,z): ", largest)

x=[45, 90, 55]

largest = max(x)
print ("x: ",x,"max(x): ", largest)

x = 4+5j
y = 1+2j
z = 5-4j
largest = max(x,y,z)
print ("x: ",x,"y:",y, "z:",z, "max(x,y,z): ", largest)

When we run the above program, it produces the following output –

x: -45 y: 90 z: 55 max(x,y,z): 90
x: [45, 90, 55] max(x): 90
Traceback (most recent call last):
File "/home/cg/root/64a3d0ad4c0e6/main.py", line 12, in <module>
largest = max(x,y,z)
TypeError: '>' not supported between instances of 'complex' and 'complex'

Note that the “>” operator does not work with complex numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *