Python max() Function



Python max() function returns the largest of its arguments or largest number from the iterable (list or tuple)

Syntax

Following is the syntax for 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 largest of its arguments.

Example

The following example shows the usage 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 doesn't work with complex numbers

python_built_in_functions.htm
Advertisements