In this session, I will guide you through the process of effectively printing patterns in Python by using for loop, while loop, and the range() function. By the end of this lesson, you will clearly understand how to print patterns using these techniques. This lesson contains over 35+ patterns.
This article teaches you how to print the following patterns in Python.
- Number pattern
- Triangle pattern
- Star (*) or asterisk pattern
- Pyramid pattern
- Inverted pyramid pattern
- Half pyramid pattern
- Diamond Shaped pattern
- Characters or alphabets pattern
- Square pattern
By printing different patterns, you can build a solid understanding of loops in Python. After reading this article, you can create various types of patterns.
Table of contents
- Steps to Print Pattern in Python
- Programs to print number pattern
- Pyramid pattern of numbers
- Inverted pyramid pattern of numbers
- Inverted Pyramid pattern with the same digit
- Another inverted half-pyramid pattern with a number
- Alternate numbers pattern using a while loop
- Reverse number pattern
- Reverse Pyramid of Numbers
- Another reverse number pattern
- Print reverse number from 10 to 1
- Number triangle pattern
- Pascal’s triangle pattern using numbers
- Square pattern with numbers
- Multiplication table pattern
- Pyramid pattern of stars in python
- Right triangle pyramid of Stars
- Downward half-Pyramid Pattern of Star
- Downward full Pyramid Pattern of star
- Right down mirror star Pattern
- Equilateral triangle pattern of star
- Print two pyramids of stars
- Right start pattern of star
- Left triangle pascal’s pattern
- Sandglass pattern of star
- Pant style pattern of stars
- Diamond-shaped pattern of stars
- Alphabets and letters pattern
- More miscellaneous Patterns
- Pyramid of horizontal number tables
- Double the number pattern
- Random number pattern
- Pyramid of numbers less than 10
- Pyramid of numbers up to 10
- Even number pattern
- Unique pyramid pattern of digits
- Pattern double number on each column
- Number reduction pattern
- Pant style pattern of numbers
- Pattern with a combination of numbers and stars
- Practice Problem
- Next Steps
Steps to Print Pattern in Python
Use the below steps to print a pattern in Python
- Decide the number of rows and columns
There is a typical structure to print any pattern, i.e., the number of rows and columns. We need to use two loops to print any pattern, i.e., use nested loops.
The outer loop tells us the number of rows, and the inner loop tells us the column needed to print the pattern.
Accept the number of rows from a user using theinput()
function to decide the size of a pattern. - Iterate rows
Next, write an outer loop to Iterate the number of rows using a for loop and
range()
function. - Iterate columns
Next, write the inner loop or nested loop to handle the number of columns. The internal loop iteration depends on the values of the outer loop.
- Print star or number
Use the
print()
function in each iteration of nestedfor
loop to display the symbol or number of a pattern (like a star (asterisk*
) or number). - Add a new line after each iteration of the outer loop
Add a new line using the
print()
function after each iteration of the outer loop so that the pattern display appropriately
Also, Solve:
Programs to print number pattern
I have created various programs that print different styles of number patterns. Let’s see them one by one.
Let’s see the Python program to print the following simple number pattern using a for loop.
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Program:
In this number pattern, we displayed a single digit on the first row, the next two digits on the second row, And the following three numbers on the third row, and this process will repeat till the number of rows is reached.
Note:
- The count of numbers on each row is equal to the current row number.
- Also, each number is separated by space.
- We used a nested loop to print the pattern
Pyramid pattern of numbers
Let’s see how to print the following half-pyramid pattern of numbers
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Note: In each row, every next number is incremented by 1.
Program:
Inverted pyramid pattern of numbers
An inverted pyramid is a downward pattern where numbers get reduced in each iteration, and on the last row, it shows only one number. Use reverse for loop to print this pattern.
Pattern
1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
Program
Inverted Pyramid pattern with the same digit
Pattern: –
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
Program: –
Another inverted half-pyramid pattern with a number
Pattern: –
0 1 2 3 4 5 0 1 2 3 4 0 1 2 3 0 1 2 0 1
Program
Alternate numbers pattern using a while loop
Let’s see how to use the while loop to print the number pattern.
Pattern: –
1 3 3 5 5 5 7 7 7 7 9 9 9 9 9
Program: –
Reverse number pattern
Let’s see how to display the pattern of descending order of numbers
Pattern 1: –
5 5 5 5 5 4 4 4 4 3 3 3 2 2 1
This pattern is also called as a inverted pyramid of descending numbers.
Program: –
Reverse Pyramid of Numbers
Pattern 2: –
1 2 1 3 2 1 4 3 2 1 5 4 3 2 1
Note: It is a downward increment pattern where numbers get increased in each iteration. At each row, the amount of number is equal to the current row number.
Program
Another reverse number pattern
Pattern: –
5 4 3 2 1 4 3 2 1 3 2 1 2 1 1
Program: –
Print reverse number from 10 to 1
Pattern: –
1 3 2 6 5 4 10 9 8 7
Program: –
Number triangle pattern
Let’s see how to print the right-angled triangle pattern of numbers
Pattern: –
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Program: –
Pascal’s triangle pattern using numbers
To build the pascal triangle, start with “1” at the top, then continue placing numbers below it in a triangular pattern.
Each number is the numbers directly above it added together.
Pattern:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1
Program: –
Square pattern with numbers
Pattern: –
1 2 3 4 5 2 2 3 4 5 3 3 3 4 5 4 4 4 4 5 5 5 5 5 5
Program: –
Multiplication table pattern
Pattern: –
1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64
Program: –
Pyramid pattern of stars in python
This section will see 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
Simple half pyramid pattern: –
* * * * * * * * * * * * * * *
This pattern is also known as a right angle triangle pyramid.
Program: –
Right triangle pyramid of Stars
Pattern: –
* * * * * * * * * * * * * * *
This pattern is also called as mirrored right triangle
Program: –
Alternative Solution:
Downward half-Pyramid Pattern of Star
Pattern: –
* * * * * * * * * * * * * * *
Note: We need to use the reverse nested loop to print the downward pyramid pattern of stars
Program: –
Downward full Pyramid Pattern of star
Let’s see how to print reversed pyramid pattern in Python.
Pattern: –
* * * * * * * * * * * * * * * * * * * * *
Program:
Right down mirror star Pattern
Pattern: –
***** **** *** ** *
In this pattern, we need to use two nested while
loops.
Program: –
Equilateral triangle pattern of star
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Program: –
Print two pyramids of stars
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Program: –
Right start pattern of star
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * *
We also call this pattern as a right pascal’s triangle.
Program: –
Left triangle pascal’s pattern
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * *
Program: –
Sandglass pattern of star
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
To print this pattern we need to use two set of three while loops.
Program: –
Pant style pattern of stars
Pattern: –
**************** *******__******* ******____****** *****______***** ****________**** ***__________*** **____________** *______________*
Program: –
Diamond-shaped pattern of stars
Pattern: –
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Program: –
Another diamond pattern of star
Pattern: –
* * * * * * * * * * * * * * * *
Program: –
Alphabets and letters pattern
In Python, there are ASCII values for each letter. To print the patterns of letters and alphabets, we need to convert them to their ASCII values.
- Decide the number of rows
- Start with ASCII number 65 ( ‘A’)
- Iterate a loop and in nested
for
loop use thechar
function to convert ASCII number to its equivalent letter.
Let’ see now how to print alphabets and letters patterns in Python.
Pattern: –
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \
This pattern is knows as right-angled pattern with characters.
Program: –
Pattern to display letter of the word
Let’s see how to print word ‘Python’ in Pattern: –
P Py Pyt Pyth Pytho Python
Program: –
Equilateral triangle pattern of characters/alphabets
Pattern: –
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \
Program: –
Pattern of same character
Pattern: –
V V V V V V V V V V V V V V V
Program: –
Let’s see some more miscellaneous patterns
More miscellaneous Patterns
Pyramid of horizontal number tables
Pattern: –
1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
Program: –
Double the number pattern
Pattern: –
1 2 1 4 2 1 8 4 2 1 16 8 4 2 1 32 16 8 4 2 1 64 32 16 8 4 2 1 128 64 32 16 8 4 2 1
Note: In each column, every number is double it’s the preceding number.
Program: –
Random number pattern
1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 16 8 4 2 1 1 2 4 8 16 32 64 32 16 8 4 2 1 1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Program: –
Pyramid of numbers less than 10
Pattern: –
1 2 3 4 5 6 7 8 9
Program: –
Pyramid of numbers up to 10
Pattern: –
1 2 3 4 5 6 7 8 9 10
Program: –
Even number pattern
Pattern: –
10 10 8 10 8 6 10 8 6 4 10 8 6 4 2
Programs: –
Unique pyramid pattern of digits
Pattern: –
1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 4 3 2 1
Program: –
Pattern double number on each column
Pattern: –
0 0 1 0 2 4 0 3 6 9 0 4 8 12 16 0 5 10 15 20 25 0 6 12 18 24 30 36
Program: –
Number reduction pattern
Pattern: –
1 2 3 4 5 2 3 4 5 3 4 5 4 5 5
Program: –
Pant style pattern of numbers
Pattern: –
5 4 3 2 1 1 2 3 4 5 5 4 3 2 2 3 4 5 5 4 3 3 4 5 5 4 4 5 5 5
Program: –
Pattern with a combination of numbers and stars
Pattern: –
1 * 2 * 3 * 4 1 * 2 * 3 1 * 2 1
Program: –
Also, see how to calculate the sum and average in Python.
Practice Problem
Pattern: –
0 2 4 4 8 8 8 16 16 16
Solution: –
Next Steps
Solve:
If you don’t find the pattern you are looking for, let me know by leaving a comment and questions below.