Last updated: Apr 11, 2024
Reading time·4 min
To use nested loops to print a rectangle:
for
loop to iterate over a range
object of length N rows.for
loop to iterate over a range
object of length N columns.num_rows = 2 num_cols = 3 for i in range(num_rows): for i in range(num_cols): print('*', end=' ') print('')
The num_rows
variable stores the number of rows the rectangle should have and
the num_cols
variable stores the number of columns.
We used a for loop to iterate over a range
object of length N rows.
The range() class is commonly used for looping a specific number of times in for loops
If you only pass a single argument to the range()
constructor, it is
considered to be the value for the stop
parameter.
print(list(range(3))) # [0, 1, 2] print(list(range(5))) # [0, 1, 2, 3, 4]
The range starts at 0
and goes up to, but not including the specified number.
On each iteration of the outer loop, we use a nested loop to iterate over a
range
object of length N columns.
Notice that we used the same name for the variable in the outer and inner loops.
num_rows = 2 num_cols = 3 for i in range(num_rows): for i in range(num_cols): print('*', end=' ') print('')
We used the print()
function to print an asterisk for each column and set the
end
argument to a space.
The end
argument is printed at the end of the message.
By default, end
is set to a newline character (\n
).
end
argument has to be set to a space, so we can print multiple asterisks on the same row (one for each column).The last step is to print an empty string so that the next asterisk is printed on the next line.
You can also extract the logic into a reusable function.
def print_rectangle(num_rows, num_cols): for _i in range(num_rows): for _i in range(num_cols): print('*', end=' ') print('') print_rectangle(2, 3) print('x' * 50) print_rectangle(3, 4) print('x' * 50) print_rectangle(4, 5)
The print_rectangle
function takes the number of rows and the number of
columns as parameters and prints a rectangle with the specified rows and
columns.
You can also use an alternative, more complex approach to print a rectangle with nested loops.
for
loop to iterate over a range
object of length N rows.for
loop to iterate over a range
object of length N
columns - 1.num_rows = 2 num_cols = 3 for i in range(num_rows): print('*', end=' ') for j in range(num_cols-1): i *= j print('*', end=' ') print('')
On each iteration of the outer for
loop, we print an asterisk.
num_rows = 2 num_cols = 3 for i in range(num_rows): print('*', end=' ') for j in range(num_cols-1): i *= j print('*', end=' ') print('')
The end
argument is printed at the end of the message.
We set the argument to a space to be able to print multiple asterisks on the same row.
By default, end
is set to a newline character (\n
).
The next step is to use a nested for
loop to iterate for the columns.
Notice that we subtract 1
from the number of columns when calling the
range()
class.
*
for the first column is already printed by the outer for
loop.On each iteration, we set the value of i
to be equal to the result of
multiplying i
by j
.
The nested for
loop also prints an asterisk for each column.
The last step is to print an empty string so that the next row starts on a new line.
The default behavior of the print()
function is that consecutive calls are
displayed on a new line but we set the end
argument to a space to be able to
display multiple columns on the same row.
You can also create a reusable function that uses nested loops to print a rectangle.
def print_rectangle(num_rows, num_cols): for i in range(num_rows): print('*', end=' ') for j in range(num_cols-1): i *= j print('*', end=' ') print('') print_rectangle(2, 3) print('x' * 50) print_rectangle(3, 4) print('x' * 50) print_rectangle(4, 5)
The print_rectangle
function takes the number of rows and the number of
columns as parameters and prints a rectangle with the specified rows and
columns.
You can learn more about the related topics by checking out the following tutorials: