Earn 20 XP


Rows and Columns With Negative Indexes

Do you know how negative indexes are represented in Python? Remember from the Python tutorial? No? No worries, let's go through it again:

image.png

Using Negative Single Index

  • Using negative indexing on 1-D array
  • Pass the negative index of the element in a square bracket [ ].

image.png

  • Using negative indexing on 2-D array
  • Pass the negative index of the element in a square bracket [].
  • Try executing num_arr[-1][-2] in your notebook

image.png

Negative Index Intervals

  • Now you have an idea about how index intervals work on a NumPy array.
  • You also have an idea about how negative indexing works on Python lists.
  • See the snippet shown here. Can you think of how to get the output in yellow box using negative indexing? Try doing this on your own first.

image.png

  • The following code didn't give the correct solution. Can you figure out why?

image.png

  • Here's the correct solution. Phew!

image.png