0
0
NumPydata~10 mins

ndarray as the core data structure in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ndarray as the core data structure
Create ndarray
Store data in contiguous memory
Access elements by index
Perform operations element-wise
Return new ndarray or modify in-place
This flow shows how an ndarray is created, stores data, allows element access, and supports element-wise operations.
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
print(arr[1])
print(arr * 2)
Create a 1D ndarray, print it, access an element, and multiply all elements by 2.
Execution Table
StepActionCode LineVariable StateOutput
1Import numpy as npimport numpy as npnp module loaded
2Create ndarray from list [1, 2, 3]arr = np.array([1, 2, 3])arr = ndarray([1, 2, 3])
3Print entire arrayprint(arr)arr unchanged[1 2 3]
4Access element at index 1print(arr[1])arr unchanged2
5Multiply array by 2 element-wiseprint(arr * 2)arr unchanged[2 4 6]
💡 All steps executed; array operations completed.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
arrundefined[1 2 3][1 2 3][1 2 3][1 2 3]
Key Moments - 2 Insights
Why does multiplying arr by 2 not change arr itself?
Because arr * 2 creates a new ndarray with multiplied values but does not modify arr in-place, as shown in execution_table step 5.
How does arr[1] access the second element?
Indexing starts at 0, so arr[1] accesses the element at position 1, which is 2, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of step 3?
A[2 4 6]
B[1 2 3]
C2
DError
💡 Hint
Check the Output column for step 3 in the execution_table.
At which step is the element 2 accessed from arr?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the Action and Output columns in execution_table for element access.
If we assign arr = arr * 2, what changes in variable_tracker after step 5?
Aarr becomes [2 4 6]
Barr remains [1 2 3]
Carr becomes 2
Darr becomes undefined
💡 Hint
Multiplying creates a new array; assigning it updates arr, check variable_tracker logic.
Concept Snapshot
ndarray is numpy's main data structure
Stores data in contiguous memory
Supports indexing and slicing
Allows element-wise operations
Operations return new ndarrays by default
Full Transcript
We start by importing numpy as np. Then we create an ndarray named arr from a Python list [1, 2, 3]. This ndarray stores the numbers in a continuous block of memory. We print the entire array, which outputs [1 2 3]. Next, we access the element at index 1, which is 2, and print it. Finally, we multiply the array by 2, which creates a new array [2 4 6] without changing the original arr. This shows how ndarray supports element-wise operations and indexing.