Editor

Multiplication Table

Advanced · Write the code · from NumPy Broadcasting: Operate on Arrays of Different Shapes

From the lesson: NumPy Broadcasting: Operate on Arrays of Different Shapes

You've already seen that np.array([1,2,3]) 2 multiplies every element by 2. But have you wondered how* NumPy knows to stretch that single number 2 across the entire array? That mechanism is called broadcasting.

Read the lesson

Use broadcasting to create a 5x5 multiplication table for numbers 1 through 5. The result should be a 5x5 array where element [i, j] equals (i+1) * (j+1). Print it.

Hint: create a column vector and a row vector, then multiply them.

import numpy as np
# Create a 5x5 multiplication table

More NumPy Broadcasting exercises

All Python exercises · 50 practice problems · Data Science & Machine Learning quiz