How to generate a rectangle pattern using numbers in Python
In this shot, we will discuss how to generate a rectangle pattern using numbers in Python.
Numerous patterns can be printed using Python once we have a strong grasp on concepts involving loops. Here, we will be using simple for loops to generate a rectangle pattern using numbers.
Description
To execute a rectangular pattern using Python programming, we will be using two for loops, one outer and one nested loop:
- Outer loop: This is used to iterate over the number of rows.
- Inner nested loop: This is used to iterate over the number of columns in each row.
Code
Let’s look at the below code snippet.
# 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 Patternprint(i+1, end = ' ')print()
Explanation
- In line 2, we have taken the input for the number of rows.
- In line 3, we have taken the input for the number of columns.
- In line 6, we have created a
forloop to iterate through the number of rows. - In line 9, we have created a
forloop to iterate through the number of columns. - In line 12,
i+1has been used so as to start the pattern from 1.iincreases with the increase in row number.