30 NumPy slicing pattern-filling exercises

30 NumPy slicing pattern-filling exercises —  crafted to strengthen your understanding of slicing and filling in NumPy arrays:


🧩 NumPy Slicing Pattern-Filling Exercises (Only Questions)

  1. Fill the diagonal of a 5x5 zero array with 1s using slicing.

  2. Create a 6x6 array and fill the border with 5s, rest with 0s.

  3. Fill only the last row of a 4x4 array with 7s.

  4. Fill every alternate column of a 5x5 array with 9s.

  5. Create a 6x6 array and fill the center 2x2 block with 8s.

  6. In a 7x7 array, fill a cross pattern (both diagonals) with 3s.

  7. Create a 5x5 array, fill all odd-indexed rows with 2s.

  8. Fill only the first column of a 6x6 array with 4s.

  9. Create an 8x8 array, fill all elements except the border with 6s.

  10. Fill the top-left 3x3 quadrant of a 6x6 array with 1s.

  11. Create a 10x10 array, fill an L-shape pattern on the left and bottom sides with 9s.

  12. Fill a checkerboard pattern (alternating 0 and 1) in an 8x8 array.

  13. Create a 7x7 array and fill all even-indexed rows and columns with 7s.

  14. In a 5x5 array, fill the second and fourth rows with 5s.

  15. Fill the middle row and middle column of a 9x9 array with 1s.

  16. In a 6x6 array, fill a square ring (excluding diagonals) with 2s.

  17. Fill the first and last two columns of an 8x8 array with 3s.

  18. Fill every 3rd row of a 9x9 array with 4s using slicing.

  19. Create a 10x10 array, fill values in a diagonal band of width 3 with 6s.

  20. Fill the lower triangle (below main diagonal) of a 5x5 array with 1s.

  21. Fill the upper triangle (above main diagonal) of a 5x5 array with 1s.

  22. Fill a centered diamond pattern in a 7x7 array with 5s using slicing logic.

  23. Fill the border of a 10x10 array with 2s and the inner 8x8 with 8s.

  24. Fill a vertical band (columns 3 to 5) in an 8x8 array with 9s.

  25. Create a 6x6 array and fill only the corners with 7s.

  26. Fill a "plus" shape in the center of a 5x5 array with 4s.

  27. Fill a horizontal stripe (rows 2 to 4) in a 7x7 array with 3s.

  28. Create a 9x9 array, fill the four quadrants with values 1, 2, 3, and 4.

  29. Fill the even-numbered columns with 6s and odd-numbered rows with 3s.

  30. Create an 8x8 array, fill all elements where row index > column index with 2s.


Let me know if you’d like more then comments and which topic

Post a Comment

Previous Post Next Post