NumPy Array Creation
Last Updated: 02 Nov 2025
NumPy gives us multiple easy ways to create arrays — from lists, using built-in functions, or generating patterns.
Creating Arrays from Python Lists
Convert a normal Python list into a NumPy ndarray (n-dimensional) array using np.array().
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr)
print(type(arr))
arr=np.array([1,2,3], dtype=np.float64)
print(arr)
print(type(arr))
Creating 2D and 3D Arrays
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
print(arr)
🧠 Tip:
- 1D → simple list
- 2D → list of lists
- 3D → list of list of lists
Numpy Arrays Attributes
ndim: Returns the number of dimensions of the array.shape: Returns the dimensions of the array.dtype: Returns the data type of the array.size: Returns the total number of elements in the array.itemsize: Returns the size of each element in the array in bytes.nbytes: Total bytes consumed by array
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Array:\n", arr)
print("Dimensions:", arr.ndim)
print("Shape:", arr.shape)
print("Size:", arr.size)
print("Data Type:", arr.dtype)
print("Item Size:", arr.itemsize)
print("Total Bytes:", arr.nbytes)
print("Memory Address:", arr.data)