Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Array creation (array, arange, linspace) in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Creation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of NumPy arange with float step
What is the output of this code snippet?
import numpy as np
result = np.arange(0, 1, 0.3)
print(result)
Data Analysis Python
import numpy as np
result = np.arange(0, 1, 0.3)
print(result)
A[0. 0.3 0.6 0.9 1.0]
B[0. 0.3 0.6 0.9 1.2]
C[0. 0.3 0.6]
D[0. 0.3 0.6 0.9]
Attempts:
2 left
💡 Hint
Remember that arange excludes the stop value and uses step increments.
❓ data_output
intermediate
1:30remaining
Number of elements in linspace with endpoint=False
How many elements does this array have?
import numpy as np
arr = np.linspace(2, 10, 5, endpoint=False)
print(len(arr))
Data Analysis Python
import numpy as np
arr = np.linspace(2, 10, 5, endpoint=False)
print(len(arr))
A6
B5
C4
D10
Attempts:
2 left
💡 Hint
The number of points is controlled by the third argument, regardless of endpoint.
❓ visualization
advanced
2:30remaining
Visual difference between arange and linspace
Which plot correctly shows the difference between np.arange(0, 1, 0.2) and np.linspace(0, 1, 6)?
Data Analysis Python
import numpy as np
import matplotlib.pyplot as plt
x1 = np.arange(0, 1, 0.2)
x2 = np.linspace(0, 1, 6)
plt.plot(x1, np.zeros_like(x1), 'o', label='arange')
plt.plot(x2, np.ones_like(x2), 'x', label='linspace')
plt.legend()
plt.show()
APlot with 5 'o' points at 0,0.2,0.4,0.6,0.8 and 6 'x' points at 0,0.2,0.4,0.6,0.8,1
BPlot with 6 'o' points at 0,0.2,0.4,0.6,0.8,1 and 5 'x' points at 0,0.25,0.5,0.75,1
CPlot with 5 'o' points at 0,0.25,0.5,0.75,1 and 6 'x' points at 0,0.2,0.4,0.6,0.8,1
DPlot with 6 'o' points at 0,0.2,0.4,0.6,0.8,1 and 6 'x' points at 0,0.2,0.4,0.6,0.8,1
Attempts:
2 left
💡 Hint
arange excludes the stop value, linspace includes it by default.
🧠 Conceptual
advanced
1:30remaining
Behavior of np.linspace with num=1
What is the output of this code?
import numpy as np
arr = np.linspace(5, 10, 1)
print(arr)
Data Analysis Python
import numpy as np
arr = np.linspace(5, 10, 1)
print(arr)
A[5.]
B[10.]
C[7.5]
D[]
Attempts:
2 left
💡 Hint
When num=1, linspace returns the start value only.
🔧 Debug
expert
1:30remaining
Identify the error in this array creation code
What error does this code raise?
import numpy as np
arr = np.arange(0, 10, 0)
print(arr)
Data Analysis Python
import numpy as np
arr = np.arange(0, 10, 0)
print(arr)
ATypeError: step must be int or float
BSyntaxError: invalid syntax
CValueError: zero step size
DNo error, prints array from 0 to 9
Attempts:
2 left
💡 Hint
Step size cannot be zero in range or arange functions.

Practice

(1/5)
1. What does the function np.arange(1, 5) produce in Python's NumPy?
easy
A. An array with values [1, 2, 3, 4, 5]
B. An array with values [1, 3, 5]
C. An array with values [0, 1, 2, 3, 4]
D. An array with values [1, 2, 3, 4]

Solution

  1. Step 1: Understand np.arange parameters

    np.arange(start, stop) creates values from start up to but not including stop.
  2. Step 2: Apply to np.arange(1, 5)

    Values start at 1 and go up to 4 (5 is excluded), so the array is [1, 2, 3, 4].
  3. Final Answer:

    An array with values [1, 2, 3, 4] -> Option D
  4. Quick Check:

    arange excludes stop = [1, 2, 3, 4] [OK]
Hint: Remember arange excludes the stop value [OK]
Common Mistakes:
  • Including the stop value in the output
  • Starting from 0 instead of the given start
  • Confusing arange with linspace
