In this shot, we will discuss how to generate a hollow rectangle pattern with numbers in Python.
Numerous patterns can be printed using Python once you have a strong grip over the concepts involving loops. Here, we will use simple for
loops to generate a rectangle pattern with numbers.
To execute a rectangular pattern in Python, we will use 2 for
loops:
Let us look at the code snippet below.
# Initialising Length and Breadthrows = 3columns = 6# Loop through number of rowsfor i in range(rows):# Loop through number of columnsfor j in range(columns):# Printing Patternif(i == 0 or i == rows - 1 or j == 0 or j == columns - 1):print(i+1, end = ' ')else:print(' ', end = ' ')print()
for
loop to iterate through the number of rows.for
loop to iterate through the number of columns.i+1
to start the pattern from 1. i
increases with an increase in row number.