find the range of all elements in an array in python

To find the range of all elements in an array in Python, we can use the min() and max() functions to find the minimum and maximum values in the array, respectively. Then, we can subtract the minimum from the maximum to get the range of the array.

Here's an example code snippet that demonstrates how to find the range of all elements in an array in Python:

main.py
def find_range(arr):
    min_value = min(arr)
    max_value = max(arr)
    return max_value - min_value

my_array = [2, 5, 8, 1, 4, 9, 3]
array_range = find_range(my_array)
print("The range of the array is:", array_range)
222 chars
9 lines

Output:

main.py
The range of the array is: 8
29 chars
2 lines

In this example, we define a function called find_range() that takes an array as input and returns the range of the array. We use the min() function to find the minimum value in the array, and the max() function to find the maximum value in the array. We then subtract the minimum from the maximum to get the range of the array. Finally, we define an array called my_array and call the find_range() function with my_array as input, and print the result.

related categories

gistlibby LogSnag