🔵 Data Science  ·  Lesson 36

NumPy Indexing, Slicing and Operations

What is NumPy Indexing and Slicing?

NumPy Indexing and Slicing means indexing and slicing select specific values, rows, columns or ranges from NumPy arrays.

In real programs, this topic helps in selecting array rows and columns. Learn the idea first, then type the program yourself and compare the output.

💡 At a Glance
PointDetails
Course AreaData Science
Tools and concepts used to analyse, clean and present data.
Main Useselecting array rows and columns
Example Filenumpy-indexing.py
Practice FocusRun, change values, and explain the output line by line.

Why should you learn this?

  • It is useful for selecting array rows and columns.
  • It connects with filtering numbers.
  • It improves your ability to read, write and debug Python programs.

Important Terms

These terms are used directly in this lesson. Understand them before memorising the code.

TermMeaning
indexLabels used to identify rows.
sliceslice is an important term in this topic.
2D array2D array is an important term in this topic.
rowHorizontal record in a table or array.
columnVertical field in a table or array.

Syntax / Basic Pattern

The simple pattern is: prepare data, apply the concept, then show the result.

Basic Pattern
import numpy as np
data = np.array([[10, 20, 30], [40, 50, 60]])
print("First row:", data[0])
print("Second column:", data[:, 1])
print("Value:", data[1, 2])

Complete Example Program

Python – numpy-indexing.py
import numpy as np

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

print("First row:", data[0])
print("Second column:", data[:, 1])
print("Value:", data[1, 2])

Expected Output

First row: [10 20 30] Second column: [20 50] Value: 60

Program Explanation

  • import numpy as np imports ready-made features from a module/library.
  • data = np.array([[10, 20, 30], [40, 50, 60]]) stores a value in data.
  • print("First row:", data[0]) displays information or calculated result on the screen.
  • print("Second column:", data[:, 1]) displays information or calculated result on the screen.
  • print("Value:", data[1, 2]) displays information or calculated result on the screen.

Where will you use it?

  • Selecting array rows and columns.
  • Filtering numbers.
  • Working with image pixels.

Common Mistakes

  • Analysing data before checking missing values, duplicates and data types.
  • Changing original data without keeping a clean copy.
  • Creating charts without title, labels or explanation.

Practice Tasks

  1. Type the program in numpy-indexing.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to selecting array rows and columns.
  4. Write 5 lines explaining the logic in your own words.

Summary

NumPy Indexing and Slicing is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.

← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.