Let’s define a maximum
function that determines and returns the largest of three values—the following session calls the function three times with integers, floating-point numbers and strings, respectively.
In [1]: def maximum(value1, value2, value3):
...: """Return the maximum of three values."""
...: max_value = value1
...: if value2 > max_value:
...: max_value = value2
...: if value3 > max_value:
...: max_value = value3
...: return max_value
...:
In [2]: maximum(12, 27, 36)
Out[2]: 36
In [3]: maximum(12.3, 45.6, 9.7)
Out[3]: 45.6
In [4]: maximum('yellow', 'red', 'orange')
Out[4]: 'yellow'
We did not place blank lines above and below the if
statements, because pressing return on a blank line in interactive mode completes the function’s definition.
You also may call maximum
with mixed types, such as int
s and float
s:
In [5]: maximum(13.5, -3, 7)
Out[5]: 13.5
The call maximum(13.5,
'hello',
7)
results in TypeError
because strings and numbers cannot be compared to one another with the greater-than (>
) operator.
maximum
’s DefinitionFunction maximum
specifies three parameters in a comma-separated list. Snippet [2]
’s arguments 12
, 27
and 36
are assigned to the parameters value1
, value2
and value3
, respectively.
To determine the largest value, we process one value at a time:
Initially, we assume that value1
contains the largest value, so we assign it to the local variable max_value
. Of course, it’s possible that value2
or value3
contains the actual largest value, so we still must compare each of these with max_value
.
The first if
statement then tests value2
>
max_value
, and if this condition is True
assigns value2
to max_value
.
The second if
statement then tests value3
>
max_value
, and if this condition is True
assigns value3
to max_value
.
Now, max_value
contains the largest value, so we return it. When control returns to the caller, the parameters value1
, value2
and value3
and the variable max_value
in the function’s block—which are all local variables—no longer exist.
max
and min
FunctionsFor many common tasks, the capabilities you need already exist in Python. For example, built-in max
and min
functions know how to determine the largest and smallest of their two or more arguments, respectively:
In [6]: max('yellow', 'red', 'orange', 'blue', 'green')
Out[6]: 'yellow'
In [7]: min(15, 9, 27, 14)
Out[7]: 9
Each of these functions also can receive an iterable argument, such as a list or a string. Using built-in functions or functions from the Python Standard Library’s modules rather than writing your own can reduce development time and increase program reliability, portability and performance. For a list of Python’s built-in functions and modules, see
https:/ / docs.python.org/ 3/ library/ index.html
(Fill-In) A function with multiple parameters specifies them in a(n) .
Answer: comma-separated list.
(True/False) When defining a function in IPython interactive mode, pressing Enter on a blank line causes IPython to display another continuation prompt so you can continue defining the function’s block.
Answer: False. When defining a function in IPython interactive mode, pressing Enter on a blank line terminates the function definition.
(IPython Session) Call function max
with the list [14,
27,
5,
3]
as an argument, then call function min
with the string 'orange'
as an argument.
Answer:
In [1]: max([14, 27, 5, 3])
Out[1]: 27
In [2]: min('orange')
Out[2]: 'a'