A NumPy array is a grid of values, all of the same type, indexed by a tuple of nonnegative integers. It is like a list but faster and can handle large data efficiently.
Click to reveal answer
beginner
How do you create a NumPy array from a Python list?
Use numpy.array() function. For example, np.array([1, 2, 3]) creates a 1D array with elements 1, 2, and 3.
Click to reveal answer
beginner
What is the difference between a Python list and a NumPy array?
Python lists can hold different data types and are slower. NumPy arrays hold elements of the same type and are optimized for numerical operations, making them faster and more memory efficient.
Click to reveal answer
beginner
What does the shape attribute of a NumPy array tell you?
The shape attribute shows the size of the array in each dimension. For example, a shape of (3, 4) means 3 rows and 4 columns.
Click to reveal answer
beginner
How can you access the element in the second row and third column of a 2D NumPy array?
Use zero-based indexing: array[1, 2] accesses the element in the second row and third column.
Click to reveal answer
Which function creates a NumPy array from a list?
Anp.create()
Bnp.list()
Cnp.array()
Dnp.make_array()
✗ Incorrect
The correct function to create a NumPy array from a list is np.array().
What is the data type requirement for elements in a NumPy array?
AElements can be of any type
BOnly strings are allowed
COnly integers are allowed
DAll elements must be of the same type
✗ Incorrect
NumPy arrays require all elements to be of the same data type for efficient computation.
If a NumPy array has shape (4, 5), how many elements does it contain?
A20
B9
C10
D5
✗ Incorrect
The total elements are rows × columns = 4 × 5 = 20.
How do you access the first element of a 1D NumPy array named arr?
Aarr[0]
Barr[1]
Carr.first()
Darr.get(0)
✗ Incorrect
NumPy arrays use zero-based indexing, so the first element is accessed with arr[0].
Which of these is NOT a benefit of using NumPy arrays over Python lists?
AFaster numerical operations
BSupports mixed data types
CUses less memory
DSupports multi-dimensional arrays
✗ Incorrect
NumPy arrays do NOT support mixed data types; all elements must be the same type.
Explain what a NumPy array is and why it is useful compared to a Python list.
Think about how computers handle numbers and speed.
You got /5 concepts.
Describe how to create a 2D NumPy array and how to find its shape.
Use nested lists and check the size with a property.
You got /4 concepts.
Practice
(1/5)
1. What is a key advantage of using a NumPy array over a regular Python list for storing numbers?
easy
A. NumPy arrays automatically sort the data
B. NumPy arrays can store different data types in the same array
C. NumPy arrays use less memory and allow faster math operations
D. NumPy arrays are slower but more flexible than lists
Solution
Step 1: Understand memory and speed benefits
NumPy arrays store data in a compact way and use optimized code for math.
Step 2: Compare with Python lists
Python lists store references and are slower for math operations.
Final Answer:
NumPy arrays use less memory and allow faster math operations -> Option C
Quick Check:
NumPy arrays = efficient storage + fast math [OK]
Hint: NumPy arrays are built for fast math and memory efficiency [OK]
Common Mistakes:
Thinking NumPy arrays can store mixed data types like lists
Assuming NumPy arrays sort data automatically
Believing NumPy arrays are slower than lists
2. Which of the following is the correct way to create a 1-dimensional NumPy array with elements 1, 2, and 3?
easy
A. np.array(1, 2, 3)
B. np.array([1, 2, 3])
C. np.array{1, 2, 3}
D. np.array([1; 2; 3])
Solution
Step 1: Recall correct syntax for np.array
NumPy arrays are created by passing a list inside parentheses: np.array([elements]).
Step 2: Check each option
np.array([1, 2, 3]) uses correct syntax with square brackets inside parentheses. Others have syntax errors.
Final Answer:
np.array([1, 2, 3]) -> Option B
Quick Check:
np.array needs a list inside parentheses [OK]
Hint: Use square brackets inside parentheses to create arrays [OK]