Challenge - 5 Problems
np.arange() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.arange() with float step
What is the output of this code snippet using
np.arange()?NumPy
import numpy as np result = np.arange(1.0, 2.0, 0.3) print(result)
Attempts:
2 left
💡 Hint
Remember that np.arange stops before reaching the stop value.
✗ Incorrect
The np.arange() function creates values starting at 1.0, increasing by 0.3, but stops before reaching 2.0. So it includes 1.0, 1.3, 1.6, and 1.9.
❓ data_output
intermediate1:30remaining
Length of array from np.arange() with negative step
How many elements are in the array created by this code?
NumPy
import numpy as np arr = np.arange(5, 0, -1) print(len(arr))
Attempts:
2 left
💡 Hint
Count numbers from 5 down to but not including 0, stepping by -1.
✗ Incorrect
np.arange(5, 0, -1) creates the array [5, 4, 3, 2, 1], which has 5 elements. It starts at 5, decrements by 1, and stops before reaching 0.
❓ Predict Output
advanced2:00remaining
Output of np.arange() with integer step and float start
What is the output of this code?
NumPy
import numpy as np result = np.arange(0.5, 5, 1) print(result)
Attempts:
2 left
💡 Hint
np.arange includes start and increments by step until reaching or passing stop.
✗ Incorrect
Starting at 0.5, adding 1 each time, the values are 0.5, 1.5, 2.5, 3.5, 4.5. It stops before 5, so 5.5 is not included.
❓ visualization
advanced2:30remaining
Plotting np.arange() output
Which plot correctly shows the points generated by
np.arange(0, 3, 0.5)?NumPy
import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 3, 0.5) plt.scatter(x, [1]*len(x)) plt.xticks(x) plt.yticks([]) plt.show()
Attempts:
2 left
💡 Hint
Check the start, stop, and step values carefully.
✗ Incorrect
The np.arange(0, 3, 0.5) generates values starting at 0, increasing by 0.5, stopping before 3. So points are at 0, 0.5, 1, 1.5, 2, 2.5.
🧠 Conceptual
expert2:00remaining
Why might np.arange() produce unexpected results with floats?
Why can
np.arange() sometimes produce arrays with unexpected last elements when using float steps?Attempts:
2 left
💡 Hint
Think about how computers store decimal numbers.
✗ Incorrect
Floating point numbers are stored approximately in computers. This can cause small rounding errors. When np.arange() uses these floats, the stop condition may be affected, leading to unexpected last elements.