NumPy Array Slicing Practice Questions with Solutions

NumPy array slicing allows you to extract a specific portion of an array without modifying the original data. It is one of the most frequently used features in data analysis, machine learning, and scientific computing. In this chapter, you’ll practice the most important NumPy array slicing questions with complete solutions. NumPy Array Slicing practice questions with solutions help to understand the concepts.


1. Python Program to Slice the First Three Elements of a NumPy Array

Problem Statement

Write a Python program to print the first three elements of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[:3])

Sample Output

[10 20 30]

Explanation

The slice [:3] starts from index 0 and ends before index 3.

Concepts Covered

  • Array Slicing
  • Start Index
  • End Index

2. Python Program to Slice the Last Three Elements

Problem Statement

Write a Python program to print the last three elements of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[-3:])

Sample Output

[30 40 50]

Explanation

Negative slicing starts from the end of the array. -3: returns the last three elements.

Concepts Covered

  • Negative Slicing
  • Last Elements

3. Python Program to Slice Elements from Index 1 to Index 4

Problem Statement

Write a Python program to print elements from index 1 to index 4.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[1:4])

Sample Output

[20 30 40]

Explanation

The ending index is excluded from the output.

Concepts Covered

  • Range Slicing
  • Start and End Index

4. Python Program to Slice Every Second Element

Problem Statement

Write a Python program to print every second element of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40, 50, 60])

print(numbers[::2])

Sample Output

[10 30 50]

Explanation

The third value in slicing (2) represents the step size.

Concepts Covered

  • Step Slicing
  • Skip Elements

5. Python Program to Reverse a NumPy Array Using Slicing

Problem Statement

Write a Python program to reverse a NumPy array using slicing.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print(numbers[::-1])

Sample Output

[50 40 30 20 10]

Explanation

Using a step value of -1 reverses the array.

Concepts Covered

  • Reverse Array
  • Negative Step

6. Python Program to Slice the First Row of a 2D Array

Problem Statement

Write a Python program to print the first row of a two-dimensional NumPy array.

Python Solution

import numpy as np

numbers = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(numbers[0, :])

Sample Output

[1 2 3]

Explanation

The row index selects the row, while : selects all columns.

Concepts Covered

  • 2D Array Slicing
  • Row Selection

7. Python Program to Slice the Second Column of a 2D Array

Problem Statement

Write a Python program to print the second column of a two-dimensional NumPy array.

Python Solution

import numpy as np

numbers = np.array([
    [10, 20, 30],
    [40, 50, 60]
])

print(numbers[:, 1])

Sample Output

[20 50]

Explanation

The colon (:) selects all rows, and index 1 selects the second column.

Concepts Covered

  • Column Slicing
  • Two-Dimensional Arrays

8. Python Program to Slice a Subarray from a 2D Array

Problem Statement

Write a Python program to print the first two columns from all rows.

Python Solution

import numpy as np

numbers = np.array([
    [10, 20, 30],
    [40, 50, 60]
])

print(numbers[:, :2])

Sample Output

[[10 20]
 [40 50]]

Explanation

This slice selects all rows and the first two columns.

Concepts Covered

  • Subarray
  • Multiple Column Selection

9. Python Program to Slice the Last Two Elements

Problem Statement

Write a Python program to print the last two elements of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([5, 10, 15, 20, 25])

print(numbers[-2:])

Sample Output

[20 25]

Explanation

Using -2: returns the last two elements of the array.

Concepts Covered

  • Negative Slicing
  • Array Slicing

10. Python Program to Slice Every Third Element

Problem Statement

Write a Python program to print every third element of a NumPy array.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90])

print(numbers[::3])

Sample Output

[10 40 70]

Explanation

The step value 3 selects every third element from the array.

Concepts Covered

  • Step Slicing
  • Array Traversal

Chapter Summary

In this chapter, you learned how to extract specific portions of NumPy arrays using slicing. You practiced positive slicing, negative slicing, step slicing, reversing arrays, and slicing rows and columns in two-dimensional arrays.


Key Takeaways

  • NumPy slicing returns a portion of an array.
  • The ending index is not included in the result.
  • Negative indexes are useful for accessing elements from the end.
  • Step values allow skipping elements.
  • A step value of -1 reverses an array.
  • Slicing works with both one-dimensional and multi-dimensional arrays.
  • Row and column slicing are widely used in data analysis.

Frequently Asked Questions (FAQs)

1. What is array slicing in NumPy?

Array slicing is the process of extracting a portion of a NumPy array using start, end, and step indexes.


2. Does slicing modify the original NumPy array?

No. Slicing returns a view of the selected elements. The original array remains unchanged unless you modify the returned view.


3. How do I slice the first five elements of an array?

Use:

array[:5]

4. How do I reverse a NumPy array?

Use:

array[::-1]

5. What does the step value mean in slicing?

The step value determines how many positions to skip while selecting elements. For example, array[::2] selects every second element.


6. How do I slice a specific column in a 2D NumPy array?

Use:

array[:, column_index]

For example:

array[:, 1]

7. Is NumPy slicing important for Data Science?

Yes. NumPy slicing is widely used in Data Science, Machine Learning, Artificial Intelligence, and Data Analytics to efficiently access and manipulate datasets.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top