Python Find the Minimum and Maximum of an Array

Document 对象参考手册Python3 Examples

In Python, we can use the built-inmin()andmax()function to easily find the minimum and maximum values in an array. Here is a simple example code:

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 the result
print(f"The minimum value in the array is: {min_value}")
print(f"The maximum value in the array is: {max_value}")

Code explanation:

  1. numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]: Define an array containing multiple integers.
  2. min_value = min(numbers): Usemin()function to find the minimum value in the array and assign it tomin_value。
  3. max_value = max(numbers): Usemax()function to find the maximum value in the array and assign it tomax_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 it works for any array containing comparable elements.

Document 对象参考手册Python3 Examples

Other Extensions