Pattern Program in Python (With 30 Examples)
Pattern Program in Python (With 30 Examples)
Category
PYTHON TUTORIAL
Python tutorial
Pattern Program in Python BlueStone Jewellery Prayagraj
Python versions
Dictionary in Python
Dictionary Comprehension
in Python
report this ad
Pattern program in python
Python Miscellaneous
There are also other types of patterns that do not use stars but numbers or
alphabets. We will also look at these in brief here.
1 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
We are going to see the following pattern program in python here in this article.
2. Hollow Square Pattern in Python 14. Right Pascal star Pattern in Python
3. Left triangle star Pattern in Python 15. Left Pascal star Pattern in python
Stay Ahead, Learn More 7. Hollow triangle star Pattern 19. Left triangle number pattern
*****
*****
*****
*****
*****
The square pattern is the easiest pattern program. It is a pattern that has the
shape of a square made of stars.
1. Take size of the square as input from the user or just set a value.
3. Print the star in each iteration and print a new line after each row.
Beginner Pro
Output:
*****
*****
*****
*****
2 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
*****
*****
* *
* *
* *
*****
The hollow square pattern is a bit more difficult pattern program than a simple
square because here you will have to deal with spaces within the square.
4. Otherwise check if its the first or last column then print star else print spaces.
Beginner Pro
Output:
*****
* *
* *
* *
*****
*
**
***
****
*****
The left triangle star pattern is a star pattern in the shape of a triangle. It is quite
easy to create it.
3 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
1. Run a nested for loop where internal loop will run for number of times external
loop has run.
Beginner Pro
*
**
***
****
*****
The right triangle star pattern is a star pattern in the shape of a triangle as
shown above. It is similar to the left triangle star pattern but you will have to deal
with spaces.
1. Create a loop that will run for the number of rows (size).
2. Inside this we will have 2 loops, first will print spaces and second will print stars.
(look at pattern above)
3. Spaces will be printed for size - i times and stars will be printed for i times.
Where i is the current row.
Beginner Pro
Output:
*
**
***
4 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
****
*****
*****
****
***
**
*
The left downward triangle pattern is the star pattern of a triangle upside down.
It is very easy to create.
Stay Ahead, Learn More Run 2 nested loops where the internal loop will run size - i times and external
• Number Pattern Programs in Python
loop will run size times. Here i is the current row.
• Alphabet Pattern Programs in
Python
Beginner Pro
• Pyramid Pattern Programs in
Python 1 # downward triangle star pattern
2 n = 5
3
4 for i in range(n):
5 # internal loop run for n - i times
6 for j in range(n - i):
7 print('*', end='')
8 print()
Output:
*****
****
***
**
*
*****
****
***
**
*
The right downward triangle pattern is a pattern that is upside down and has
perpendicular to the right side.
1. From the above given pattern you can see that, this also requires 2 internal loop.
2. First internal loop print spaces for i times and second internal loop print stars
for size - i times.
5 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
Output:
*****
****
***
**
Stay Ahead, Learn More
*
• Number Pattern Programs in Python
• Alphabet Pattern Programs in
Python 7. Hollow triangle star Pattern
• Pyramid Pattern Programs in
Python *
**
* *
* *
* *
******
The hollow triangle star pattern is a star pattern in the shape of the triangle
made of stars but hollow.
1. You can see the pattern up there be sure to understand it. Stars are printed only
in first last column of any row, except the last row.
2. Run a nested loop where external loop runs for the size of triangle.
3. Inside create internal loop. Inside it check if its first or last row then print only
stars. If not print starts only at first and last column else print spaces.
Output:
*
**
* *
* *
* *
******
6 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
*
***
*****
*******
*********
The pyramid pattern is a very famous pattern in programming. It has the shape
of an equilateral triangle and it is used to represent a pyramid. You can see the
pattern up here.
The pyramid pattern has an odd number of stars in each row 1, 3, 5, 7, etc.
2. Create 2 internal loop, first print spaces and second print stars.
• Number Pattern Programs in Python
• Alphabet Pattern Programs in 3. Print spaces for number of times equal to size - i and stars for 2 * i - 1 times.
Python
• Pyramid Pattern Programs in 4. Here i is the current row.
Python
Beginner Pro
Output:
*
***
*****
*******
*********
*
* *
* *
* *
*********
The hollow pyramid pattern is a pyramid pattern made of stars but hollow. You
can see the pattern up there.
1. You can observe that we have to handle spaces at 2 different places. First before
printing star and second between the pyramid.
2. Create nested loop with 2 internal loops. First loop just create space, second
loop print both spaces & stars with some logics.
7 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
4. The second internal loop pexecutes for 2 * i - 1 times and checks if it is first or
last row then print star, if not check if it is first or last column then print star else
print space.
Output:
*
* *
* *
* *
*********
*********
*******
*****
***
*
The reverse pyramid pattern is the same as the pyramid pattern but it is upside
down. See the pattern up there.
Just like the pyramid pattern, the reverse pyramid pattern follows the same logic.
The only difference is that we have to print the spaces and stars with reverse
logic.
Output:
8 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
*********
*******
*****
***
*
*
***
*****
*******
*********
*******
*****
***
*
The diamond star pattern is a star pattern with the shape of the diamond. You
can see the pattern up here.
If you look closely, you will see that the pattern is a combination of a pyramid
pattern and a downward triangle star pattern. So you can create this pattern by
combining both patterns.
Output:
*
***
*****
*******
*********
*******
*****
***
*
9 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
*
* *
* *
* *
* *
* *
* *
* *
*
The hollow diamond pattern is the same as the diamond star pattern but hollow.
The pattern is up here.
This one is complex because you have to deal with multiple things like spaces,
stars for each row where the pattern itself is divided into two parts upper pyramid
and lower pyramid.
Output:
*
* *
* *
* *
* *
* *
* *
* *
*
10 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
*********
Stay Ahead, Learn More *******
*****
• Number Pattern Programs in Python ***
• Alphabet Pattern Programs in *
Python ***
• Pyramid Pattern Programs in *****
Python *******
*********
The hourglass pattern is a pattern with the shape of an hourglass. When you
observe the pattern, you will see that it is made up of two patterns. The first
pattern is a downward pyramid pattern and the second pattern is an upward
triangle pattern.
You can create this pattern by combining both patterns. The code is as follows.
Output:
*********
*******
*****
***
*
***
*****
*******
*********
11 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
***
****
*****
****
***
**
*
The right pascal triangle pattern is shown above. It can be clearly seen that it is
made up of an upper triangle and a lower triangle.
So you can run 2 different loops one which creates the upper triangle and another
which creates the lower triangle.
Output:
*
**
***
****
*****
****
***
**
*
*
**
***
****
*****
****
***
**
*
The left pascal triangle pattern is a mirror image of the right pascal triangle
pattern. The pattern is shown above.
The left pascal triangle pattern is a little bit more complicated than the right pascal
triangle pattern because you will have to deal with both spaces and stars.
Let's see the code.
12 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
Output:
*
**
***
****
*****
****
***
**
*
*** ***
***** *****
***********
*********
*******
*****
***
*
The heart pattern is a pattern with the shape of a heart. It is quite a complex
pattern. But if you observe the code carefully then you will understand it easily.
1 # heart pattern
2 n = 6
3
4 # upper part of the heart
5 for i in range(n//2, n, 2):
6 # print first spaces
7 for j in range(1, n-i ,2):
8 print(" ", end="")
9 # print first stars
10 for j in range(1, i+1, 1):
11 print("*", end="")
12 # print second spaces
13 for j in range(1, n-i+1, 1):
14 print(" ", end="")
15 # print second stars
16 for j in range(1, i+1, 1):
17 print("*", end="")
18 print()
19
20 # lower part
21 for i in range(n,0,-1):
22 for j in range(i, n, 1):
23 print(" ", end="")
24 for j in range(1, i*2, 1):
25 print("*", end="")
26 print()
13 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
Output:
*** ***
***** *****
***********
*********
*******
*****
***
*
The plus pattern is a pattern with the shape of a plus sign (+).
*
*
*****
*
*
Output:
*
*
*****
*
*
14 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
The cross pattern is a pattern with the shape of a cross sign (X).
Stay Ahead, Learn More
* *
• Number Pattern Programs in Python
* *
• Alphabet Pattern Programs in *
Python * *
• Pyramid Pattern Programs in * *
Python
Here is the complete code to create the cross pattern.
Output:
* *
* *
*
* *
* *
The left number triangle pattern is a triangle pattern that is made of numbers
and has perpendicular on its left side.
1
12
123
1234
12345
The complete code for the left number triangle pattern is given below.
15 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
1
12
123
1234
12345
The right number triangle pattern is a triangle pattern that is made of numbers
and has perpendicular on its right side.
1
12
Stay Ahead, Learn More 123
1234
• Number Pattern Programs in Python
12345
• Alphabet Pattern Programs in
Python
The complete code for the right number triangle pattern is given below.
• Pyramid Pattern Programs in
Python
1 # right number triangle pattern
2 size = 5
3 for i in range(size):
4 # print spaces
5 for j in range(1, size - i):
6 print(" ", end="")
7 # print numbers
8 for k in range(i + 1):
9 print(k + 1, end="")
10 print()
Output:
1
12
123
1234
12345
The number pyramid pattern is a pattern that is made of numbers and has a
pyramid shape.
1
123
12345
1234567
123456789
The complete code for the number pyramid pattern is given below.
16 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
Output:
1
123
Stay Ahead, Learn More
12345
1234567
• Number Pattern Programs in Python
• Alphabet Pattern Programs in 123456789
Python
• Pyramid Pattern Programs in
Python 22. Reverse Number Pyramid Pattern
Program In Python
123456789
1234567
12345
123
1
The complete code for the reverse number pyramid pattern is given below.
Output:
123456789
1234567
12345
123
1
The hollow number pyramid pattern is a number pyramid pattern that has a
hollow space in the middle.
1 2
1 2
1 2
123456789
17 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
The complete code for the hollow number pyramid pattern is given below.
Output:
1
1 2
1 2
1 2
123456789
1
123
12345
1234567
123456789
1234567
12345
123
1
The complete code for the number diamond pattern is given below.
18 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
Output:
1
123
12345
1234567
123456789
1234567
12345
123
1
1
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1
The complete code for the hollow number diamond pattern is given below.
19 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
Output:
1
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1
Let's now create some pattern programs using alphabets instead of stars or
numbers.
A
ABC
ABCDE
ABCDEFG
20 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
ABCDEFGHI
The complete code for the alphabet pyramid pattern is given below.
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
The complete code for the reverse alphabet pyramid pattern is given below.
Output:
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
21 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
The complete code for the hollow alphabet pyramid pattern is given below.
Output:
A
B C
D E
F G
HIJKLMNOP
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFG
ABCDE
ABC
22 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
The complete code for the alphabet diamond pattern is given below.
Output:
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
A
A B
A B
A B
A B
A B
A B
A B
A
The complete code for the hollow alphabet diamond pattern is given below.
23 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
Output:
A
A B
A B
A B
A B
A B
A B
A B
A
Conclusion
After looking at 30 pattern program in python you have learned the basics of
creating patterns. Now you can create your own patterns and make them look
beautiful. Try the alphabet pattern program discussed in the next article.
❮ Prev Next ❯
24 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
25 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
About Us
About us
Contact us
Privacy Policy
Write for us
Disclaimer
Tutorials
HTML5
CSS3
JavaScript
Bootstrap 4
Python
Practice Problems
Tools
HTML Editor
JavaScript Compiler
Follow Us
Copyright © 2023 TutorialsTonight
26 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...
27 of 27 20/09/23, 05:59