Python Pattern Programs using While Loop
In this tutorial, we will learn how to print star and number patterns using Python While Loop. Pattern programs are useful for understanding nested loops because each printed row usually requires one loop for the rows and another loop for the characters or numbers within that row.
We will start with right-triangle and inverted-triangle star patterns, then print a number pattern. Additional examples show how the same nested while loop approach can be used for square and centered pyramid patterns.
How nested while loops print patterns in Python
Most pattern programs use two loops. The outer while loop controls the number of rows. The inner while loop controls how many stars, numbers, or spaces are printed in the current row.
A common structure for a pattern with n rows is:
i = 1
while i <= n:
j = 1
while j <= items_in_this_row:
# print one item
j += 1
print()
i += 1
The inner loop normally prints on the same line by using end=" ". After that loop finishes, print() moves the cursor to the next line. Both loop-control variables must be updated so that the loops eventually stop.
Example 1 – Python Program to Print Right Triangle using While Loop
In this example, we will write a Python program to print the following start pattern to console. We shall read the number of rows and print starts as shown below.
Pattern
For an input number of 4, following would be the pattern.
*
* *
* * *
* * * *
Python Program
n = int(input('Enter number of rows : '))
i = 1
while i <= n :
j = 1
while j <= i:
print("*", end = " ")
j += 1
print()
i += 1
Inner while loop prints a single row after its complete execution. Outer while loop helps to print n number of rows.
In other words, outer while loop prints the rows, while inner while loop prints columns in each row.
For row i, the inner condition j <= i prints exactly i stars. Therefore, the first row contains one star, the second contains two stars, and so on.
Output
Enter number of rows : 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
Example 2 – Python Program to Print Inverted Right Triangle using While Loop
In this example, we will write a Python program to print the following start pattern to console.
Pattern
For an input number of 4, following would be the pattern.
* * * *
* * *
* *
*
Python Program
n = int(input('Enter number of rows : '))
i = 1
while i <= n :
j = n
while j >= i:
print("*", end = " ")
j -= 1
print()
i += 1
This time, the inner loop starts j at n for each row and decreases it until j is smaller than i. As i increases, the number of stars printed in each row decreases by one.
Output
Enter number of rows : 5
* * * * *
* * * *
* * *
* *
*
Example 3 – Python Program to Print Number Pattern using While Loop
In this example, we will write a Python program to print the following pattern to console. We shall read the number of rows and print numbers as shown below.
Pattern
For an input number of 5, following would be the pattern.
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Python Program
n = int(input('Enter number of rows : '))
k = 1
i = 1
while i <= n :
j = 1
while j <= i:
print("{:3d}".format(k), end = " ")
j += 1
k += 1
print()
i += 1
The variables have separate roles in this program. i tracks the current row, j counts the values printed in that row, and k stores the next number to display. Because k is not reset at the beginning of every row, the numbers continue sequentially across the whole pattern.
The format expression {:3d} gives each integer a field width of three characters, helping single-digit and double-digit values remain aligned.
Output
Enter number of rows : 7
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
Python square star pattern using while loop
A square pattern is a useful variation because every row contains the same number of stars. If the input is 4, both the number of rows and the number of stars in each row are four.
n = int(input("Enter number of rows : "))
i = 1
while i <= n:
j = 1
while j <= n:
print("*", end=" ")
j += 1
print()
i += 1
For n = 4, the program prints:
* * * *
* * * *
* * * *
* * * *
The outer loop runs four times. On every iteration, the inner loop also runs four times, so each row has the same width.
Python centered pyramid star pattern using while loop
A centered pyramid requires both spaces and stars. For row i, the program first prints leading spaces and then prints 2 * i - 1 stars.
n = int(input("Enter number of rows : "))
i = 1
while i <= n:
spaces = n - i
while spaces > 0:
print(" ", end="")
spaces -= 1
stars = 1
while stars <= 2 * i - 1:
print("*", end="")
stars += 1
print()
i += 1
For n = 4, the output is:
*
***
*****
*******
The number of leading spaces decreases by one in each new row, while the number of stars increases by two. This produces the centered pyramid shape.
Python repeated-number triangle using while loop
Pattern programs do not have to use stars. The inner loop can print the current row number instead. The following program prints each row number as many times as the row number itself.
n = 5
i = 1
while i <= n:
j = 1
while j <= i:
print(i, end=" ")
j += 1
print()
i += 1
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
The loop structure is almost the same as the right-triangle star example. Only the value passed to print() changes from "*" to i.
How to change a Python while-loop pattern
Once the row and column roles are clear, many patterns can be created by changing the inner-loop condition or the value being printed.
- Increase items by row: use a condition such as
j <= i. - Decrease items by row: make the inner loop depend on both
nandi. - Print a fixed-width pattern: let the inner loop run from
1throughnfor every row. - Print numbers instead of stars: replace the string passed to
print()with a row, column, or separate counter variable. - Center a pattern: print leading spaces before printing the visible characters in each row.
Common mistakes in Python pattern programs using while loops
- Not resetting the inner-loop counter: initialize
jinside the outer loop when every row needs a fresh column count. - Forgetting to increment or decrement a counter: a
whilecondition may remain true indefinitely if its control variable never changes. - Using
print()at the wrong indentation level: the emptyprint()that creates a new line should normally run after the inner loop, not during every inner iteration. - Using the wrong inner-loop limit:
j <= icreates a growing triangle, whilej <= ncreates the same number of items in every row. - Resetting a sequence counter too early: for continuously increasing number patterns, keep the sequence variable outside the outer loop.
What to remember about Python pattern programs using while loops
In this Python Tutorial, we learned to write Python programs to print different types of patterns using While Loop.
The main idea is to treat a pattern as rows and items within those rows. The outer while loop controls the rows, while one or more inner loops control stars, numbers, and spaces. By changing the inner-loop limits and printed values, the same structure can produce right triangles, inverted triangles, squares, pyramids, and number patterns.
TutorialKart.com