🔵 Data Science  ·  Lesson 36

NumPy Indexing, Slicing and Operations

NumPy Indexing, Slicing और 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.

NumPy Indexing और Slicing क्या है?

NumPy Indexing और Slicing ka matlab hai: Indexing and slicing select specific values, rows, columns or ranges from NumPy arrays. Simple words me, ye topic practical Python programs likhne me direct use hota hai.

Is topic ko sirf definition ke liye nahi, balki selecting array rows and columns jaise real examples ke liye practice karein.

यह क्यों सीखना जरूरी है?

  • Ye selecting array rows and columns me kaam aata hai.
  • Ye filtering numbers se bhi connected hai.
  • Isse aap code ka output aur errors better samajh paate hain.

Important Terms

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

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

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.

Practical Uses

  • 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. Program ko numpy-indexing.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. selecting array rows and columns par ek छोटा example banayen.
  4. Logic ko apne words me 5 lines me likhein.

सारांश

NumPy Indexing and Slicing ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.

← Back to Python Tutorial