Python Pattern Programs Using While Loop
Python Pattern Programs Using While Loop
In this tutorial, we will learn how to print patterns to console using Python While Loop.
* *
* * * *
* * * * * *
* * * * * * * *
1 1
2 3 2 2
4 5 6 3 3 3
7 8 9 10 4 4 4 4
Pattern
*
* *
* * *
* * * *
Python Program
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.
Output
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
* * * *
* * *
* *
*
Python Program
i = 1
while i <= n :
j = n
while j >= i:
print("*", end = " ")
j -= 1
print()
i += 1
Output
Pattern
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Python Program
k = 1
i = 1
while i <= n :
j = 1
while j <= i:
print("{:3d}".format(k), end = " ")
j += 1
k += 1
print()
i += 1
Output
Conclusion
In this Python Tutorial, we learned to write Python programs to print different types of patterns using While
Loop.
Python Programming
⊩ Python Tutorial
⊩ Install Python
⊩ Python Variables
⊩ Python Comments
Control Statements
⊩ Python If
⊩ Python If Else
Python String
Functions
⊩ Python Functions
Python Collections
⊩ Python List
⊩ Python Dictionary
Advanced
⊩ Python Multithreading
Useful Resources