Numpy quick example

amitmund June 03, 2026

Quick example of numpy

NumPy Array vs Python List

Feature Python List NumPy Array
Purpose General-purpose container Numerical computing
Speed Slower for large data Much faster
Memory Usage Higher Lower
Data Types Can store mixed types Usually stores same type
Mathematical Operations Limited Built-in vectorized operations
Multidimensional Support Nested lists required Native support
Scientific Computing Not ideal Designed for it

1. Creating Data

Python List

numbers = [1, 2, 3, 4, 5]

NumPy Array

import numpy as np

numbers = np.array([1, 2, 3, 4, 5])

2. Mathematical Operations

Python List

Adding two lists concatenates them:

a = [1, 2, 3]
b = [4, 5, 6]

print(a + b)

Output

[1, 2, 3, 4, 5, 6]

NumPy Array

Adding arrays performs element-wise addition:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

print(a + b)

Output

[5 7 9]

3. Multiplication

Python List

a = [1, 2, 3]

print(a * 3)

Output

[1, 2, 3, 1, 2, 3, 1, 2, 3]

NumPy Array

import numpy as np

a = np.array([1, 2, 3])

print(a * 3)

Output

[3 6 9]

4. Memory Usage

Python List NumPy Array
Stores references to Python objects Stores raw data in contiguous memory
Higher memory consumption Lower memory consumption
Slower access for large datasets Faster access for large datasets

5. Speed Comparison

Python List

numbers = [i for i in range(1000000)]

squared = [x * x for x in numbers]

NumPy Array

import numpy as np

numbers = np.arange(1000000)

squared = numbers ** 2
Operation Python List NumPy Array
Large-scale calculations Slower Faster
Vectorized operations No Yes
Performance Good for small data Excellent for large data

6. Multidimensional Data

Python List

matrix = [
    [1, 2, 3],
    [4, 5, 6]
]

NumPy Array

import numpy as np

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])
Feature Python List NumPy Array
2D Arrays Nested lists Native support
Matrix Operations Difficult Easy
Linear Algebra Not built-in Built-in

7. Useful NumPy Operations

import numpy as np

arr = np.array([10, 20, 30, 40])

print(arr.mean())
print(arr.sum())
print(arr.max())
print(arr.min())

Output

25.0
100
40
10

When to Use Which?

Use Python Lists When

  • Small datasets
  • Mixed data types
  • General programming
  • Frequent insertion and deletion

Use NumPy Arrays When

  • Numerical computing
  • Data analysis
  • Machine learning
  • Scientific computing
  • Large datasets
  • Matrix operations

Quick Summary

Choose If You Need
Python List General-purpose data storage
NumPy Array Fast numerical computation

Rule of Thumb: If you're doing math on lots of numbers,



NumPy vs Python List — Practical Examples Guide

Learn when and why NumPy is better than Python Lists through real-world examples.


Example 1: Student Marks (Beginner)

Using Python List

marks = [78, 85, 90, 67, 88]

total = sum(marks)
average = total / len(marks)

print("Total:", total)
print("Average:", average)

Output

Total: 408
Average: 81.6

Using NumPy

import numpy as np

marks = np.array([78, 85, 90, 67, 88])

print("Total:", marks.sum())
print("Average:", marks.mean())

Output

Total: 408
Average: 81.6

Why NumPy?

  • Built-in mathematical functions
  • Less code
  • Faster calculations

Example 2: Increase Employee Salary by 10%

Using Python List

salaries = [20000, 25000, 30000, 40000]

new_salary = []

for salary in salaries:
    new_salary.append(salary * 1.10)

print(new_salary)

Output

[22000.0, 27500.0, 33000.0, 44000.0]

Using NumPy

import numpy as np

salaries = np.array([20000, 25000, 30000, 40000])

new_salary = salaries * 1.10

print(new_salary)

Output

[22000. 27500. 33000. 44000.]

Why NumPy?

Vectorized operations eliminate loops.


Example 3: Temperature Conversion (Intermediate)

Convert Celsius to Fahrenheit.

Formula:

F = (C × 9/5) + 32

Using Python List

celsius = [10, 20, 30, 40]

fahrenheit = []

for temp in celsius:
    fahrenheit.append((temp * 9/5) + 32)

print(fahrenheit)

Using NumPy

import numpy as np

celsius = np.array([10, 20, 30, 40])

fahrenheit = (celsius * 9/5) + 32

print(fahrenheit)

Output

[50. 68. 86. 104.]

