Learning Python loop patterns is a fun and essential step for every beginner who wants to master programming logic. In this tutorial, we will explore how to create star patterns and number patterns using for
loops and while
loops in Python. These examples help you understand nested loops and how to control output with proper indentation and iteration.

1. What Are Python Loop Patterns?
Loop patterns in Python are visual structures printed in the console, often using stars (*
), numbers, or characters. They are created using nested loops — a loop inside another loop — to control rows and columns of printed characters. Loop patterns are excellent exercises for improving your logic and problem-solving skills.
2. Basic Concepts You Should Know
- For Loop: Used when the number of iterations is known.
- While Loop: Used when the number of iterations depends on a condition.
- Range Function: Generates a sequence of numbers, often used with
for
loops. - End Parameter: Controls what is printed after each iteration (default is newline).
3. Star Patterns Using For Loop
Example 1: Simple Right Triangle Star Pattern
This is one of the easiest patterns to print in Python. It helps you understand how nested loops work.
rows = 5 for i in range(1, rows + 1): for j in range(i): print("*", end="") print()
Output:
* ** *** **** *****
Explanation:
The outer loop controls the number of rows, while the inner loop prints the stars in each row. The end=""
argument keeps the output on the same line.
Example 2: Inverted Right Triangle Pattern
rows = 5 for i in range(rows, 0, -1): for j in range(i): print("*", end="") print()
Output:
***** **** *** ** *
Explanation:
We reverse the range in the outer loop using range(rows, 0, -1)
to decrease the number of stars printed in each row.
Example 3: Pyramid Star Pattern
rows = 5 for i in range(rows): print(" " * (rows - i - 1) + "*" * (2 * i + 1))
Output:
* *** ***** ******* *********
Explanation:
This pattern combines spaces and stars to form a centered pyramid. The key idea is to print spaces before the stars to align them properly.
Example 4: Diamond Star Pattern
rows = 5 for i in range(rows): print(" " * (rows - i - 1) + "*" * (2 * i + 1)) for i in range(rows - 2, -1, -1): print(" " * (rows - i - 1) + "*" * (2 * i + 1))
Output:
* *** ***** ******* ********* ******* ***** *** *
Explanation:
The diamond shape is made by combining an upward pyramid and an inverted pyramid. The first loop prints the top half, and the second loop prints the bottom half.
4. Number Patterns Using For Loop
Example 5: Increasing Number Triangle
rows = 5 for i in range(1, rows + 1): for j in range(1, i + 1): print(j, end=" ") print()
Output:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Example 6: Repeating Number Pattern
rows = 5 for i in range(1, rows + 1): for j in range(i): print(i, end=" ") print()
Output:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Example 7: Inverted Number Triangle
rows = 5 for i in range(rows, 0, -1): for j in range(1, i + 1): print(j, end=" ") print()
Output:
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Example 8: Number Pyramid Pattern
rows = 5 for i in range(1, rows + 1): print(" " * (rows - i), end="") for j in range(1, i + 1): print(j, end=" ") print()
Output:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
5. Using While Loop for the Same Patterns
While loops can also be used for printing patterns. Let’s take a simple star example using while
loop.
rows = 5 i = 1 while i <= rows: j = 1 while j <= i: print("*", end="") j += 1 print() i += 1
Output:
* ** *** **** *****
6. Why Learn Loop Patterns in Python?
Practicing pattern programs helps you:
- Understand nested loops in depth.
- Improve logical thinking and algorithm design skills.
- Strengthen your Python fundamentals for interviews and tests.
- Enhance problem-solving with real-world coding scenarios.
7. Real-Life Application Example
Let’s use a simple example where loop logic can be applied in a practical case, like generating seat numbers for a classroom.
rows = 5 columns = 4 seat_number = 1 for i in range(rows): for j in range(columns): print(f"Seat-{seat_number}", end="\t") seat_number += 1 print()
Output:
Seat-1 Seat-2 Seat-3 Seat-4 Seat-5 Seat-6 Seat-7 Seat-8 Seat-9 Seat-10 Seat-11 Seat-12 Seat-13 Seat-14 Seat-15 Seat-16 Seat-17 Seat-18 Seat-19 Seat-20
Conclusion:
Practicing Python loop patterns such as star and number designs is a great way to strengthen your understanding of loops, conditions, and logic flow. Once you master these basics, you’ll find it easier to handle more complex algorithms, data structures, and real-world automation tasks.
To explore more about Python programming, you can visit the official documentation:
https://docs.python.org/3/tutorial/index.html