It creates a new array from a list or sequence of numbers. This array can be used for fast math and data operations.
Click to reveal answer
beginner
How does numpy.arange() differ from Python's built-in range()?
numpy.arange() can create arrays with decimal steps, not just whole numbers like range(). It returns a NumPy array instead of a list.
Click to reveal answer
beginner
What is the purpose of numpy.linspace()?
It creates an array of evenly spaced numbers between a start and stop value, including the stop value. You specify how many numbers you want.
Click to reveal answer
intermediate
Example: What does np.arange(1, 5, 0.5) produce?
It creates an array starting at 1, increasing by 0.5, up to but not including 5: [1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5]
Click to reveal answer
intermediate
Why might you choose linspace() over arange()?
Because linspace() lets you specify the exact number of points you want between start and stop, which is helpful for precise spacing. arange() uses step size, which can cause rounding issues.
Click to reveal answer
Which function creates an array with a specific number of evenly spaced points between two values?
Anumpy.array()
Bnumpy.linspace()
Cnumpy.arange()
DPython range()
✗ Incorrect
numpy.linspace() creates a fixed number of points evenly spaced between start and stop.
What will np.arange(0, 3, 1) output?
A[0, 1, 2]
B[1, 2, 3]
C[0, 1, 2, 3]
D[0, 1, 2, 3, 4]
✗ Incorrect
np.arange() includes start but excludes stop, so it stops before 3.
Which function converts a Python list into a NumPy array?
Anumpy.array()
Bnumpy.arange()
Cnumpy.tolist()
Dnumpy.linspace()
✗ Incorrect
numpy.array() takes a list and returns a NumPy array.
If you want 5 numbers evenly spaced from 0 to 1, which function and parameters would you use?
Anp.array([0, 0.2, 0.4, 0.6, 0.8])
Bnp.arange(0, 1, 0.2)
Cnp.linspace(0, 1, 5)
Dnp.arange(0, 1, 0.25)
✗ Incorrect
np.linspace(0, 1, 5) creates exactly 5 points evenly spaced between 0 and 1.
What type of object do numpy.array(), arange(), and linspace() return?
APython list
BDictionary
CTuple
DNumPy array
✗ Incorrect
All these functions return a NumPy array, which is optimized for math and data tasks.
Explain how to create a NumPy array from a Python list and why you might want to do this.
Think about turning simple lists into powerful arrays for calculations.
You got /4 concepts.
Describe the difference between arange() and linspace() and when to use each.
Focus on how you control the spacing and number of points.
You got /5 concepts.
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
Step 1: Understand np.arange parameters
np.arange(start, stop) creates values from start up to but not including stop.
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].
Final Answer:
An array with values [1, 2, 3, 4] -> Option D
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
Step 1: Recall np.array syntax
np.array() takes a single list or iterable as input to create an array.
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.
Final Answer:
np.array([10, 20, 30]) -> Option A
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
Step 1: Understand np.linspace parameters
np.linspace(start, stop, num) creates num evenly spaced points from start to stop inclusive.
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.
Final Answer:
[0. 0.25 0.5 0.75 1. ] -> Option C
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
Step 1: Check np.arange with float steps
NumPy's arange supports float step values, so 3.5 is valid.
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.
Final Answer:
The code runs correctly and prints [0. 3.5 7.] -> Option B