Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Find the Minimum and Maximum Values of an Array

Python Find the Minimum and Maximum Values of an Array

Document Object Reference Manual Python3 Examples

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

print

(

f

"The minimum value in the array is: {min_value}"

)

print

(

f

"The maximum value in the array is: {max_value}"

)

Code analysis:

  1. numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]: Define an array containing multiple integers.
  2. min_value = min(numbers): When using min() function to find the minimum value in the array and assign it to min_value。
  3. max_value = max(numbers): When using max() function to find the maximum value in the array and assign it to max_value。
  4. print(f"数组中的最小值是: {min_value}"): Output the minimum value in the array.
  5. 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.

Document Object Reference Manual Python3 Examples