# Arrays in Python
# Arrays in Python
In Python, arrays are data structures that store elements of the same type in contiguous
memory locations. While Python has a built-in `list` type that can store heterogeneous elements,
true arrays (for homogeneous data) are available through the `array` module or third-party
libraries like NumPy.
Python lists are flexible and can hold different data types, but they're commonly used as arrays:
```python
# Creating a list
my_list = [1, 2, 3, 4, 5]
# Accessing elements
print(my_list[0]) # Output: 1
# Modifying elements
my_list[1] = 20
# Length of list
print(len(my_list)) # Output: 5
# Adding elements
my_list.append(6)
```
## 2. Array Module
```python
import array
```python
import numpy as np
# Multi-dimensional arrays
matrix = np.array([[1, 2, 3], [4, 5, 6]])
```
## Common Operations:
```python
arr = [10, 20, 30, 40, 50] # Using list as example
# Slicing
print(arr[1:4]) # Output: [20, 30, 40]
# Searching
print(30 in arr) # Output: True
# Concatenation
new_arr = arr + [60, 70]
# Length
print(len(arr)) # Output: 5
```
For most numerical work, NumPy arrays are recommended due to their performance and
functionality. For general-purpose use, Python lists are typically sufficient.