AmbifulWorksheets
Cheat sheet
CBSE · Class 11 · Informatics Practices

Introduction to NumPy

NumPy arrays: creating them from lists and with zeros, ones and arange; their attributes ndim, shape, size, dtype and itemsize; and element-wise arithmetic.

Key ideas

Remember

Shape2-D: (rows, columns); 1-D: (n,) with a comma
sizerows × columns: np.zeros((3, 4)).size → 12
dtype from the itemsall whole numbers → int; any float → float; any string → string
itemsizefloat64 = 8 bytes; int32 = 32 bits = 4 bytes
Total bytessize × itemsize: np.ones((3, 5), dtype=np.int32) → 15 × 4 = 60
arangenp.arange(1, 10, 2) → 1, 3, 5, 7, 9; np.arange(6) → 0 to 5
Array and listimport numpy as np
L = [2, 5, 7]
A = np.array(L)
print(L * 2) # [2, 5, 7, 2, 5, 7]
print(A * 2) # [ 4 10 14]

Watch out

Worked example

What are the elements of c? Write them separated by commas.
import numpy as np
a = np.array([24, 13, 17, 45])
b = np.array([32, 30, 33, 28])
c = a + b

Working: element by element: 24 + 32, 13 + 30, 17 + 33, 45 + 28

Answer: 56, 43, 50, 73