Skip to main content
NumPy is a Python library used for fast numerical computing with multidimensional arrays.

np.array()

Creates a NumPy array from a list, tuple, or other iterable.
Output
Converts a Python list into a NumPy array.

np.array(34)

Creates a zero-dimensional (scalar) NumPy array.
Output
A single value has 0 dimensions.

.ndim

Returns the number of dimensions of an array.
Output
The array has two dimensions.

.shape

Returns the shape of an array.
Output
The array contains 2 rows and 3 columns. Tip: Shape is always (rows, columns) for a 2D array.

np.arange()

Creates evenly spaced values within a range.
Output
Numbers increase by the specified step value.

np.linspace()

Creates evenly spaced numbers between two values.
Output
Generates 5 equally spaced numbers.

np.logspace()

Creates numbers spaced evenly on a logarithmic scale.
Output
Values are powers of 10.

np.zeros()

Creates an array filled with zeros.
Output
Creates a 2×3 matrix of zeros.

np.ones()

Creates an array filled with ones.
Output
Every element is initialized to 1.

np.full()

Creates an array filled with a specified value.
Output
All elements contain the same value.

np.empty()

Creates an uninitialized array.
Output
Values are random memory contents.

.dtype

Returns the data type of array elements.
Output
The array stores integer values.

astype()

Converts an array to another data type.
Output
The integer array becomes a float array.

.size

Returns the total number of elements.
Output
The array contains six elements.

.itemsize

Returns the memory occupied by one element.
Output
Each element occupies 4 bytes.

Array Manipulation

reshape()

Changes the shape of an array.
Output
The 1D array becomes a 2×3 array. Tip: Total elements must remain the same.

ravel()

Converts an array into a 1D view.
Output
Returns a flattened view of the original array. Tip: Changes in the view affect the original array.

flatten()

Returns a flattened copy of an array.
Output
Returns a new one-dimensional array. Tip: Changes do not affect the original array.

transpose()

Swaps rows and columns.
Output
Rows become columns.

swapaxes()

Swaps two specified axes.
Output
Axis 0 and Axis 1 are exchanged.

Indexing, Slicing & Iteration

Access Elements

Accesses elements using their index.
Output
Returns the element at index 2.

Slicing

Extracts a portion of an array.
Output
Returns elements from index 1 to 4. Tip: start is included, stop is excluded.

Reverse Slicing

Returns elements in reverse order.
Output
The array is reversed. Tip: [::-1] is the easiest way to reverse an array.

Multidimensional Slicing

Slices rows and columns together.
Output
Selects specific rows and columns. Tip: Use array[rows, columns].

Specific Rows and Columns

Accesses complete rows or columns.
Output
Returns the second column and second row.

np.take()

Selects elements using index positions.
Output
Returns elements at the specified indices.

np.nditer()

Efficiently iterates through every element.
Output
Visits every element one by one.

np.ndenumerate()

Iterates through elements with their indices.
Output
Returns both the index and value of each element.

Arithmetic Operations on NumPy Arrays

Performs mathematical operations element-wise on arrays.
Output
Each operation is performed on corresponding elements. Tip: Both arrays should have compatible shapes.

Universal Functions (ufuncs)

Built-in NumPy functions that operate element-wise on arrays.
Output
The square root is calculated for every element.

np.sin()

Computes the sine of each element (in radians).
Output
Returns the sine value of each angle. Tip: NumPy trigonometric functions use radians.

Broadcasting

Allows arrays of different shapes to perform arithmetic automatically.
Output
The value 10 is added to every element. Tip: Broadcasting avoids manually repeating data.

Vectorization

Performs operations on entire arrays without using loops.
Output
Each element is multiplied by 5. Tip: Vectorized operations are much faster than Python loops.

Joining Arrays

Combines multiple arrays into a single array.
Output
The two arrays are joined together.

np.concatenate()

Joins arrays along an existing axis.
Output
Arrays are joined row-wise. Tip: Use axis=1 to join column-wise.

np.vstack()

Stacks arrays vertically (row-wise).
Output
The arrays are stacked as rows.

np.hstack()

Stacks arrays horizontally (column-wise).
Output
The arrays are joined side by side.

np.stack()

Stacks arrays along a new axis.
Output
A new dimension is created while stacking. Tip: Unlike concatenate(), stack() creates a new axis.

Splitting Arrays

Divides an array into multiple sub-arrays.
Output
The array is split into three equal parts.

np.split()

Splits an array into equal sections.
Output
Returns a list of smaller arrays. Tip: The array size must be divisible by the number of splits.

np.repeat()

Repeats each element a specified number of times.
Output
Each element is repeated twice.

np.tile()

Repeats the entire array multiple times.
Output
The whole array is repeated two times. Tip: repeat() repeats elements, while tile() repeats the entire array.

np.sum()

Calculates the total sum of array elements.
Output
Adds all elements of the array.

np.std()

Calculates the standard deviation of array elements.
Output
Shows how much values differ from the average.

np.min()

Returns the smallest value in an array.
Output
Returns the minimum element.

Sum by Axis

Calculates sums along specific dimensions.
Output
axis=0 adds columns and axis=1 adds rows. Tip:
  • axis=0 → vertical operation (columns)
  • axis=1 → horizontal operation (rows)

np.cumsum()

Returns the cumulative sum of elements.
Output
Each element stores the sum of all previous elements.

np.cumprod()

Returns the cumulative product of elements.
Output
Each element stores the multiplication of previous values.

np.where()

Returns indices or values based on a condition.
Output
Returns positions where the condition is true.

np.argwhere()

Returns indices of elements that satisfy a condition.
Output
Returns the index positions of matching elements.

Masking in Arrays

Selects elements using a condition.
Output
Only elements satisfying the condition are selected. Tip: Masking is useful for filtering data.

np.logical_and()

Combines two conditions using AND operation.
Output
Returns values satisfying both conditions.

np.logical_or()

Combines two conditions using OR operation.
Output
Returns values satisfying either condition.

np.nan

Represents missing or undefined numerical values.
Output
nan represents a missing value.

np.isnan()

Checks whether elements are NaN values.
Output
Returns True for missing values.

Infinite Values

Represents values that are positive or negative infinity.
Output
Stores infinite numerical values.

np.isinf()

Checks whether elements are infinite.
Output
Returns True for infinite values.

np.nan_to_num()

Replaces NaN and infinity values with numbers.
Output
Converts invalid values into valid numbers.

np.random.random()

Generates random decimal numbers between 0 and 1.
Output
Creates random floating-point values.

np.random.choice()

Selects random values from an array.
Output
Randomly selects elements from the array.

np.random.seed()

Controls random number generation for reproducible results.
Output
The same random values are generated every time.

np.sort()

Sorts array elements in ascending order.
Output
Returns sorted values.

np.unique()

Returns unique values from an array.
Output
Removes duplicate values.

np.argsort()

Returns indices that would sort an array.
Output
Returns the positions of sorted elements.

np.argmax()

Returns the index of the maximum value.
Output
Returns the position of the largest element.

np.argmin()

Returns the index of the minimum value.
Output
Returns the position of the smallest element.

np.save()

Saves a NumPy array into a file.
Output
Stores array data permanently.

np.load()

Loads an array from a saved file.
Output
Reads the saved NumPy array.

np.mean()

Calculates the average value of elements.
Output
Returns the arithmetic average.

np.max()

Returns the largest value in an array.
Output
Returns the maximum element.

np.min()

Returns the smallest value in an array.
Output
Returns the minimum element.