This program shows how to get one temperature value from a 2D array and how to update one value.
NumPy
import numpy as np
# Create a 2D array representing temperatures in Celsius
temps = np.array([[22, 24, 19], [21, 23, 20], [20, 22, 18]])
# Access the temperature on the first day, second time slot
temp_value = temps[0, 1]
print(f"Temperature on day 1, time slot 2: {temp_value}°C")
# Change the temperature on day 3, time slot 3
temps[2, 2] = 25print("Updated temperatures:")
print(temps)
OutputSuccess
Important Notes
Trying to access an index outside the array size will cause an error.
Negative indexes count from the end, like -1 is the last element.
Summary
Single element access lets you read or change one value in a numpy array.
Use zero-based indexes inside square brackets, separated by commas for multi-dimensional arrays.
This is useful for precise data handling and updates.
Practice
(1/5)
1. What does single element access in a numpy array allow you to do?
easy
A. Sort the entire array
B. Add new rows to the array
C. Read or change one specific value in the array
D. Calculate the sum of all elements
Solution
Step 1: Understand the concept of single element access
Single element access means selecting one value from the array using its position.
Step 2: Identify what single element access can do
It allows reading or changing that one value, not adding rows, sorting, or summing.
Final Answer:
Read or change one specific value in the array -> Option C
Quick Check:
Single element access = read/change one value [OK]
Hint: Single element access targets one value by position [OK]
Common Mistakes:
Confusing single element access with adding or removing elements
Thinking it can perform operations like sum or sort
Mixing up element access with slicing
2. Which of the following is the correct syntax to access the element in the 2nd row and 3rd column of a numpy array named arr?
easy
A. arr[2, 3]
B. arr[1, 2]
C. arr(1, 2)
D. arr[2][3]
Solution
Step 1: Recall zero-based indexing in numpy
Indexing starts at 0, so 2nd row is index 1, 3rd column is index 2.
Step 2: Check syntax for multi-dimensional access
Use square brackets with comma-separated indexes: arr[1, 2]. Parentheses or double brackets are incorrect here.