📖 Scenario: Imagine you have a list of daily temperatures for a week. You want to find the temperature on a specific day and also change the temperature for a day if needed.
🎯 Goal: You will create a NumPy array of temperatures, select a specific day's temperature using indexing, update a temperature, and then print the updated array.
📋 What You'll Learn
Create a NumPy array called temps with the exact values: 22, 24, 19, 23, 25, 20, 21
Create a variable called day_index and set it to 3
Use day_index to get the temperature for that day from temps and store it in selected_temp
Update the temperature at day_index in temps to 26
Print the updated temps array
💡 Why This Matters
🌍 Real World
Indexing is important when working with data like daily temperatures, sales numbers, or any list of values where you want to access or change specific items.
💼 Career
Data scientists often need to select and update data points efficiently using indexing in arrays or tables.
Progress0 / 4 steps
1
Create the temperature array
Create a NumPy array called temps with these exact values: 22, 24, 19, 23, 25, 20, 21
NumPy
Hint
Use np.array([...]) to create the array with the exact numbers inside the brackets.
2
Set the day index
Create a variable called day_index and set it to 3
NumPy
Hint
Just assign the number 3 to the variable day_index.
3
Select and update the temperature
Use day_index to get the temperature from temps and store it in selected_temp. Then update the temperature at day_index in temps to 26
NumPy
Hint
Use temps[day_index] to get and set the value at that position.
4
Print the updated temperatures
Print the updated temps array
NumPy
Hint
Use print(temps) to show the updated array.
Practice
(1/5)
1. What does indexing in NumPy allow you to do?
easy
A. Create new arrays from scratch
B. Select specific elements or slices from an array
C. Automatically sort the array elements
D. Convert arrays into lists
Solution
Step 1: Understand the purpose of indexing
Indexing is used to access specific parts of an array, like single elements or slices.
Step 2: Compare with other options
Creating new arrays, sorting, or converting are different operations, not indexing.
Final Answer:
Select specific elements or slices from an array -> Option B
Quick Check:
Indexing = selecting parts of array [OK]
Hint: Indexing means picking parts of data quickly [OK]
Common Mistakes:
Confusing indexing with array creation
Thinking indexing sorts data
Mixing indexing with data type conversion
2. Which of the following is the correct syntax to get the first element of a NumPy array arr?
easy
A. arr.first()
B. arr(0)
C. arr[0]
D. arr{0}
Solution
Step 1: Recall NumPy indexing syntax
NumPy arrays use square brackets with zero-based indices to access elements.
Step 2: Check each option
Only arr[0] uses correct square brackets and zero index; others are invalid syntax or methods.
Final Answer:
arr[0] -> Option C
Quick Check:
First element = arr[0] [OK]
Hint: Use square brackets and zero-based index [OK]