practicepython.in
python exercises
171 exercises

Working with arrays

The only track that loads a library, because numpy is not a library you can reason about from the outside: a slice shares memory with the original, a one-line operation silently stretches shapes to fit, and an int8 rolls over at 127 without a word. These exercises are the handful of behaviours that surprise people who already know Python. numpy is fetched the first time you open one of these pages and cached afterwards, so give it a moment.

12 exercisesdifficulty 2–4~268 min in total
Selecting with a condition instead of a loop boolean arrays, & rather than and ~20 min Running totals and the gaps between readings cumsum, diff shortens the array ~20 min An if statement with no if in it np.where, both branches are evaluated ~20 min Where the largest value is, not what it is argmax returns a position, unravel_index ~24 min A slice of an array is not a copy of it views versus copies, assigning into a slice ~22 min Shapes that stretch to fit broadcasting rules, trailing dimensions ~24 min Brightening a pixel until it goes dark fixed-width integers, silent wraparound ~24 min Picking rows in an order you choose indexing with an array, fancy indexing copies ~22 min The value that is not equal to itself nan propagation, isnan rather than == ~24 min The same numbers, a different shape reshape, the -1 dimension ~20 min Three lists into three columns stack adds a dimension, concatenate joins one ~22 min The at sign is not the star @ versus *, inner dimensions must match ~26 min