Example 4: E-Commerce Sales Analysis (Intermediate)

Monthly sales data:

sales = [10000, 12000, 9000, 15000, 17000]

Using NumPy

import numpy as np

sales = np.array([10000, 12000, 9000, 15000, 17000])

print("Total Sales:", sales.sum())
print("Average Sales:", sales.mean())
print("Highest Sales:", sales.max())
print("Lowest Sales:", sales.min())

Output

Total Sales: 63000
Average Sales: 12600
Highest Sales: 17000
Lowest Sales: 9000

Real World Use

  • Business Intelligence
  • Sales Reports
  • Financial Analytics

Example 5: Attendance System (Intermediate)

Find students present today.

import numpy as np

students = np.array([
    "Ram",
    "Sita",
    "John",
    "Amit"
])

present = np.array([
    True,
    False,
    True,
    True
])

print(students[present])

Output

['Ram' 'John' 'Amit']

Concept Used

Boolean Indexing


Example 6: Image Processing (Advanced)

A grayscale image is stored as pixel values.

import numpy as np

image = np.array([
    [100, 150],
    [200, 250]
])

bright_image = image + 50

print(bright_image)

Output

[[150 200]
 [250 300]]

Real World Use

  • Photoshop
  • OpenCV
  • Medical Imaging

Example 7: Machine Learning Dataset (Advanced)

Student dataset:

import numpy as np

data = np.array([
    [80, 70, 90],
    [75, 85, 88],
    [90, 95, 92]
])

print("Average Score:")
print(data.mean(axis=0))

Output

[81.67 83.33 90.00]

Use Case

Feature Engineering

Machine Learning Models


Example 8: Matrix Multiplication (Advanced)

Using NumPy

import numpy as np

A = np.array([
    [1, 2],
    [3, 4]
])

B = np.array([
    [5, 6],
    [7, 8]
])

result = np.dot(A, B)

print(result)

Output

[[19 22]
 [43 50]]

Real World Use

  • AI
  • Deep Learning
  • Computer Graphics

Example 9: Fast Data Filtering (Advanced)

Find products above ₹50,000.

import numpy as np

prices = np.array([
    25000,
    45000,
    60000,
    80000,
    35000
])

expensive = prices[prices > 50000]

print(expensive)

Output

[60000 80000]

Concept

Boolean Filtering


Example 10: Stock Market Analysis (Advanced)

import numpy as np

stock_prices = np.array([
    100,
    105,
    103,
    110,
    115
])

daily_change = np.diff(stock_prices)

print(daily_change)

Output

[5 -2 7 5]

Real World Use

  • Trading Systems
  • Financial Analysis
  • Quantitative Research

Why Companies Use NumPy

Industry Usage
Banking Risk Analysis
Healthcare Medical Imaging
AI Deep Learning
Finance Stock Prediction
E-Commerce Sales Analytics
Research Scientific Computing

Interview Questions

Q1. Why is NumPy faster than Python Lists?

Answer:

  • Written in C
  • Contiguous memory allocation
  • Vectorized operations
  • Less Python overhead

Q2. When should you use NumPy instead of Lists?

Use NumPy when:

  • Performing mathematical operations
  • Working with large datasets
  • Doing Data Science or Machine Learning
  • Handling matrices and vectors

Summary

Task Python List NumPy
Store Mixed Data
Numerical Computing
Matrix Operations
Speed Slow Fast
Memory Efficiency Low High
Machine Learning

Rule: Lists are for general-purpose programming. NumPy is for numerical computing and data analysis.



NumPy Notes with Practical Examples

What is NumPy?

NumPy (Numerical Python) is a powerful Python library used for:

  • Numerical Computing
  • Data Analysis
  • Machine Learning
  • Scientific Computing
  • Matrix Operations

Import NumPy

import numpy as np

Creating Arrays

1D Array

import numpy as np

arr = np.array([10, 20, 30, 40])

print(arr)

Output:

[10 20 30 40]

2D Array

matrix = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

print(matrix)

Output:

[[1 2 3]
 [4 5 6]]

Useful Array Functions

Create Array Using arange()

arr = np.arange(1, 11)

print(arr)

Output:

[1 2 3 4 5 6 7 8 9 10]

Create Array Using linspace()

arr = np.linspace(0, 100, 5)

print(arr)

Output:

[  0.  25.  50.  75. 100.]

Example 1: Student Marks Analysis

marks = np.array([78, 85, 90, 65, 88])

