How do you access the third element in an array in Python?
a) array[0]
b) array[2]
c) array[3]
d) array[1]
Answer:
b) array[2]
Explanation:
In Python, arrays (like lists) are zero-indexed, meaning the first element is accessed with index 0
. To access the third element, you would use index 2
.
from array import array
numbers = array('i', [10, 20, 30, 40])
third_element = numbers[2]
print(third_element) # Outputs 30
Accessing elements by their index is a fundamental operation in arrays, allowing you to retrieve, modify, or perform calculations on specific elements in the array.
Understanding how to work with array indices is crucial for efficiently managing and manipulating data stored in arrays.