Skip main navigation

Hands-on: Array slicing

Explore the slicing syntax of NumPy arrays
© CSC - IT Center for Science Ltd.

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 4×4 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 2×2 subarray.

Next, create a 8×8 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?

© CSC - IT Center for Science Ltd.
This article is from the free online

Python in High Performance Computing

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now