📖 Scenario: You are working with daily temperatures recorded over a week. You want to create arrays representing days and temperature ranges to analyze the data easily.
🎯 Goal: Build arrays using np.arange() to represent days and temperature ranges, then print the arrays to see the results.
📋 What You'll Learn
Use np.arange() to create arrays
Create arrays with specific start, stop, and step values
Print the arrays to display the data
💡 Why This Matters
🌍 Real World
Scientists and analysts often need to create sequences of numbers to represent time, measurements, or ranges for experiments and data analysis.
💼 Career
Knowing how to create arrays with <code>np.arange()</code> is essential for data scientists and analysts to prepare and manipulate data efficiently.
Progress0 / 4 steps
1
Create an array of days using np.arange()
Import the numpy library as np. Then create an array called days using np.arange() that starts at 1 and ends at 8 (not including 8) to represent days 1 to 7.
NumPy
Hint
Use np.arange(start, stop) where stop is one more than the last day you want.
2
Create an array of temperatures from 20 to 30
Create an array called temps using np.arange() that starts at 20, ends at 31 (not including 31), with a step of 1 to represent temperatures from 20 to 30 degrees.
NumPy
Hint
Remember to include the step value as the third argument in np.arange().
3
Create an array of temperatures with step 0.5
Create an array called temps_half using np.arange() that starts at 20, ends at 22 (not including 22), with a step of 0.5 to represent temperatures increasing by half degrees.
NumPy
Hint
Use a decimal number for the step to get half-degree increments.
4
Print all three arrays
Print the arrays days, temps, and temps_half each on a separate line to see their values.
NumPy
Hint
Use three separate print() statements, one for each array.
Practice
(1/5)
1. What does np.arange(3) produce?
easy
A. [0, 1, 2]
B. [1, 2, 3]
C. [0, 1, 2, 3]
D. [3]
Solution
Step 1: Understand default start value
If only one number is given, np.arange starts from 0 by default.
Step 2: Identify stop value exclusion
The stop value 3 is excluded, so the array includes 0, 1, 2.
Final Answer:
[0, 1, 2] -> Option A
Quick Check:
Start=0, stop=3 excluded, so [0,1,2] [OK]
Hint: Remember: stop value is NOT included in output [OK]
Common Mistakes:
Including the stop value in the array
Starting from 1 instead of 0
Confusing step with stop
2. Which of these is the correct syntax to create an array from 2 to 6 (excluding 6) with step 1 using np.arange()?
easy
A. np.arange(6, 2, 1)
B. np.arange(2, 6, 0)
C. np.arange(2, 7)
D. np.arange(2, 6, 1)
Solution
Step 1: Check start, stop, and step order
Start should be less than stop for positive step; step cannot be zero.
Step 2: Validate each option
np.arange(2, 6, 1) uses start=2, stop=6, step=1 correctly. np.arange(6, 2, 1) reverses start and stop. np.arange(2, 7) sets stop=7, producing [2 3 4 5 6] which includes 6. np.arange(2, 6, 0) has step=0 which is invalid.
Final Answer:
np.arange(2, 6, 1) -> Option D
Quick Check:
Start=2, stop=6, step=1 is correct syntax [OK]
Hint: Step must not be zero; start < stop for positive step [OK]
Common Mistakes:
Using zero as step value
Reversing start and stop values
Using wrong stop value
3. What is the output of this code?
import numpy as np
arr = np.arange(1, 10, 3)
print(arr)
medium
A. [1 4 7]
B. [1 3 6 9]
C. [1 2 3 4 5 6 7 8 9]
D. [3 6 9]
Solution
Step 1: Understand parameters of np.arange
Start=1, stop=10 (excluded), step=3 means numbers start at 1 and increase by 3 until less than 10.
Step 2: Calculate values
Values are 1, 4 (1+3), 7 (4+3). Next would be 10 which is excluded.