NumPy Assignment– 2

Array Indexing & Slicing

Basic Questions

  1. Create a 1D array of numbers 0–9 and print the 3rd element using indexing.
  2. From a 1D array of numbers 10–19, slice and print elements from index 2 to 7.
  3. Generate a 1D array of numbers 0–20. Print elements from index 5 to the end.
  4. From the same array, print the first 5 elements using slicing.
  5. Demonstrate negative indexing: print the last element of a 1D array from 1–10.
  6. Slice with negative indices: print the last 3 elements of a 1D array from 1–10.
  7. Create a 2D 3×3 array with numbers 1–9. Print the element at row 1, column 2.
  8. From the same 2D array, extract the entire first row.
  9. Extract the entire second column from the 2D array.
  10. Print the sub-matrix containing the first two rows and first two columns.
  11. Create a 3D array of shape (2, 3, 4). Print the element at index [1, 2, 3].
  12. Print the second 2D slice (all rows, all columns) from the 3D array.
  13. Slice with step: from a 1D array of numbers 0–20, print every 2nd element.
  14. From the same array, print every 3rd element starting from index 2.
  15. Demonstrate reverse slicing: print a 1D array [1,2,3,4,5] in reverse order.
  16. Create a 1D array of numbers 1–10. Extract elements greater than 5 using boolean indexing.
  17. From the same array, extract only even numbers using boolean indexing.
  18. Create a 1D array [10, 20, 30, 40, 50]. Use fancy indexing to pick elements at indices [0, 2, 4].
  19. From the same array, use fancy indexing to repeat indices [1, 1, 3].
  20. Create a 2D 4×4 array with numbers 1–16. Extract elements at positions (0,0), (1,1), (2,2), (3,3) using fancy indexing.

Intermediate Questions

  1. Create a 1D array of numbers 50–100. Slice from index 10 to 20 with step 2.
  2. From the same array, extract the last 10 elements using slicing.
  3. Generate a 2D 5×5 array of numbers 1–25. Extract the last row.
  4. From the same array, extract the first column.
  5. Extract the middle 3×3 sub-matrix.
  6. From the 5×5 array, extract alternate rows (row 0, 2, 4).
  7. Extract alternate columns (column 0, 2, 4) from the 5×5 array.
  8. From a 2D array of numbers 1–16 reshaped to 4×4, print the elements at row indices [0,2] and column indices [1,3].
  9. Create a 3D array of shape (3,3,3). Extract all elements from the second layer.
  10. From the same 3D array, print the first row of the last layer.
  11. Using step slicing, reverse every row of a 2D array of numbers 1–12 reshaped to 3×4.
  12. From the same array, reverse the entire matrix (rows and columns both).
  13. Create a 1D array of numbers 1–15. Use boolean indexing to select elements divisible by 3.
  14. From the same array, select elements between 5 and 10 using boolean indexing.
  15. Create a 2D 4×4 array with random integers 1–50. Use boolean indexing to select all values greater than 25.
  16. Create an array [5, 10, 15, 20, 25]. Use fancy indexing to rearrange it as [25, 20, 15, 10, 5].
  17. From the same array, use fancy indexing to duplicate [10, 10, 25, 25].
  18. Create a 6×6 array using ‘np.arange(36).reshape(6,6)’. Extract a checkerboard pattern of even rows and odd columns.
  19. Demonstrate fancy indexing on a 2D array: pick elements from positions (0,1), (1,2), (2,3), (3,0).
  20. Compare the result of boolean indexing vs fancy indexing by selecting odd numbers from an array of 1–10 in both ways.

Advanced Questions

  1. Create a 10×10 array with numbers 1–100. Extract the border elements (first row, last row, first column, last column).
  2. From the same 10×10 array, extract the diagonal elements using fancy indexing.
  3. From a 3D array of shape (4,4,4), extract all elements where the sum of indices (i+j+k) is even.
  4. Generate a 2D 8×8 array and extract a chessboard pattern of 0s and 1s using slicing.
  5. Create a 1D array of numbers 1–50. Extract prime numbers using boolean indexing with a helper prime-checking function.
  6. Create a 6×6 array of numbers 1–36. Extract the central 2×2 block using slicing.
  7. From the same 6×6 array, extract all rows where the first element is greater than 20.
  8. Demonstrate fancy indexing to swap two columns of a 2D array (e.g., swap col 0 and col 2).
  9. Use boolean indexing to set all negative numbers of a random 5×5 integer array (-10 to 10) to zero.
  10. Create a 2D 7×7 array. Extract upper triangular elements (above the main diagonal) using fancy indexing.