In this article, I show you how to Print pattern in Python. Following Python programs uses for loop, while loop, and range() function to display various patterns. In this article, We will cover the following Python pattern programs:
- Number Pattern
- Triangle Pattern with Number
- Star (*) or Asterisk Pattern
- Pyramid pattern
- Inverted pyramid pattern
- Half pyramid pattern
- Diamond Shaped Pattern
- Characters or Alphabets Pattern
- Square pattern
As of now, this article contains 30+ pattern programs. If you don’t find the pattern you are looking for, let me know by leaving a comment below I will try to provide a solution. Using the pattern programs, you can build a solid understanding of loops in Python.
The algorithm to print the pattern using for loop in Python:
We need to use two for loops to print patterns, i.e. nested loops.
There is a typical structure to print any pattern, i.e. the number of rows and columns in the pattern. Outer loop tells us the number of rows used and the inner loop tells us the column used to print pattern.
- Accept the number of rows from a user using input() function or decide the size of a pattern.
- Iterate the number of rows using outer for loop and range() function
- Next, the inner loop or nested for loop to handle the number of columns. Inner loop iteration depends on the values of the outer loop.
- Print start, number, asterisk, Pyramid, and diamond pattern using the print() function.
- Add a new line after each row, i.e. after each iteration of outer for loop so you can display pattern appropriately.
Print Number pattern in Python
I have created multiple programs that print different styles of number patterns. Let see them one by one.
Print Pyramid, Star, and Diamond pattern in Python
In this section, we will see now how to print pyramid and Star (asterisk) patterns in Python. Here we will print the following pyramid pattern with Star (asterisk).
- Half pyramid Pattern with Stars(*)
- Full pyramid Pattern with Stars
- Inverted pyramid Pattern with Stars
- Triangle pattern with Stars
- Right-angled triangle pattern with Stars
Print Alphabets and Letters pattern in python
We are using an ASCII value of a letter, so we can convert ASCII value to a character to print it on screen. let’ see now how to print Alphabets and Letters patterns in Python with examples.