Python Loop Patterns

In the last lesson, we learned about Python Nested Loops. Now let’s use loops to draw patterns and understand how rows and columns work together.

Let’s Draw with Loops

Loops are not just for counting and repeating things. They can also be used to draw patterns.

We can make triangles, pyramids, diamonds, squares, and more. All using simple loops and print statements.

It is a fun way to practice loops. And it teaches us how to control what gets printed, where, and how many times.

Let’s start with the simplest pattern.

Right Triangle of Stars in Python

This is the most basic pattern. We want to print a triangle that looks like this:

*
* *
* * *
* * * *
* * * * *

Here is the code:

rows = 5
for i in range(1, rows + 1):
for j in range(i):
print("*", end=" ")
print()

Let me explain.

  • The outer loop runs from 1 to 5. i is the current row number.
  • The inner loop runs i times. So for row 1, it runs once. For row 2, twice. And so on.
  • Inside the inner loop, we print a star and a space, but we do not go to a new line. That is what end=" " does.
  • After the inner loop finishes, we call print() with no arguments. That moves to the next line.

So for each row, we print the right number of stars. And then we move to the next line.

Inverted Right Triangle in Python

Now let’s flip it upside down.

* * * * *
* * * *
* * *
* *
*

The code:

rows = 5
for i in range(rows, 0, -1):
for j in range(i):
print("*", end=" ")
print()

Here, the outer loop starts at 5 and goes down to 1. So the first row has 5 stars, the next has 4, and so on.

Pyramid Pattern in Python

A pyramid is a triangle that is centered. Like this:

*
* *
* * *
* * * *
* * * * *

To make this, we need to print spaces before the stars.

rows = 5
for i in range(1, rows + 1):
# print leading spaces
for j in range(rows - i):
print(" ", end="")
# print stars
for k in range(i):
print("*", end=" ")
print()

Let me explain.

  • For row 1, we need 4 spaces before the star. rows - i is 5 - 1 = 4. So we print 4 spaces.
  • Then we print 1 star followed by a space.
  • For row 2, we need 3 spaces, then 2 stars. And so on.

The end="" in the space loop prevents new lines. We just print spaces.

The star loop uses end=" " so stars are separated by spaces.

After both inner loops, we call print() to move to the next line.

Number Pattern in Python

Instead of stars, we can print numbers.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Code:

rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()

Here, the inner loop runs from 1 to i. So it prints the numbers 1, 2, 3, … up to i. That gives the pattern.

We can also print the row number instead of the column number.

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Code:

rows = 5
for i in range(1, rows + 1):
for j in range(i):
print(i, end=" ")
print()

Diamond Pattern in Python

A diamond is two triangles: one going up, one going down.

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

We can build it by combining a pyramid and an inverted pyramid.

rows = 5
# Upper half
for i in range(1, rows + 1):
for j in range(rows - i):
print(" ", end="")
for k in range(i):
print("*", end=" ")
print()
# Lower half
for i in range(rows - 1, 0, -1):
for j in range(rows - i):
print(" ", end="")
for k in range(i):
print("*", end=" ")
print()

The first loop makes the top half (including the middle row). The second loop makes the bottom half, starting from rows - 1 down to 1.

Square Pattern in Python

A square is simple. Just print the same number of stars for each row.

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
size = 5
for i in range(size):
for j in range(size):
print("*", end=" ")
print()

The outer loop runs size times. The inner loop also runs size times. So we get a square.

We can also make a hollow square.

* * * * *
* *
* *
* *
* * * * *
size = 5
for i in range(size):
for j in range(size):
if i == 0 or i == size - 1 or j == 0 or j == size - 1:
print("*", end=" ")
else:
print(" ", end=" ")
print()

Here, we only print stars on the borders. The inside is empty spaces.

Why Practice Patterns in Python?

Patterns are not just for fun. They teach us:

  • How nested loops work
  • How to control output with end and print()
  • How to think about rows and columns
  • How to break a problem into smaller steps

Many job interviews ask us to print patterns. So it is good practice.

We can try changing the number of rows. Try different characters. Try combining patterns. The more we play, the better we understand loops.

Next, we will learn about Python Functions and group reusable instructions under clear names.