Array Indexing

Python · NumPy Library

Theory

Indexing picks elements by position, starting at 0. In a 1-D array, arr[0] is the first value and arr[-1] the last. Slicing uses start:stop:step and stops before stop, so arr[1:4] gives positions 1, 2 and 3, arr[:3] the first three and arr[::-1] the array reversed.

A 2-D array takes one index per axis separated by a comma: arr[row, col]. For a 3 x 4 table of store sales, sales[0, 2] is store 0 in quarter 2, sales[1] (or sales[1, :]) is the whole second row, sales[:, 0] is the first column and sales[1:, 1:3] is the block of rows 1-2 and columns 1-2. A slice keeps the dimension and a single integer drops it, so sales[:, 0] has shape (3,) while sales[:, 0:1] has shape (3, 1).

Why we use it

Analysis constantly needs part of an array: one row, one column, a time window or a block of a matrix. Indexing and slicing get those pieces in one short expression without loops.

When to use it

Use integer indexes and slices when you know the positions. To pick values by a condition use a boolean mask, and to pick a scattered list of positions use fancy indexing. For labelled rows and columns, pandas .loc is clearer.

How it works

  1. 2-D array sales (3 stores x 4 quarters)
  2. Pick rows first: an index, a slice or ':' for all
  3. Then pick columns after the comma
  4. Used a plain integer on an axis? Yes: That axis is dropped from the result. No: The axis is kept.
  5. Sub-array (a view of the original)

Syntax

arr[i]  arr[-1]  arr[start:stop:step]
arr2d[row, col]
arr2d[row_slice, col_slice]
arr2d[:, j]

Example

sales = np.array([[120, 135, 150], [90, 95, 110]])
print(sales[1, 2])    # 110
print(sales[:, 0])    # first column

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic