Junior — Middle
Can you explain what a data structure array is and how it is used?
sobes.tech AI
Answer from AI
An array is a data structure that represents an ordered collection of elements of the same type, stored in a contiguous block of memory. In Python, the standard list type is often used as a dynamic array, allowing storage of elements of different types and resizing during runtime.
Arrays are used to store collections of data when fast access by index is important. For example, to access an element by position, it is enough to refer to it by its index, which occurs in constant time.
Example of using a list in Python:
arr = [10, 20, 30, 40]
print(arr[2]) # Outputs 30
arr.append(50) # Adds an element to the end
If you need more efficient work with arrays of numbers of the same type, you can use the array module or libraries like NumPy.