📖 Scenario: You are working with a small grid of temperature readings from different sensors arranged in rows and columns. You want to access specific readings by their row and column positions.
🎯 Goal: Learn how to create a 2D NumPy array and access its elements using row and column indexes.
📋 What You'll Learn
Create a 2D NumPy array with exact values
Define variables for row and column indexes
Use these indexes to access the correct element in the array
Print the accessed element
💡 Why This Matters
🌍 Real World
Accessing specific data points in grids or tables is common in weather data, images, and spreadsheets.
💼 Career
Data scientists often need to extract and analyze specific values from multi-dimensional data arrays.
Progress0 / 4 steps
1
Create a 2D NumPy array
Import NumPy as np and create a 2D NumPy array called temps with these exact values: [[23, 25, 22], [21, 24, 20], [19, 22, 23]].
NumPy
Hint
Use np.array() to create a 2D array from a list of lists.
2
Set row and column indexes
Create two variables called row and col. Set row to 1 and col to 2 to select the second row and third column.
NumPy
Hint
Remember that Python uses zero-based indexing, so row 1 is the second row.
3
Access the element at the given row and column
Use the variables row and col to access the element in temps at that position. Store this value in a variable called selected_temp.
NumPy
Hint
Use temps[row, col] to get the element at the specified row and column.
4
Print the selected temperature
Print the value stored in selected_temp.
NumPy
Hint
Use print(selected_temp) to display the value.
Practice
(1/5)
1. What does arr[2, 1] mean when arr is a 2D numpy array?
easy
A. Access the element in the 3rd row and 2nd column
B. Access the element in the 2nd row and 3rd column
C. Access the entire 2nd row
D. Access the entire 1st column
Solution
Step 1: Understand zero-based indexing
In numpy, counting starts at 0, so index 2 means the 3rd row, and index 1 means the 2nd column.
Step 2: Interpret the indexing
arr[2, 1] accesses the element at row 3, column 2.
Final Answer:
Access the element in the 3rd row and 2nd column -> Option A
Quick Check:
Row 2, Col 1 means 3rd row, 2nd column [OK]
Hint: Remember: rows and columns start counting at zero [OK]
Common Mistakes:
Confusing row and column order
Starting count from 1 instead of 0
Thinking arr[2,1] returns a row or column
2. Which of the following is the correct syntax to access the element in the 1st row and 3rd column of a numpy 2D array arr?
easy
A. arr[1, 3]
B. arr[0, 2]
C. arr[3, 1]
D. arr[2, 0]
Solution
Step 1: Recall zero-based indexing
The 1st row is index 0, and the 3rd column is index 2.
Step 2: Match indexes to syntax
So, arr[0, 2] accesses the element in the 1st row and 3rd column.
Final Answer:
arr[0, 2] -> Option B
Quick Check:
Row 0, Col 2 = 1st row, 3rd column [OK]
Hint: Count rows and columns from zero for correct indexing [OK]
The array has 2 rows (index 0 and 1). Index 2 for row is out of range.
Step 2: Understand error type
Accessing arr[2, 0] causes an IndexError because row 2 does not exist.
Final Answer:
IndexError because row 2 does not exist -> Option C
Quick Check:
Row index 2 is invalid for 2-row array [OK]
Hint: Check array shape before indexing to avoid errors [OK]
Common Mistakes:
Assuming indexing starts at 1
Ignoring array shape
Confusing error types
5. You have a 4x4 numpy array arr filled with numbers 1 to 16 in row-major order. Which code correctly changes the element in the last row and second column to 100?
hard
A. arr[3, -2] = 100
B. arr[4, 2] = 100
C. arr[-1, 2] = 100
D. arr[3, 1] = 100
Solution
Step 1: Identify last row and second column indexes
For a 4x4 array, rows and columns are indexed 0 to 3. Last row is index 3, second column is index 1.
Step 2: Check each option
arr[3, 1] = 100 uses arr[3, 1] which is correct. arr[4, 2] = 100 uses invalid index 4. arr[-1, 2] = 100 uses column index 2 (third column), not second. arr[3, -2] = 100 uses negative index -2 for column which is the third column, not second.
Final Answer:
arr[3, 1] = 100 -> Option D
Quick Check:
Last row = 3, second column = 1 [OK]
Hint: Use zero-based indexes: last row is size-1, second column is 1 [OK]
Common Mistakes:
Using out-of-range indexes like 4
Confusing negative indexes for columns
Mixing row and column positions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.