NumPy Arrays

Python · NumPy Library

Theory

NumPy is the core library for numerical computing in Python, and its central object is the ndarray (n-dimensional array). Every element has the same data type (dtype) and the values sit side by side in one block of memory, which is why maths on arrays runs in fast compiled code. np.array([100, 250, 300]) turns a list into a 1-D array; a list of lists gives a 2-D array.

NumPy also builds arrays for you. np.arange(0, 10, 3) works like range and gives [0, 3, 6, 9] (the stop value is excluded). np.linspace(0, 100, 5) gives 5 evenly spaced values including both ends. np.zeros(3), np.ones((2, 3)) and np.full((2, 2), 7) fill an array of a given shape, and np.eye(3) is the identity matrix. Check any array with .shape, .ndim, .dtype and .size.

Why we use it

Python lists are flexible but slow for maths on large amounts of numbers. NumPy arrays store numbers compactly and run element-wise maths fast, and they are the shared format that pandas, scikit-learn and matplotlib use underneath.

When to use it

Use arrays for numeric data: measurements, matrices, model inputs and pixels. Use arange when you know the step, linspace when you know how many points you need. For labelled or mixed-type columns use a pandas DataFrame instead.

How it works

  1. import numpy as np
  2. Do you already have the values in a list? Yes: np.array(list). No: Build one with a creation function.
  3. Know the step size? Yes: np.arange(start, stop, step). No: np.linspace(start, stop, num).
  4. Need a blank array? np.zeros / np.ones / np.full(shape, value)
  5. Check .shape, .ndim and .dtype
  6. Array ready for vectorised maths

Syntax

import numpy as np
np.array([1, 2, 3])
np.arange(start, stop, step)
np.linspace(start, stop, num)
np.zeros(shape)  np.ones(shape)  np.full(shape, value)  np.eye(n)

Example

import numpy as np
sales = np.array([100, 250, 300])
grid = np.zeros((2, 3))
print(sales.shape, grid.shape)

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