w3resource logo

:

Python Exercises

Python NumPy: Create a 8x8 matrix and fill it with a checkerboard pattern

Python NumPy: Array Object Exercise-10 with Solution

Write a Python program to create a 8x8 matrix and fill it with a checkerboard pattern.

Sample Solution:-

Python Code:

import numpy as np
x = np.ones((3,3))
print("Checkerboard pattern:")
x = np.zeros((8,8),dtype=int)
x[1::2,::2] = 1
x[::2,1::2] = 1
print(x)

Sample Output:

Checkerboard pattern:                                                   
[[0 1 0 1 0 1 0 1]                                                      
 [1 0 1 0 1 0 1 0]                                                      
 [0 1 0 1 0 1 0 1]                                                      
 [1 0 1 0 1 0 1 0]                                                      
 [0 1 0 1 0 1 0 1]                                                      
 [1 0 1 0 1 0 1 0]                                                      
 [0 1 0 1 0 1 0 1]                                                      
 [1 0 1 0 1 0 1 0]]

Python Code Editor:

Improve this sample solution and post your code through Disqus