Python Practice › NumPy Programs › Program 38
Dot product of two vectors
Write a NumPy program to calculate the dot product of two vectors.
Input
[5, 3] and [7, 4]
Output
47 47
Approach
np.dot(a, b)ora @ b.
Solution
import numpy as npa, b = np.array([5, 3]), np.array([7, 4])print(np.dot(a, b), a @ b)Output
47 47
Line by line
Line 3
5 × 7 + 3 × 4 = 47.
Note
The original sample answer (42) was not the dot product of these vectors.
Learn the concept
New to this topic? Read the lesson, then come back.