0
0
NumPydata~15 mins

Negative indexing in NumPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Negative Indexing with NumPy Arrays
📖 Scenario: You are working with daily temperature data for a week. You want to access specific days using negative indexing, which counts from the end of the data.
🎯 Goal: Learn how to use negative indexing on a NumPy array to access elements from the end.
📋 What You'll Learn
Create a NumPy array with exact temperature values
Create a variable to hold the negative index
Use negative indexing to get the temperature for that day
Print the temperature value
💡 Why This Matters
🌍 Real World
Negative indexing helps quickly access recent data points, like the last few days of temperature, sales, or stock prices, without counting the total length.
💼 Career
Data scientists often use negative indexing to efficiently retrieve recent records from datasets, making data analysis faster and code cleaner.
Progress0 / 4 steps
1
Create a NumPy array with temperatures
Import NumPy as np and create a NumPy array called temps with these exact values: 23, 25, 22, 20, 19, 21, 24.
NumPy
Need a hint?

Use np.array([...]) to create the array with the exact values.

2
Create a variable for negative index
Create a variable called neg_index and set it to -2 to represent the second last day.
NumPy
Need a hint?

Just assign -2 to the variable neg_index.

3
Use negative indexing to get temperature
Create a variable called temp_neg and assign it the value from temps at the negative index neg_index.
NumPy
Need a hint?

Use temps[neg_index] to get the value at the negative index.

4
Print the temperature from negative index
Print the value of temp_neg to display the temperature for the second last day.
NumPy
Need a hint?

Use print(temp_neg) to show the temperature.