In this article Math and Numpy module we give the information about the math module provides mathematical functions for numbers (integers and floats) and NumPy is a Numerical Python is a powerful open-source library used for numerical computations in Python.

Math and Numpy module

Using the math Module (for integers/lists)

  • The math module provides mathematical functions for numbers (integers and floats).
  • Works on single values (not arrays).

Import

import math

Examples

import math

x = 25

print(math.sqrt(x))       # 5.0 → square root

print(math.factorial(5))  # 120

print(math.gcd(24, 36))   # 12

print(math.pow(2, 3))     # 8.0

print(math.ceil(4.2))     # 5

print(math.floor(4.8))    # 4

print(math.pi)            # 3.141592…

Applying to a list:

nums = [4, 9, 16, 25]

sqrts = [math.sqrt(n) for n in nums]

print(sqrts)   # [2.0, 3.0, 4.0, 5.0]

  1. Using the numpy Module (for arrays)
  • numpy is a powerful library for numerical computations.
  • Unlike math, it works on entire arrays at once (vectorized operations).

NumPy Libraries:

NumPy is a Numerical Python is a powerful open-source library used for numerical computations in Python.

It provides support for:

  • Large, multi-dimensional arrays and matrices
  • A large collection of high-level mathematical functions

Fast operations on arrays

Import

import numpy as np

Creating Arrays

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

print(arr)                # [1 2 3 4 5]

Mathematical Operations

print(np.sqrt(arr))       # [1. 1.414 1.732 2. 2.236]

print(np.power(arr, 2))   # [ 1  4  9 16 25]

print(np.sum(arr))        # 15

print(np.mean(arr))       # 3.0

print(np.max(arr))        # 5

print(np.min(arr))        # 1

NumPy Libraries:

  1. Create an array:

import numpy as np

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

print(arr)     # Output: [1 2 3 4]

type(arr) # show the data type

  1. Array operations

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

b = np.array([4, 5, 6])

print(a + b)    # Output: [5 7 9]

  1. Multidimensional array:

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

print(matrix)        #  O/P: [[1 2] [3 4]]

  1. Useful functions:

np.zeros((2, 3))     # 2×3 array of zeros

np.ones((3, 3))      # 3×3 array of ones

np.eye(3)            # 3×3 identity matrix

np.arange(0, 10, 2)  # [0 2 4 6 8]

np.linspace(0, 1, 5) # Linear Space: [0. 0.25 0.5 0.75 1.]

# This is a NumPy function used to generate evenly spaced numbers over a specified range.

  1. Array reshaping and slicing:

a = np.arange(10)

print(a[2:7])       # Output: [2 3 4 5 6]

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

print(b.shape) # Output: (2, 3)

print(b.reshape(3,2))    #  [[1 2] [3 4] [5 6]]

  1. Mathematical operations:

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

print(np.mean(a))       # Average     2.0

print(np.std(a))        # Standard deviation 0.816496580927726

print(np.sum(a))        # Sum   6

  1. Initializing numpy array with same number:

import numpy as np

n1=np.full((2,2),10)

n1

# O/P: array([[10, 10], [10, 10]])

  1. Initializing numpy array within a range

#1. Example

import numpy as np

n1=np.arange(10,20)

n1                 # array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])

#2.Example

import numpy as np

n1=np.arange(10,50,5)

n1                    # array([10, 15, 20, 25, 30, 35, 40, 45])

  1. Initializing NumPy Array with random numbers

import numpy as np

n1=np.random.randint(1,100,5)

n1               # array([94, 36, 88, 74, 71], dtype=int32)

  1. Checking the shape of NumPy arrays

import numpy as np

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

n1.shape         # (2, 3)

n1.shape=(3,2)

n1.shape

n1             #array([[1, 2],

[3, 4],

[5, 6]])

  1. Joining NumPy Array:

vstack(), hstack(), column_stack()

vstack() :

import numpy as np

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

n2=np.array([40,50,60])

np.vstack((n1,n2))

# array([[10, 20, 30],

[40, 50, 60]])

hstack():

import numpy as np

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

n2=np.array([40,50,60])

np.hstack((n1,n2))

# array([10, 20, 30, 40, 50, 60])

column_stack():

import numpy as np

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

n2=np.array([40,50,60])

np.column_stack((n1,n2))

# array([[10, 40],

[20, 50],

[30, 60]])

  1. NumPy Union, Insertion and Difference :

import numpy as np

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

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

print(“Union: “, np.union1d(a, b))         # [1 2 3 4 5 6]

print(“Difference (A – B):”, np.setdiff1d(a, b))  # [1 2]

print(“Intersection:”, np.intersect1d(a, b))      # [3 4]

print(“Symmetric Diff:”, np.setxor1d(a, b))       # [1 2 5 6]

  1. mean, median, standard deviation:

import numpy as np

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

print(“Data:”, data)

print(“Mean:”, np.mean(data))

print(“Median:”, np.median(data))

print(“Standard Deviation:”, np.std(data))

  1. Math vs NumPy (Key Differences)
Feature math Module numpy Module
Works on Single numbers Arrays (1D, 2D, etc.)
Speed Slower on large data Very fast (uses C backend)
Functions sqrt, pow, factorial, gcd, log, sin, cos sqrt, power, sum, mean, std, matrix ops
Use Case Small numeric calculations Large datasets, scientific computing

 Summary

  • Use math for basic single-value calculations.
  • Use numpy for arrays, matrices, and large data processing.
Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Leave a Reply

Your email address will not be published. Required fields are marked *