Python Practice › NumPy Programs › Program 51
Multiply a row by a scalar
Write a NumPy program to multiply row 1 of an array by 2.
Input
[[6, 5], [10, 9], [7, 8]]
Output
[[12 10] [10 9] [ 7 8]]
Approach
- Index the row and use
*=.
Solution
import numpy as npa = np.array([[6, 5], [10, 9], [7, 8]])a[0] *= 2print(a)Output
[[12 10] [10 9] [ 7 8]]
Learn the concept
New to this topic? Read the lesson, then come back.