Hands-on: Array slicing

In this exercise we explore the slicing syntax of NumPy arrays.

Source code for this exercise is located in numpy/array-slicing/

First, create a 4x4 array with arbitrary values, then

  1. Extract every element from the second row.
  2. Extract every element from the third column.
  3. Assign a value of 0.21 to upper left 2x2 subarray.

Next, create a 8x8 array with chequerboard pattern, i.e. alternating zeros and ones:

1 0 1 ...
0 1 0 ...
1 0 1 ...
 ...

How many statements you do need for creating the pattern?

Share this article:

This article is from the free online course:

Python in High Performance Computing

Partnership for Advanced Computing in Europe (PRACE)