Python Arrays

In Python, arrays are a data structure that can hold multiple values at once. Unlike lists, arrays are specialized for numerical data and can only store elements of the same data type. Arrays are useful when you need to work with a collection of items, especially when numerical operations are involved. Python provides the array module to work with arrays.
Python arrays are a great way to store and manipulate homogeneous data efficiently. While lists are more versatile, arrays can be faster for numerical operations due to their fixed data type. Understanding arrays will help you optimize your Python programs when dealing with large amounts of numerical data.

Definition and Characteristics

Creating an Array

To create an array in Python, you need to import the array module:

Example


# Importing the array module
import array as arr
# Creating an array of integers
numbers = arr.array('i',[1, 2, 3, 4, 5]) #'i' indicates array of integers
print(numbers)
                

Output

array('i',[1, 2, 3, 4, 5])

Accessing Elements in an Array

You can access elements in an array using their index:

Example


# Accessing array elements
import array as arr
numbers = arr.array('i',[1, 2, 3, 4, 5])
print("First element:", numbers[0])
print("Last element:", numbers[-1])
                

Output

First element: 1 Last element: 5

Modifying Elements in an Array

Arrays are mutable, so you can change the value of an element:

Example


# Modifying an element
import array as arr
numbers = arr.array('i',[1, 2, 3, 4, 5])
numbers[2] = 10
print(numbers)
                

Output

array('i',[1, 2, 10, 4, 5])

Common Array Methods

Example: Using Array Methods


# Using array methods
import array as arr
numbers = arr.array('i',[1, 2, 10, 4, 5])
numbers.append(6)
numbers.insert(1, 15)
numbers.remove(10)
print(numbers)
                

Output

array('i',[1, 15, 2, 4, 5, 6])

Looping Through an Array

You can loop through an array using a for loop:


# Looping through an array
import array as arr
numbers = arr.array('i',[1, 15, 2, 4, 5, 6])
for num in numbers:
    print(num, end=' ')
                

Output

1 15 2 4 5 6

Using Numpy Arrays

For more advanced numerical computations, the numpy library offers the ndarray type, which is highly optimized for performance.

Let's see how the array is created by using numpy library.

Example:


import numpy as np
np_array = np.array([1, 2, 3, 4, 5])
print(np_array)  

Output

[1 2 3 4 5]

Matrix Operations Using Numpy

NumPy supports matrix operations directly:

1. Matrix Creation


import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
print("Matrix 1:")
print(matrix1) 
print("Matrix 2:")
print(matrix2)

Output

Matrix 1: [[1 2] [3 4]] Matrix 2: [[5 6] [7 8]]

2. Matrix Addition


import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
result = matrix1 + matrix2
print("Addition:")
print(result) 

Output

Addition: [[ 6 8] [10 12]]

3. Matrix Subtraction


import numpy as np    
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
result = matrix1 - matrix2
print("Subtraction:")
print(result) 

Output

Subtraction: [[-4 -4] [-4 -4]]

4. Matrix Transpose


import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
transpose = matrix1.T
print("Transpose of Matrix 1:")
print(transpose)

Output

Transpose of Matrix 1: [[1 3] [2 4]]

5. Matrix Inversion


import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
inverse = np.linalg.inv(matrix1)
print("Inverse of Matrix 1:")
print(inverse)  

Output

Inverse of Matrix 1: [[-2. 1. ] [ 1.5 -0.5]]