Python Find the Minimum and Maximum of an Array
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}")
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:
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]: Define an array containing multiple integers.min_value = min(numbers): Usemin()function to find the minimum value in the array and assign it tomin_value。max_value = max(numbers): Usemax()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 it works for any array containing comparable elements.
Other Extensions
Python3 Examples