How do you declare an array in Python?
In Python, you can represent arrays using lists. You declare one by using square brackets, e.g., `my_array = [1, 2, 3, 4]`. Alternatively, you can use the `array` module for more array-specific operations: `import array; my_array = array.array('i', [1, 2, 3, 4])`.
How do you access elements in a Python array?
You can access elements in a Python array (using a list, since Python doesn't have a built-in array type) by using square brackets and the index of the element. For example, `arr[index]` retrieves the element at the specified `index`, where indexing starts at 0 for the first element.
How do you add or remove elements from a Python array?
To add elements to a Python array, use the `append()` method for single elements or `extend()` for multiple elements. To remove elements, use the `remove()` method to delete by value or `pop()` to remove by index. Note that Python lists are more commonly used for dynamic arrays.
What are the differences between Python arrays and lists?
Python arrays are fixed-type, requiring all elements to be of the same data type, and are provided by the 'array' module, offering efficient numerical operations. Lists, on the other hand, are flexible with heterogeneous data types, part of Python's built-in data types, and are generally more versatile but less efficient for numerical processing.
What are the limitations of using arrays in Python?
Arrays in Python are limited in that they require all elements to be of the same data type, lack built-in methods for dynamic resizing, and have less flexibility compared to lists. They are not a native feature of Python, requiring the 'array' module or external libraries like NumPy for use.