Bird
Raised Fist0
NumPydata~20 mins

Why array creation matters in NumPy - Challenge Your Understanding

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 array creation with shape and dtype
What is the output of this code snippet creating a numpy array with zeros, shape (2,3), and dtype int?
NumPy
import numpy as np
arr = np.zeros((2,3), dtype=int)
print(arr)
A
[[0 0 0]
 [0 0 0]]
B
[[0. 0. 0.]
 [0. 0. 0.]]
C
[[0 0]
 [0 0]
 [0 0]]
D[0 0 0 0 0 0]
Attempts:
2 left
💡 Hint
Remember that np.zeros creates an array filled with zeros of the given shape and dtype.
❓ data_output
intermediate
2:00remaining
Shape and size of numpy array created with arange and reshape
What are the shape and size of the numpy array created by this code?
NumPy
import numpy as np
arr = np.arange(12).reshape(3,4)
print(arr.shape, arr.size)
A(3, 4) 7
B(4, 3) 12
C(12,) 3
D(3, 4) 12
Attempts:
2 left
💡 Hint
The reshape changes the shape but not the total number of elements.
🔧 Debug
advanced
2:00remaining
Identify the error in numpy array creation
What error does this code raise when trying to create a numpy array?
NumPy
import numpy as np
arr = np.array([1, 2, 3], dtype='float32', shape=(2,2))
AValueError: cannot reshape array of size 3 into shape (2,2)
BSyntaxError: invalid syntax
CTypeError: array() got an unexpected keyword argument 'shape'
DNo error, array created successfully
Attempts:
2 left
💡 Hint
Check the parameters accepted by np.array function.
🚀 Application
advanced
2:00remaining
Choosing the right numpy array creation method for memory efficiency
You want to create a large array of zeros with shape (10000, 10000) efficiently. Which method is best?
Anp.zeros((10000, 10000))
Bnp.empty((10000, 10000))
Cnp.ones((10000, 10000)) * 0
Dnp.array([[0]*10000]*10000)
Attempts:
2 left
💡 Hint
Consider memory allocation and initialization speed.
🧠 Conceptual
expert
3:00remaining
Why does numpy array creation method affect computation speed?
Which statement best explains why the method of creating a numpy array affects the speed of later computations?
AArrays created with np.empty are faster because they skip initialization, reducing setup time before computations.
BArrays created with np.array are always slower because they copy data from Python lists.
CArrays created with np.ones are faster because they are stored in contiguous memory blocks.
DArrays created with np.zeros are slower because they use integer type by default, which is slower than float.
Attempts:
2 left
💡 Hint
Think about what happens when memory is allocated and initialized.

Practice

(1/5)
1. Why is it important to use np.array() to create arrays in NumPy instead of just using Python lists?
easy
A. Because NumPy arrays allow fast math operations on many numbers at once.
B. Because Python lists cannot store numbers.
C. Because np.array() creates strings, not numbers.
D. Because Python lists are faster than NumPy arrays.

Solution

  1. Step 1: Understand Python lists vs NumPy arrays

    Python lists store items but are slow for math on many numbers.
  2. Step 2: Recognize NumPy arrays' advantage

    NumPy arrays allow fast, efficient math on large sets of numbers.
  3. Final Answer:

    Because NumPy arrays allow fast math operations on many numbers at once. -> Option A
  4. Quick Check:

    NumPy arrays speed up math = A [OK]
Hint: NumPy arrays speed math on many numbers, lists do not [OK]
Common Mistakes:
  • Thinking Python lists can't store numbers
  • Confusing array creation with string creation
  • Believing lists are faster for math
2. Which of the following is the correct way to create a NumPy array from a Python list [1, 2, 3]?
easy
A. np.array{1, 2, 3}
B. array([1, 2, 3])
C. np.list([1, 2, 3])
D. np.array([1, 2, 3])

