Python Practice › NumPy Programs › Program 51

Multiply a row by a scalar

EasyNumPy Programs · 51 of 100

Write a NumPy program to multiply row 1 of an array by 2.

Advertisement
Input
[[6, 5], [10, 9], [7, 8]]
Output
[[12 10]
 [10  9]
 [ 7  8]]

Approach

  1. Index the row and use *=.

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment