Bird
Raised Fist0
NumPydata~10 mins

np.arange() for range arrays in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a NumPy array from 0 to 4 using np.arange().

NumPy
import numpy as np
arr = np.arange([1])
print(arr)
Drag options to blanks, or click blank then click option'
A5
B10
C6
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using 4 instead of 5 will create array up to 3 only.
Confusing np.arange() with range() which behaves similarly but returns a list.
2fill in blank
medium

Complete the code to create a NumPy array from 2 to 6 (excluding 6) using np.arange().

NumPy
import numpy as np
arr = np.arange([1], 6)
print(arr)
Drag options to blanks, or click blank then click option'
A0
B2
C1
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 1 as start will include unwanted numbers.
Forgetting that the stop value is excluded.
3fill in blank
hard

Fix the error in the code to create an array from 1 to 10 with step 2 using np.arange().

NumPy
import numpy as np
arr = np.arange(1, 10, [1])
print(arr)
Drag options to blanks, or click blank then click option'
A3
B1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using step 0 causes an error.
Using step 1 gives all numbers, not skipping.
4fill in blank
hard

Fill both blanks to create an array from 10 down to 2 (excluding 2) with step -2 using np.arange().

NumPy
import numpy as np
arr = np.arange([1], [2], -2)
print(arr)
Drag options to blanks, or click blank then click option'
A10
B2
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using stop greater than start with negative step results in empty array.
Confusing stop value inclusion.
5fill in blank
hard

Fill all three blanks to create a NumPy array of even numbers from 0 to 8 using np.arange().

NumPy
import numpy as np
arr = np.arange([1], [2], [3])
print(arr)
Drag options to blanks, or click blank then click option'
A0
B10
C2
D8
Attempts:
3 left
💡 Hint
Common Mistakes
Using stop as 8 excludes 8 itself.
Using step 1 gives all numbers, not just even.

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

  1. Step 1: Understand default start value

    If only one number is given, np.arange starts from 0 by default.
  2. Step 2: Identify stop value exclusion

    The stop value 3 is excluded, so the array includes 0, 1, 2.
  3. Final Answer:

    [0, 1, 2] -> Option A
  4. 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

  1. Step 1: Check start, stop, and step order

    Start should be less than stop for positive step; step cannot be zero.
  2. 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.
  3. Final Answer:

    np.arange(2, 6, 1) -> Option D
  4. 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

  1. 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.
  2. Step 2: Calculate values

    Values are 1, 4 (1+3), 7 (4+3). Next would be 10 which is excluded.
  3. Final Answer:

    [1 4 7] -> Option A
  4. Quick Check:

    Start=1, step=3, stop=10 excluded gives [1,4,7] [OK]
Hint: Add step repeatedly until reaching stop (excluded) [OK]
Common Mistakes:
  • Including stop value 10
  • Adding step incorrectly
  • Confusing step with stop
4. What is wrong with this code?
import numpy as np
arr = np.arange(5, 2, 1)
print(arr)
medium
A. It raises a ValueError
B. It produces an empty array
C. It produces [5, 4, 3]
D. It produces [2, 3, 4, 5]

Solution

  1. Step 1: Analyze start, stop, and step values

    Start=5, stop=2, step=1 means counting up from 5 to less than 2, which is impossible.
  2. Step 2: Understand np.arange behavior

    Since step is positive but start > stop, no values satisfy condition, so result is empty array.
  3. Final Answer:

    It produces an empty array -> Option B
  4. Quick Check:

    Positive step with start > stop gives empty array [OK]
Hint: Positive step needs start < stop to produce values [OK]
Common Mistakes:
  • Expecting reversed array without negative step
  • Thinking it raises error
  • Confusing start and stop order
5. You want to create an array starting at 10, decreasing by 2, stopping before 0. Which np.arange() call is correct?
hard
A. np.arange(0, 10, -2)
B. np.arange(10, 0, 2)
C. np.arange(10, 0, -2)
D. np.arange(0, 10, 2)

Solution

  1. Step 1: Identify start, stop, and step for decreasing sequence

    Start=10, stop=0 (excluded), step must be negative to decrease.
  2. Step 2: Check each option

    np.arange(10, 0, -2) uses step=-2 correctly. np.arange(10, 0, 2) has positive step, so no values. Options C and D start at 0, which is not desired.
  3. Final Answer:

    np.arange(10, 0, -2) -> Option C
  4. Quick Check:

    Decreasing sequence needs negative step [OK]
Hint: Use negative step to count down in np.arange [OK]
Common Mistakes:
  • Using positive step for decreasing sequence
  • Swapping start and stop values
  • Starting at stop value instead of start
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard