0
0
Data Analysis Pythondata~20 mins

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

Choose your learning style9 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.