Python Practice › NumPy Programs › Program 83

Top-left 2 × 2 block

EasyNumPy Programs · 83 of 100

Write a NumPy program to extract the first and second elements of the first and second rows.

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

Approach

  1. a[:2, :2].

Solution

Python
import numpy as npa = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])print(a[:2, :2])
Output
[[ 6  5]
 [10  9]]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs