NumPy
Array Creation
Create arrays from lists and built-in constructors.
By EZ4Code Team
arraycreation
Code
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([[1, 2], [3, 4]], dtype=float)
zeros = np.zeros((2, 3))
ones = np.ones(5)
full = np.full((2, 2), 7)
eye = np.eye(3)
arange = np.arange(0, 10, 2)
linspace = np.linspace(0, 1, 5)
random = np.random.rand(2, 2)
print(a.shape, b.ndim, b.dtype)
print(eye)Explanation
np.array() converts a list (or nested list) into an ndarray with an explicit dtype. Constructors like zeros, ones, eye, and full create arrays of a given shape without writing loops. arange and linspace produce evenly spaced ranges for sampling and plotting.
More NumPy Snippets
Indexing and Slicing
Slice arrays and index with boolean masks.
Broadcasting
Combine arrays of compatible shapes without copying.
Math Operations
Apply element-wise math and reductions.
Linear Algebra
Solve systems, factorize, and compute eigenvalues.
Random Numbers
Sample from distributions with a Generator.
Reshaping
Reshape, transpose, and stack arrays.