Bird
Raised Fist0
NumPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of a numpy ndarray?

easy
A. To display images on screen
B. To create text documents
C. To store numbers in one or more dimensions for fast math operations
D. To connect to databases

Solution

  1. Step 1: Understand what ndarray stores

    ndarray is designed to hold numbers arranged in rows and columns or more dimensions.
  2. Step 2: Recognize its use for math

    It allows fast math operations on many numbers at once, unlike regular Python lists.
  3. Final Answer:

    To store numbers in one or more dimensions for fast math operations -> Option C
  4. Quick Check:

    ndarray = fast number storage [OK]
Hint: ndarray = fast multi-dimensional number storage [OK]
Common Mistakes:
  • Thinking ndarray stores text or images
  • Confusing ndarray with database tools
  • Assuming ndarray is for display only
2.

Which of the following is the correct way to create a 1D ndarray from a Python list [1, 2, 3]?

import numpy as np
# Which line is correct?
easy
A. arr = np.array([1, 2, 3])
B. arr = np.ndarray([1, 2, 3])
C. arr = np.array(1, 2, 3)
D. arr = np.array{1, 2, 3}

Solution

  1. Step 1: Recall correct syntax for np.array()

    np.array() takes a list or nested lists inside parentheses.
  2. Step 2: Check each option

    arr = np.array([1, 2, 3]) uses correct syntax with square brackets inside parentheses. Others have wrong function calls or braces.
  3. Final Answer:

    arr = np.array([1, 2, 3]) -> Option A
  4. Quick Check:

    np.array(list) syntax = correct [OK]
Hint: Use np.array() with list inside parentheses [OK]
Common Mistakes:
  • Using np.ndarray() directly without parameters
  • Passing multiple arguments instead of one list
  • Using curly braces {} instead of []
3.

What will be the output of the following code?

import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)
medium
A. (4,)
B. (2, 2)
C. (1, 4)
D. (2,)

Solution

  1. Step 1: Understand the array shape

    The array has 2 rows and 2 columns, so shape is (2, 2).
  2. Step 2: Check print output

    arr.shape returns a tuple with dimensions, so output is (2, 2).
  3. Final Answer:

    (2, 2) -> Option B
  4. Quick Check:

    2 rows and 2 columns = (2, 2) [OK]
Hint: Shape shows rows and columns as tuple [OK]
Common Mistakes:
  • Confusing shape with total elements
  • Expecting a list instead of tuple
  • Mixing up rows and columns count
4.

Find the error in this code snippet:

import numpy as np
arr = np.array([1, 2, 3])
print(arr[3])
medium
A. SyntaxError due to wrong print statement
B. No error, prints 3
C. TypeError because arr is not a list
D. IndexError because index 3 is out of bounds

Solution

  1. Step 1: Check array length and indexing

    arr has elements at indices 0, 1, 2 only. Index 3 is out of range.
  2. Step 2: Understand error type

    Accessing arr[3] causes IndexError because index is invalid.
  3. Final Answer:

    IndexError because index 3 is out of bounds -> Option D
  4. Quick Check:

    Index out of range = IndexError [OK]
Hint: Check array length before indexing [OK]
Common Mistakes:
  • Thinking arr[3] returns last element
  • Confusing IndexError with SyntaxError
  • Assuming numpy arrays allow out-of-range access
5.

You have a nested list [[1, 2], [3, 4], [5, 6]]. How do you create a numpy ndarray and then get the sum of all elements?

import numpy as np
nested_list = [[1, 2], [3, 4], [5, 6]]
# Fill in the blanks
arr = np.array(nested_list)
total = _____
print(total)
hard
A. arr.sum()
B. sum(arr)
C. arr.add()
D. np.sum(arr, axis=1)

Solution

  1. Step 1: Create ndarray from nested list

    np.array(nested_list) creates a 2D array with shape (3, 2).
  2. Step 2: Use ndarray method to sum all elements

    arr.sum() returns sum of all elements in the array.
  3. Final Answer:

    arr.sum() -> Option A
  4. Quick Check:

    Use arr.sum() for total sum [OK]
Hint: Use arr.sum() to sum all elements [OK]
Common Mistakes:
  • Using Python sum() which sums rows, not elements
  • Using arr.add() which does not exist
  • Using np.sum with axis=1 sums rows, not total
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard