📖 Scenario: Imagine you have a list of daily temperatures recorded over two weeks. You want to analyze specific parts of this data by selecting certain days using slicing.
🎯 Goal: Learn how to use start:stop:step slicing on a NumPy array to select specific elements.
📋 What You'll Learn
Create a NumPy array with exact temperature values
Define slicing parameters as variables
Use slicing with start:stop:step on the array
Print the sliced array
💡 Why This Matters
🌍 Real World
Slicing data arrays is common when analyzing time series data like temperatures, stock prices, or sensor readings to focus on specific intervals.
💼 Career
Data scientists and analysts often use slicing to quickly extract relevant parts of datasets for visualization or further analysis.
Progress0 / 4 steps
1
Create the NumPy array of temperatures
Import NumPy as np and create a NumPy array called temps with these exact values: [22, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
NumPy
Hint
Use np.array([...]) to create the array with the exact temperature values.
2
Set slicing parameters
Create three variables: start set to 2, stop set to 12, and step set to 3.
NumPy
Hint
Assign the numbers 2, 12, and 3 to variables named start, stop, and step respectively.
3
Slice the array using start:stop:step
Create a new variable called sliced_temps that slices the temps array using the variables start, stop, and step with the syntax temps[start:stop:step].
NumPy
Hint
Use the slicing syntax with the variables: temps[start:stop:step].
4
Print the sliced array
Print the variable sliced_temps to display the sliced temperatures.
NumPy
Hint
Use print(sliced_temps) to show the result.
Practice
(1/5)
1. What does the step value control in a NumPy array slice like arr[start:stop:step]?
easy
A. How many elements to skip between each selected element
B. The total number of elements to select
C. The starting index of the slice
D. The ending index of the slice
Solution
Step 1: Understand the slice syntax
The slice arr[start:stop:step] selects elements from start up to but not including stop, moving by step.
Step 2: Identify the role of step
The step determines how many elements to skip between each selected element. For example, step=2 picks every second element.
Final Answer:
How many elements to skip between each selected element -> Option A
Quick Check:
Step controls skipping elements = A [OK]
Hint: Step is the gap between elements in slicing [OK]
Common Mistakes:
Confusing step with start or stop index
Thinking step is total count of elements
Ignoring that step can be negative
2. Which of the following is the correct syntax to slice a NumPy array arr to get every 3rd element from index 1 to 10?
easy
A. arr[10:1:3]
B. arr[1:3:10]
C. arr[1:10:3]
D. arr[3:1:10]
Solution
Step 1: Recall slice order
The slice syntax is arr[start:stop:step]. Here, start=1, stop=10, step=3 means from index 1 up to 9, every 3rd element.
Step 2: Check each option
arr[1:10:3] matches the correct order and values. Others have wrong order or values that don't match the requirement.
Final Answer:
arr[1:10:3] -> Option C
Quick Check:
Correct slice syntax = D [OK]
Hint: Remember slice is start:stop:step in order [OK]
Common Mistakes:
Mixing up stop and step positions
Using step as stop index
Reversing start and stop incorrectly
3. Given arr = np.array([10, 20, 30, 40, 50, 60, 70]), what is the output of arr[1:6:2]?
medium
A. [10 30 50 70]
B. [20 30 40 50 60]
C. [30 50]
D. [20 40 60]
Solution
Step 1: Identify slice indices
The slice arr[1:6:2] starts at index 1 (value 20), stops before index 6, stepping by 2.
Step 2: Select elements
Indexes selected: 1 (20), 3 (40), 5 (60). So output is [20 40 60].
Final Answer:
[20 40 60] -> Option D
Quick Check:
Elements at 1,3,5 = [20 40 60] [OK]
Hint: Pick elements at start, start+step, ... until stop-1 [OK]
Common Mistakes:
Including stop index element
Using wrong step size
Starting from index 0 instead of 1
4. What is wrong with this code snippet? arr = np.array([1,2,3,4,5]) slice = arr[4:1:-1] What will slice contain?
medium
A. [4 3 2]
B. [5 4 3]
C. Empty array
D. IndexError
Solution
Step 1: Understand negative step slicing
With step=-1, slicing goes backwards from start index 4 down to stop index 1 (exclusive).