print("Total:", marks.sum())
print("Average:", marks.mean())
print("Highest:", marks.max())
print("Lowest:", marks.min())

Output:

Total: 406
Average: 81.2
Highest: 90
Lowest: 65

Real-World Use

School Result Systems


Example 2: Employee Salary Increment

Increase all salaries by 10%.

salary = np.array([
    25000,
    30000,
    45000,
    50000
])

new_salary = salary * 1.10

print(new_salary)

Output:

[27500. 33000. 49500. 55000.]

Concept

Vectorized Operations


Example 3: Monthly Sales Report

sales = np.array([
    12000,
    15000,
    18000,
    14000,
    17000
])

print("Average Sales:", sales.mean())
print("Best Month:", sales.max())

Output:

Average Sales: 15200
Best Month: 18000

Real-World Use

Business Analytics


Example 4: Temperature Conversion

Convert Celsius to Fahrenheit.

Formula:

F = (C × 9/5) + 32

celsius = np.array([0, 10, 20, 30])

fahrenheit = (celsius * 9/5) + 32

print(fahrenheit)

Output:

[32. 50. 68. 86.]

Example 5: Product Price Filtering

Find products costing more than ₹50,000.

prices = np.array([
    25000,
    60000,
    35000,
    80000,
    45000
])

expensive = prices[prices > 50000]

print(expensive)

Output:

[60000 80000]

Concept

Boolean Indexing


Example 6: Attendance System

students = np.array([
    "Ram",
    "John",
    "Amit",
    "Sita"
])

attendance = np.array([
    True,
    False,
    True,
    True
])

print(students[attendance])

Output:

['Ram' 'Amit' 'Sita']

Example 7: Image Brightness Increase

Images are represented as arrays.

image = np.array([
    [100, 120],
    [150, 200]
])

bright = image + 50

print(bright)

Output:

[[150 170]
 [200 250]]

Real-World Use

Image Processing


Example 8: Matrix Multiplication

A = np.array([
    [1, 2],
    [3, 4]
])

B = np.array([
    [5, 6],
    [7, 8]
])

result = np.dot(A, B)

print(result)

Output:

[[19 22]
 [43 50]]

Real-World Use

Machine Learning and AI


Example 9: Random Number Generation

random_numbers = np.random.randint(
    1,
    100,
    size=5
)

print(random_numbers)

Possible Output:

[45 12 87 34 56]

Real-World Use

Testing and Simulations


Example 10: Stock Market Analysis

Daily stock prices:

prices = np.array([
    100,
    105,
    102,
    110,
    120
])

change = np.diff(prices)

print(change)

Output:

[ 5 -3  8 10]

Real-World Use

Financial Analysis


Example 11: Reshaping Data

arr = np.arange(1, 13)

matrix = arr.reshape(3, 4)

print(matrix)

Output:

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

Concept

Reshaping Arrays


Example 12: Flattening Arrays

matrix = np.array([
    [1, 2],
    [3, 4]
])

flat = matrix.flatten()

print(flat)

Output:

[1 2 3 4]

Example 13: Finding Even Numbers

numbers = np.array([
    1,2,3,4,5,6,7,8,9,10
])

even = numbers[numbers % 2 == 0]

print(even)

Output:

[ 2 4 6 8 10]

Example 14: Quiz Score Analysis

scores = np.array([
    70,
    80,
    90,
    85,
    95
])

print("Mean:", np.mean(scores))
print("Standard Deviation:", np.std(scores))

Output:

Mean: 84.0
Standard Deviation: 8.6

Real-World Use

Performance Analysis


Common NumPy Functions Cheat Sheet

Function Purpose
np.array() Create Array
np.arange() Create Sequence
np.linspace() Evenly Spaced Values
np.zeros() Array of Zeros
np.ones() Array of Ones
np.mean() Average
np.sum() Total
np.max() Maximum Value
np.min() Minimum Value
np.std() Standard Deviation
np.reshape() Change Shape
np.flatten() Convert to 1D
np.dot() Matrix Multiplication
np.random.randint() Random Numbers

Key Takeaways

✅ NumPy is faster than Python Lists

✅ Supports vectorized operations

✅ Uses less memory

✅ Ideal for Data Science and Machine Learning

✅ Provides powerful mathematical and statistical functions

✅ Supports multidimensional arrays and matrix operations

Rule: If you are working with numbers, calculations, matrices, or large datasets, prefer NumPy over Python Lists.



0 Likes
11 Views

Filters

No filters available for this view.

Reset All