2. Which of the following is the correct syntax to create a NumPy array from the list [10, 20, 30]?
easy
A. np.array([10, 20, 30])
B. np.linspace(10, 20, 30)
C. np.arange([10, 20, 30])
D. np.array(10, 20, 30)

Solution

  1. Step 1: Recall np.array syntax

    np.array() takes a single list or iterable as input to create an array.
  2. Step 2: Check each option

    np.array([10, 20, 30]) correctly passes a list inside brackets. np.array(10, 20, 30) passes separate arguments, which is invalid. np.arange([10, 20, 30]) uses arange incorrectly. np.linspace(10, 20, 30) uses linspace which is for ranges, not lists.
  3. Final Answer:

    np.array([10, 20, 30]) -> Option A
  4. Quick Check:

    np.array needs a list inside brackets [OK]
Hint: Use brackets [] inside np.array to pass a list [OK]
Common Mistakes:
  • Passing multiple arguments instead of a list
  • Using arange or linspace to convert lists
  • Forgetting brackets around the list
3. What is the output of the following code?
import numpy as np
arr = np.linspace(0, 1, 5)
print(arr)
medium
A. [0. 0.5 1. ]
B. [0. 0.2 0.4 0.6 0.8]
C. [0. 0.25 0.5 0.75 1. ]
D. [0. 0.1 0.2 0.3 0.4]

Solution

  1. Step 1: Understand np.linspace parameters

    np.linspace(start, stop, num) creates num evenly spaced points from start to stop inclusive.
  2. Step 2: Apply to np.linspace(0, 1, 5)

    It creates 5 points between 0 and 1: 0.0, 0.25, 0.5, 0.75, 1.0.
  3. Final Answer:

    [0. 0.25 0.5 0.75 1. ] -> Option C
  4. Quick Check:

    linspace includes stop and divides evenly [OK]
Hint: linspace includes stop and divides into equal parts [OK]
Common Mistakes:
  • Confusing linspace with arange step behavior
  • Expecting stop to be excluded
  • Wrong number of points generated
4. Identify the error in the following code snippet:
import numpy as np
arr = np.arange(0, 10, 3.5)
print(arr)
medium
A. arange does not accept float step values
B. The code runs correctly and prints [0. 3.5 7.]
C. arange requires integer start and stop values
D. Missing import statement for numpy

Solution

  1. Step 1: Check np.arange with float steps

    NumPy's arange supports float step values, so 3.5 is valid.
  2. Step 2: Predict output of np.arange(0, 10, 3.5)

    Values start at 0, then 3.5, then 7.0. Next would be 10.5 which exceeds stop 10, so stops.
  3. Final Answer:

    The code runs correctly and prints [0. 3.5 7.] -> Option B
  4. Quick Check:

    arange accepts float steps and excludes stop [OK]
Hint: arange allows float steps, output excludes stop [OK]
Common Mistakes:
  • Assuming arange only works with integers
  • Expecting stop value to be included
  • Forgetting to import numpy
5. You want to create an array of exactly 4 numbers starting at 2 and ending at 5, including both ends. Which code snippet correctly does this?
hard
A. np.linspace(2, 5, 4)
B. np.arange(2, 5, 1)
C. np.arange(2, 6, 2)
D. np.linspace(2, 5, 3)

Solution

  1. Step 1: Understand the requirement

    We need 4 numbers starting at 2 and ending at 5, including both ends.
  2. Step 2: Check np.arange options

    np.arange(2, 5, 1) creates [2, 3, 4], only 3 numbers and excludes 5. np.arange(2, 6, 2) creates [2, 4], wrong number of elements and values.
  3. Step 3: Check np.linspace options

    np.linspace(2, 5, 4) creates 4 evenly spaced numbers including 2 and 5: [2.0, 3.0, 4.0, 5.0]. np.linspace(2, 5, 3) creates only 3 numbers.
  4. Final Answer:

    np.linspace(2, 5, 4) -> Option A
  5. Quick Check:

    linspace fixes number of points including ends [OK]
Hint: Use linspace to fix number of points including start and stop [OK]
Common Mistakes:
  • Using arange which excludes stop and depends on step size
  • Choosing wrong number of points in linspace
  • Confusing arange and linspace behavior
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