Skip to main content

np.array()

Creates a NumPy array from a list or tuple.
Output:

.ndim

Returns the number of dimensions of the array.
Output:

np.array(34)

Creates a scalar (0D) NumPy array.
Output:

.shape

Returns the shape as (rows, columns).
Output:

np.arange()

Creates numbers within a range using step size.
Output:

np.linspace()

Creates evenly spaced numbers between two values.
Output:

np.logspace()

Creates numbers in logarithmic scale.
Output:

np.zeros()

Creates an array filled with zeros.
Output:

2D Example

Output:

np.ones()

Creates an array filled with ones.
Output:

np.full()

Creates an array filled with a custom value.
Output:

2D Example

Output:

np.empty()

Creates an uninitialized array with random garbage values.
Output:
(Output varies every time)

np.random.rand()

Generates random numbers between 0 and 1.
Output:
(Random output)

np.random.randn()

Generates random numbers from normal distribution.
Output:
(Random output)

np.random.randint()

Generates random integers within a range.
Output:
(Random output)

.dtype

Returns datatype of array elements.
Output:

astype()

Converts array datatype into another datatype.
Output:

.size

Returns total number of elements.
Output:

.itemsize

Returns memory size of one element in bytes.
Output:

reshape()

Changes array shape without changing data.
Output:

ravel()

Converts multi-dimensional array into 1D view.
Output:

flatten()

Converts array into independent 1D copy.
Output:

Arithmetic Operations on NumPy Arrays

NumPy supports direct arithmetic operations on arrays.
Output:

Universal Functions (ufuncs)

Universal functions perform element-wise operations efficiently.

np.sin()

Calculates sine value for each element.
Output:

Indexing and Slicing

Access Elements

Output:

Slicing

Output:

Reverse Slicing

Output:

Multidimensional Slicing

Output:

Specific Rows and Columns

Output:

Index Array using np.take()

Selects elements using index positions.
Output:

Iterating NumPy Arrays

np.nditer()

Iterates element by element.
Output:

np.ndenumerate()

Returns index and value while iterating.
Output:

Views vs Copies

View

Changes affect original array.
Output:

Copy

Changes do not affect original array.
Output:

Transpose of Matrix

transpose()

Converts rows into columns.
Output:

swapaxes()

Swaps two specific axes in multidimensional arrays.
Output:

Concatenation and Stacking

np.concatenate()

Joins arrays into one array.
Output:

np.vstack()

Stacks arrays vertically.
Output:

np.hstack()

Stacks arrays horizontally.
Output:

np.stack()

Stacks arrays along a new axis.
Output:

Splitting Arrays

np.split()

Splits array into equal parts.
Output:

Repeat vs Tile

np.repeat()

Repeats each element multiple times.
Output:

np.tile()

Repeats whole array multiple times.
Output:

Aggregate Functions

np.sum()

Returns sum of elements.
Output:

np.std()

Returns standard deviation.
Output:

np.min()

Returns minimum value.
Output:

Sum by Axis

Output:

np.cumsum()

Returns cumulative sum.
Output:

np.cumprod()

Returns cumulative product.
Output:

Conditional Operations

np.where()

Applies condition and returns values.
Output:

np.argwhere()

Returns indexes where condition is true.
Output:

Masking in Arrays

np.logical_and()

Checks multiple conditions together.
Output:

np.logical_or()

Returns true if any condition is true.
Output:

Broadcasting

Broadcasting automatically applies operations across arrays.
Output:

Vectorization

Applies functions efficiently without loops.
Output:

Missing Values

np.nan

Represents missing values.
Output:

np.isnan()

Checks for NaN values.
Output:

Infinite Values

np.isinf()

Checks infinite values.
Output:

np.nan_to_num()

Converts NaN and Inf into numbers.
Output: