Array Operations

Python · NumPy Library

Theory

Arithmetic on arrays is vectorised: + - * / // % and ** work element by element without a loop. price * qty multiplies the first price by the first quantity, the second by the second, and so on, returning a new array of the same shape. An operation with a single number applies it to every element, so rev * 1.18 adds 18% GST to every bill.

Comparisons such as price > 300 also work element-wise and return a boolean array. The two arrays must have the same shape, or shapes that broadcasting can stretch to match; otherwise NumPy raises a ValueError. Division always returns floats, // gives the floor of the division and % the remainder. In-place forms like rev += 10 change the existing array instead of creating a new one.

Why we use it

Element-wise maths replaces loops with one readable line and runs in compiled code, so it is both shorter and much faster. It is how prices, scores and features are transformed in every data pipeline.

When to use it

Use whenever you transform numeric columns: totals, percentages, unit conversions, differences between two readings. If shapes differ, check broadcasting rules; for row or column summaries use aggregations such as sum(axis=...).

How it works

  1. Two arrays price and qty
  2. Same shape (or broadcastable)? Yes: Continue. No: ValueError: operands could not be broadcast.
  3. NumPy pairs element i of price with element i of qty
  4. Compiled loop applies the operator to every pair
  5. New array of results (or in-place with +=)

Syntax

a + b   a - b   a * b   a / b   a // b   a % b   a ** 2
a * 1.18   a > 300   a += 10

Example

price = np.array([250, 1200, 80])
qty = np.array([4, 1, 10])
print(price * qty)          # [1000 1200  800]

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