Solution

  1. Step 1: Recall correct NumPy array syntax

    The function to create arrays is np.array() with parentheses and a list inside.
  2. Step 2: Check each option

    np.array([1, 2, 3]) uses correct syntax. array([1, 2, 3]) misses np.. np.list([1, 2, 3]) uses wrong function. np.array{1, 2, 3} uses braces instead of parentheses.
  3. Final Answer:

    np.array([1, 2, 3]) -> Option D
  4. Quick Check:

    Correct function and syntax = A [OK]
Hint: Use np.array() with parentheses and list inside [OK]
Common Mistakes:
  • Omitting np. prefix
  • Using curly braces instead of parentheses
  • Using wrong function names
3. What will be the output of this code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)
medium
A. (4,)
B. (1, 4)
C. (2, 2)
D. Error

Solution

  1. Step 1: Understand the array structure

    The array is created from a list of two lists, each with 2 elements, so it is 2 rows and 2 columns.
  2. Step 2: Check the shape attribute

    arr.shape returns the dimensions as a tuple (rows, columns), which is (2, 2).
  3. Final Answer:

    (2, 2) -> Option C
  4. Quick Check:

    2 rows and 2 columns shape = (2, 2) [OK]
Hint: Shape shows rows and columns count as tuple [OK]
Common Mistakes:
  • Confusing shape with size
  • Expecting a flat tuple for nested lists
  • Thinking shape returns error
4. Find the error in this code that tries to create a NumPy array:
import numpy as np
arr = np.array(1, 2, 3)
print(arr)
medium
A. The print statement is missing parentheses.
B. np.array() needs a single list or array argument, not multiple separate arguments.
C. np.array() cannot create arrays from numbers.
D. The import statement is incorrect.

Solution

  1. Step 1: Check np.array() argument format

    np.array() expects one argument, usually a list or array, not multiple separate values.
  2. Step 2: Identify the error in the code

    The code passes three separate arguments instead of one list, causing a TypeError.
  3. Final Answer:

    np.array() needs a single list or array argument, not multiple separate arguments. -> Option B
  4. Quick Check:

    Single argument list required = C [OK]
Hint: np.array() takes one list or array argument [OK]
Common Mistakes:
  • Passing multiple arguments instead of one list
  • Thinking np.array() can't take numbers
  • Misreading print syntax
5. You have a list of lists representing daily temperatures for a week:
temps = [[20, 21, 19], [22, 20, 18], [19, 20, 21], [21, 22, 20], [20, 19, 22], [18, 20, 21], [19, 21, 20]]

How can you create a NumPy array from this list and find the average temperature for each day?
hard
A. "import numpy as np\ntemps_array = np.array(temps)\ndaily_avg = temps_array.mean(axis=1)\nprint(daily_avg)"
B. "import numpy as np\ntemps_array = np.array(temps)\ndaily_avg = temps_array.mean(axis=0)\nprint(daily_avg)"
C. "import numpy as np\ntemps_array = np.array(temps)\ndaily_avg = temps_array.sum(axis=1)\nprint(daily_avg)"
D. "import numpy as np\ntemps_array = np.array(temps)\ndaily_avg = temps_array.sum(axis=0)\nprint(daily_avg)"

Solution

  1. Step 1: Create NumPy array from list of lists

    Use np.array(temps) to convert the list of daily temperatures into a 2D array.
  2. Step 2: Calculate average temperature per day

    Use mean(axis=1) to average across each row (each day), since rows represent days.
  3. Final Answer:

    import numpy as np temps_array = np.array(temps) daily_avg = temps_array.mean(axis=1) print(daily_avg) -> Option A
  4. Quick Check:

    Mean per row (day) = D [OK]
Hint: Use mean(axis=1) to average rows (days) in 2D array [OK]
Common Mistakes:
  • Using mean(axis=0) which averages columns (times)
  • Using sum instead of mean
  • Not converting list to array first
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