Introduction
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
11. Python Program to Extract Different Sections of a 2D NumPy Array
Problem Statement
Write a Python program to create a 5×5 NumPy array and extract the following using array slicing:
- First two rows
- Last three rows
- First three columns
- Last two columns
Python Solution
import numpy as np
# Create a 5×5 array
array = np.arange(1, 26).reshape(5, 5)
print("Original Array:")
print(array)
print("\nFirst Two Rows:")
print(array[:2, :])
print("\nLast Three Rows:")
print(array[2:, :])
print("\nFirst Three Columns:")
print(array[:, :3])
print("\nLast Two Columns:")
print(array[:, 3:])
Sample Output
Original Array:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]]
First Two Rows:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
Last Three Rows:
[[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]]
First Three Columns:
[[ 1 2 3]
[ 6 7 8]
[11 12 13]
[16 17 18]
[21 22 23]]
Last Two Columns:
[[ 4 5]
[ 9 10]
[14 15]
[19 20]
[24 25]]
Explanation
The program demonstrates how slicing can be used to extract specific rows and columns from a 2D NumPy array.
Concepts Covered
- Array Slicing
- Row Selection
- Column Selection
- 2D Arrays
12. Python Program to Extract the Center Portion of a Matrix
Problem Statement
Write a Python program to create a 5×5 matrix and extract the center 3×3 matrix using NumPy slicing.
Python Solution
import numpy as np
matrix = np.arange(1, 26).reshape(5, 5)
print("Original Matrix:")
print(matrix)
center = matrix[1:4, 1:4]
print("\nCenter 3×3 Matrix:")
print(center)
Sample Output
Center 3×3 Matrix:
[[ 7 8 9]
[12 13 14]
[17 18 19]]
Explanation
Rows 1 to 3 and columns 1 to 3 are selected to obtain the center portion of the matrix.
Concepts Covered
- Matrix Slicing
- Row and Column Selection
- NumPy Arrays
13. Python Program to Extract Alternate Rows and Columns Using Step Slicing
Problem Statement
Write a Python program to create a 6×6 NumPy array and display:
- Every alternate row
- Every alternate column
- Alternate rows and alternate columns together
Python Solution
import numpy as np
array = np.arange(1, 37).reshape(6, 6)
print("Original Array:")
print(array)
print("\nAlternate Rows:")
print(array[::2, :])
print("\nAlternate Columns:")
print(array[:, ::2])
print("\nAlternate Rows and Columns:")
print(array[::2, ::2])
Sample Output
Alternate Rows:
[[ 1 2 3 4 5 6]
[13 14 15 16 17 18]
[25 26 27 28 29 30]]
Alternate Columns:
[[ 1 3 5]
[ 7 9 11]
[13 15 17]
[19 21 23]
[25 27 29]
[31 33 35]]
Alternate Rows and Columns:
[[ 1 3 5]
[13 15 17]
[25 27 29]]
Explanation
The slicing step value (2) skips every second row or column.
Concepts Covered
- Step Slicing
- Alternate Rows
- Alternate Columns
- NumPy Indexing
14. Python Program to Reverse Rows and Columns Using Slicing
Problem Statement
Write a Python program to reverse:
- Entire array
- Row order
- Column order
using NumPy slicing.
Python Solution
import numpy as np
array = np.arange(1, 17).reshape(4, 4)
print("Original Array:")
print(array)
print("\nReverse All Rows:")
print(array[::-1])
print("\nReverse Columns:")
print(array[:, ::-1])
print("\nReverse Both Rows and Columns:")
print(array[::-1, ::-1])
Sample Output
Reverse All Rows:
[[13 14 15 16]
[ 9 10 11 12]
[ 5 6 7 8]
[ 1 2 3 4]]
Reverse Columns:
[[ 4 3 2 1]
[ 8 7 6 5]
[12 11 10 9]
[16 15 14 13]]
Reverse Both Rows and Columns:
[[16 15 14 13]
[12 11 10 9]
[ 8 7 6 5]
[ 4 3 2 1]]
Explanation
Negative step values (-1) reverse the order of rows or columns without modifying the original array.
Concepts Covered
- Reverse Slicing
- Negative Step
- Array Manipulation
15. Python Program to Extract the Border Elements of a Matrix
Problem Statement
Write a Python program to create a 5×5 NumPy matrix and extract:
- Top row
- Bottom row
- Left column
- Right column
using array slicing.
Python Solution
import numpy as np
matrix = np.arange(1, 26).reshape(5, 5)
print("Original Matrix:")
print(matrix)
print("\nTop Row:")
print(matrix[0, :])
print("\nBottom Row:")
print(matrix[-1, :])
print("\nLeft Column:")
print(matrix[:, 0])
print("\nRight Column:")
print(matrix[:, -1])
Sample Output
Top Row:
[1 2 3 4 5]
Bottom Row:
[21 22 23 24 25]
Left Column:
[ 1 6 11 16 21]
Right Column:
[ 5 10 15 20 25]
Explanation
This program uses slicing to extract the outer border elements of a matrix, a common task in image processing, matrix manipulation, and coding interviews.
Concepts Covered
- Array Slicing
- Border Extraction
- Matrix Operations
- NumPy Indexing
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
-1reverses 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.

