Python Find the Minimum and Maximum Values of an Array
In Python, we can use the built-in min() and max() functions to easily find the minimum and maximum values in an array. Below is a simple example:
Example
Define an array
numbers
=
[
3
,
1
,
4
,
1
,
5
,
9
,
2
,
6
,
5
,
3
,
5
]
Use the min() function to find the minimum value
min_value
=
min
(
numbers
)
Use the max() function to find the maximum value
max_value
=
max
(
numbers
)
Output result
(
f
"The minimum value in the array is: {min_value}"
)
(
f
"The maximum value in the array is: {max_value}"
)
Code analysis:
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]: Define an array containing multiple integers.min_value = min(numbers): When usingmin()function to find the minimum value in the array and assign it tomin_value。max_value = max(numbers): When usingmax()function to find the maximum value in the array and assign it tomax_value。print(f"数组中的最小值是: {min_value}"): Output the minimum value in the array.print(f"数组中的最大值是: {max_value}"): Output the maximum value in the array.
Output result:
数组中的最小值是: 1
数组中的最大值是: 9
This code can easily find the minimum and maximum values in an array, and works for any array containing comparable elements.
Python3 Examples