Given n, print the checkboard pattern for a n x n matrix
Checkboard Pattern for n = 8:
It consists of n * n squares of alternating 0 for white and 1 for black.
We can do the same using nested for loops and some if conditions, but using Python’s numpy library, we can import a 2-D matrix and get the checkboard pattern using slicing.
W2’ll be using following python function to print pattern :
Using this function, we initialize a 2-D matrix with 0’s at all index using numpy
- x[1::2, ::2] = 1 : Slice from 1st index row till 1+2+2… and fill all columns with 1 starting from 0th to 0+2+2… and so on.
- x[::2, 1::2] = 1 : Slice from 0th row till 0+2+2… and fill all columns with 1 starting from 1 to 1+2+2+…..
Function of np.zeros((n, n), dtype=int) : Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. Using the dtype parameter initializes all the values with int data-type.
For example: np.zeros, np.ones etc.
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 Program to print digit pattern
- Python program to print Emojis
- Python Program to Print Numbers in an Interval
- Python program to print all odd numbers in a range
- Python program to print even numbers in a list
- Python program to print odd numbers in a List
- Python program to print calendar of given year
- Python program to print all even numbers in a range
- Python program to print even length words in a string
- Python program to print all negative numbers in a range
- Python program to print all Prime numbers in an Interval
- Python program to print all Strong numbers in given list
- Python | Program to print duplicates from a list of integers
- Python program to print the substrings that are prefix of the given string
- Python program to print positive numbers in a list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.