python/array_indexing.py

25 lines
970 B
Python

import numpy as np
array = np.array([9, 6, 4, 8, 10])
# Accessing the first element (positive index)
print(f'The first element (positive index): {array[0]}')
# Accessing the first element (negative index)
print(f'The first element (negative index): {array[-5]}')
# Accessing the last element (positive index)
print(f'The last element (positive index): {array[4]}')
# Accessing the last element (negative index)
print(f'The last element (negative index): {array[-1]}')
# Accessing the third element (positive index)
print(f'The third element (positive index): {array[2]}')
# Accessing the third element (negative index)
print(f'The third element (negative index): {array[-3]}')
array = np.array([9, 6, 4, 8, 10])
# Finding the average between the first and the last element
print((array[0] + array[-1]) / 2)
arr = np.array([8, 18, 9, 16, 7, 1, 3])
# Calculate an average of the first, fourth and last elements
average = (arr[0] + arr[3] + arr[-1]) / 3
print(average)