Skip to content

Array Multiplication Python

  • by

Array multiplication in Python can be done using various methods, depending on your requirements. Here, I’ll demonstrate different types of array multiplication using both basic Python lists and the NumPy library.

First, make sure you have NumPy installed. You can install it using pip if you don’t have it yet:

pip install numpy

Array Multiplication Python example

Here’s an example of array multiplication using NumPy in Python:

Element-wise multiplication:

import numpy as np

# Create two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# Perform element-wise multiplication
result = array1 * array2

print(result)

Matrix multiplication (Dot product):

import numpy as np

# Create two matrices
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# Perform matrix multiplication (dot product)
result = np.dot(matrix1, matrix2)

print(result)

Broadcasting multiplication:

import numpy as np

# Create an array and multiply it by a scalar
array = np.array([1, 2, 3])
scalar = 2

result = array * scalar

print(result)

Output:

Array Multiplication Python

Note: NumPy provides highly optimized functions for array operations, making it more efficient than using loops for array multiplication.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading