Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
942 views

Pattern Program in Python (With 30 Examples)

The document discusses 30 different pattern programs that can be created in Python using stars, numbers, and letters. It provides examples of common patterns like square patterns, triangle patterns, pyramid patterns, and diamond patterns. The patterns are created using for loops to print the elements in the correct positions. Code snippets are given for each pattern type as examples.

Uploaded by

TAUHID ALAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
942 views

Pattern Program in Python (With 30 Examples)

The document discusses 30 different pattern programs that can be created in Python using stars, numbers, and letters. It provides examples of common patterns like square patterns, triangle patterns, pyramid patterns, and diamond patterns. The patterns are created using for loops to print the elements in the correct positions. Code snippets are given for each pattern type as examples.

Uploaded by

TAUHID ALAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

Learn data and AI skills

Search any topic...  Write for Us

  Category

 PYTHON TUTORIAL

Python tutorial
Pattern Program in Python BlueStone Jewellery Prayagraj
Python versions

Print in python Lifetime Exchange & Buy


back
Python keywords Stores in Delhi Gurgaon Noida
Ghaziabad Jaipur Chandigarh
Python if else statement
❮ Prev Next ❯
Python ternary operator
 Stay Ahead, Learn More
• Number Pattern Programs in Python
For Loop In Python In this article, you will look at 30 different pattern program in Python.
• Alphabet Pattern Programs in
While Loop In Python Python
List in Python
Star Pattern In Python • Pyramid Pattern Programs in
Python
 List Comprehension in Star pattern is a common pattern program created in any programming
Python
language. It is a pattern that consists of a series of stars that create some sort of
String in Python
shape.
Functions in Python

Dictionary in Python

 Dictionary Comprehension
in Python
report this ad
Pattern program in python

Alphabet Pattern Programs in


python

Number pattern programs in


python
In the image below you can see some of the star patterns.
Python string methods

Python built-in functions

 Python Miscellaneous

Python add to list

Find max and min in a list in


Python

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.

Let's start with different pattern programs using python.



1 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

List of Pattern Program In Python

We are going to see the following pattern program in python here in this article.

1. Square Pattern in Python 13. Hourglass star pattern in python

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

4. Right triangle star Pattern in Python 16. Heart Pattern in Python

5. Left downward triangle pattern 17. Plus star pattern

6. Right downward triangle pattern 18. Cross star pattern

 Stay Ahead, Learn More 7. Hollow triangle star Pattern 19. Left triangle number pattern

8. Pyramid Pattern in Python


• Number Pattern Programs in Python 20. Right triangle number pattern
• Alphabet Pattern Programs in
9. Hollow Pyramid Pattern in Python 21. Number pyramid pattern
Python
• Pyramid Pattern Programs in 22. Number pyramid reverse pattern
Python 23. Hollow number pyramid pattern

24. Number diamond pattern

25. Hollow number diamond pattern

26. Alphabet pyramid pattern

27. Reverse alphabet pyramid pattern

28. Hollow alphabet pyramid pattern


10. Reverse pyramid Pattern in Python
29. Alphabet diamond pattern
11. Diamond star pattern in Python
30. Hollow alphabet diamond pattern
12. Hollow diamond star pattern in Python

1. Square Pattern in Python

*****
*****
*****
*****
*****

The square pattern is the easiest pattern program. It is a pattern that has the
shape of a square made of stars.

Let's see how to create and print a square pattern.

1. Take size of the square as input from the user or just set a value.

2. Run a nested loop for the number of rows and columns.

3. Print the star in each iteration and print a new line after each row.

Beginner Pro

1 # Square pattern program


2 size = 5
3
4 # Create a list of rows
5 for i in range(0, size):
6 # Create a list of columns
7 for j in range(0, size):
8 print("*", end="")
9 print()

Output:

*****
*****

*****
*****

2 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

*****

2. Hollow Square Pattern

*****
* *
* *
* *
*****

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.

Here are the steps to create a hollow square pattern.


 Stay Ahead, Learn More
1. To create hollow pattern again run a nested for loop.
• Number Pattern Programs in Python
• Alphabet Pattern Programs in 2. External loop will be same as the previous square pattern but inside internal loop
Python you will have to check the condition.
• Pyramid Pattern Programs in
Python 3. If its the first or last row or column then print onlu stars.

4. Otherwise check if its the first or last column then print star else print spaces.

Beginner Pro

1 # hollow square pattern


2 size = 5
3 for i in range(size):
4 for j in range(size):
5 # print * completely in first and last row
6 # print * only in first and last position in other rows
7 if i == 0 or i == size - 1 or j == 0 or j == size - 1:
8 print('*', end='')
9 else:
10 print(' ', end='')
11 print()

Output:

*****
* *
* *
* *
*****

3. Left Triangle Star Pattern In Python

*
**
***
****
*****

The left triangle star pattern is a star pattern in the shape of a triangle. It is quite
easy to create it.

Steps to create a left triangle star pattern:

Compact-size Single Platform Laser


Cutting Machine 1500W-4000W. 
HSG®

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.

2. Print star in each iteration of internal loop.

3. Print a new line at the end of internal loop.

Beginner Pro

1 # Left triangle star pattern


2 n = 5
3
4 for i in range(1, n+1):
5 # internal loop run for i times
6 for k in range(1, i+1):
7 print("*", end="")
 Stay Ahead, Learn More 8 print()

• Number Pattern Programs in Python


• Alphabet Pattern Programs in
Output:
Python
• Pyramid Pattern Programs in *
**
Python
***
****
*****

4. Right Triangle Star Pattern In Python

*
**
***
****
*****

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.

Steps to create a right triangle star pattern:

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.

4. Print new line at the end of both internal loops.

Beginner Pro

1 # right triangle star pattern


2 size = 5
3 for i in range(size):
4 for j in range(1, size - i):
5 print(" ", end="")
6 for k in range(0, i + 1):
7 print("*", end="")
8 print()

Output:

*
**
***

4 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

****
*****

5. Left Downward Triangle Pattern

*****
****
***
**
*

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:

*****
****
***
**
*

6. Right Downward Triangle Pattern

*****
****
***
**
*

The right downward triangle pattern is a pattern that is upside down and has
perpendicular to the right side.

Steps to create a right downward triangle pattern:

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.

3. Here i is index of the current row.

4. Print new line everytime after the internal loop.



5 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

1 # downward triangle star pattern


2 size = 5
3 for i in range(size):
4 for j in range(i):
5 print(" ", end="")
6 for j in range(size, i, -1):
7 print("*", end="")
8 print()

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.

Follow these steps:

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.

1 # hollow triangle star pattern


2 n = 6
3 for i in range(1, n+1):
4 for j in range(i):
5 # print star only at start and end of the row
6 if j == 0 or j == i-1:
7 print('*', end='')
8 # print only star if it's last row
9 else:
10 if i != n:
11 print(' ', end='')
12 else:
13 print('*', end='')
14 print()

Output:

*
**
* *
* *
* *
******


6 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

8. Pyramid Pattern in python

*
***
*****
*******
*********

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.

 Stay Ahead, Learn More 1. Again we need to nest the loops.

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

1 # pyramid star pattern


2 n = 5
3 for i in range(1, n+1):
4 for j in range(n - i):
5 print(' ', end='')
6 for k in range(2 * i - 1):
7 print('*', end='')
8 print()

Output:

*
***
*****
*******
*********

9. Hollow Pyramid Pattern In Python

*
* *
* *
* *
*********

The hollow pyramid pattern is a pyramid pattern made of stars but hollow. You
can see the pattern up there.

Follow these steps to create hollow pyramid pattern:

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.

3. The first internal loop print spaces for size - i times.



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.

 Stay Ahead, Learn More


1 # hollow pyramid star pattern
2
• Number Pattern Programs in Python n = 5
• Alphabet Pattern Programs in 3 for i in range(1, n+1):
4 # printing spaces
Python
5 for j in range(n - i):
• Pyramid Pattern Programs in
6 print(' ', end='')
Python
7
8 # printing stars
9 for k in range(2 * i - 1):
10 # print star at start and end of the row
11 if k == 0 or k == 2 * i - 2:
12 print('*', end='')
13 else:
14 if i == n:
15 print('*', end='')
16 else:
17 print(' ', end='')
18 print()

Output:

*
* *
* *
* *
*********

10. Reverse Pyramid Pattern In Python

*********
*******
*****
***
*

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.

1 # reverse pyramid pattern


2 n = 5
3
4 for i in range(1, n+1):
5 # printing spaces
6 for j in range(i-1):
7 print(' ', end='')
8 # printing stars
9 for j in range(2*(n-i)+1):
10 print('*', end='')
11 print()

Output:



8 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  Stay Ahead, Learn More


• Number Pattern Programs in Python
• Pyramid Pattern In Python
• Alphabet Pattern Programs in
Python • Number Pattern Programs In Python
• Pyramid Pattern Programs in • Alphabet Pattern Programs In Python
Python

11. Diamond Star Pattern In Python

*
***
*****
*******
*********
*******
*****
***
*

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.

Here is the code to create this pattern.

1 # diamond star pattern


2 n = 5
3
4 # upward pyramid
5 for i in range(n):
6 for j in range(n - i - 1):
7 print(' ', end='')
8 for j in range(2 * i + 1):
9 print('*', end='')
10 print()
11
12 # downward pyramid
13 for i in range(n - 1):
14 for j in range(i + 1):
15 print(' ', end='')
16 for j in range(2*(n - i - 1) - 1):
17 print('*', end='')
18 print()

Output:

*
***
*****
*******
*********
*******
*****
***
* 

9 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

12. Hollow Diamond Star Pattern In Python

*
* *
* *
* *
* *
* *
* *
* *
*

The hollow diamond pattern is the same as the diamond star pattern but hollow.
The pattern is up here.

 Stay Ahead, Learn More


• Number Pattern Programs in Python
• Alphabet Pattern Programs in
Python
• Pyramid Pattern Programs in
Python

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.

Let's see the code.

1 # hollow diamond star pattern


2 n = 5
3
4 # upward hollow pyramid
5 for i in range(n):
6 for j in range(n - i - 1):
7 print(' ', end='')
8 for j in range(2 * i + 1):
9 if j == 0 or j == 2 * i:
10 print('*', end='')
11 else:
12 print(' ', end='')
13 print()
14
15 # downward pyramid
16 for i in range(n - 1):
17 for j in range(i + 1):
18 print(' ', end='')
19 for j in range(2*(n - i - 1) - 1):
20 if j == 0 or j == 2*(n - i - 1) - 2:
21 print('*', end='')
22 else:
23 print(' ', end='')
24 print()

Output:

*
* *
* *
* *
* *
* *
* *
* *
*



10 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

13. Hourglass Star Pattern In Python

*********
 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.

1 # hourglass star pattern


2 n = 5
3
4 # downward pyramid
5 for i in range(n-1):
6 for j in range(i):
7 print(' ', end='')
8 for k in range(2*(n-i)-1):
9 print('*', end='')
10 print()
11 # upward pyramid
12 for i in range(n):
13 for j in range(n-i-1):
14 print(' ', end='')
15 for k in range(2*i+1):
16 print('*', end='')
17 print()

Output:

*********
*******
*****
***
*
***
*****
*******
*********

14. Right Pascal Star Pattern In Python 


*
**

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.

Here is the complete code.


 Stay Ahead, Learn More
Beginner
• Number Pattern Programs in Python Pro
• Alphabet Pattern Programs in
1 # right pascal triangle
Python
2 n = 5
• Pyramid Pattern Programs in
3
Python
4 # upper triangle
5 for i in range(n):
6 for j in range(i + 1):
7 print('*', end="")
8 print()
9 # lower triangle
10 for i in range(n):
11 for j in range(n - i - 1):
12 print('*', end="")
13 print()

Output:

*
**
***
****
*****
****
***
**
*

15. Left Pascal Star Pattern In Python

*
**
***
****
*****
****
***
**
*

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...

1 # left pascal triangle


2 n = 5
3
4 # upper triangle
5 for i in range(n):
6 # print spaces
7 for j in range(n - i - 1):
8 print(' ', end='')
9 # print stars
10 for k in range(i + 1):
11 print('*', end='')
12 print()
13
14 # lower triangle
15 for i in range(n - 1):
 Stay Ahead, Learn More 16 # print spaces
17 for j in range(i + 1):
• Number Pattern Programs in Python
18 print(' ', end='')
• Alphabet Pattern Programs in 19 # print stars
Python 20 for k in range(n - i - 1):
• Pyramid Pattern Programs in 21 print('*', end='')
Python 22 print()

Output:

*
**
***
****
*****
****
***
**
*

16. Heart pattern in python

*** ***
***** *****
***********
*********
*******
*****
***
*

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:

 Stay Ahead, Learn More


• Number Pattern Programs in Python
• Alphabet Pattern Programs in
Python
• Pyramid Pattern Programs in
Python

*** ***
***** *****
***********
*********
*******
*****
***
*

17. Plus pattern program in Python

The plus pattern is a pattern with the shape of a plus sign (+).

*
*
*****
*
*

The complete code is given below.

1 # plus pattern in python


2
3 size = 5
4
5 for i in range(size):
6 for j in range(size):
7 if i == size // 2:
8 print('*', end='')
9 else:
10 if j == size // 2:
11 print('*', end='')
12 else:
13 print(' ', end='')
14 print()

Output:

*
*
*****
*
*



14 of 27 20/09/23, 05:59
Pattern Program in Python (with 30 Examples) https://www.tutorialstonight.com/python/pattern-prog...

18. Cross pattern program in Python

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.

1 # cross pattern in python


2 size = 5
3
4 for i in range(size):
5 for j in range(size):
6 if i == j or i + j == size - 1:
7 print("*", end="")
8 else:
9 print(" ", end="")
10 print()

Output:

* *
* *
*
* *
* *

19. Left Number Triangle Pattern Program

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.

1 # left number triangle pattern


2 size = 5
3 for i in range(size):
4 for j in range(i+1):
5 print(j+1, end="")
6 print()

Output:

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

20. Right Number Triangle Pattern Program

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

21. Number Pyramid Pattern Program In


Python

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...

1 # number pyramid pattern


2 size = 5
3 for i in range(size):
4 # print spaces
5 for j in range(size - i - 1):
6 print(" ", end="")
7 # print numbers
8 for k in range(2 * i + 1):
9 print(k+1, end="")
10 print()

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

The reverse number pyramid pattern is a number pyramid reversed 180


degrees.

123456789
1234567
12345
123
1

The complete code for the reverse number pyramid pattern is given below.

1 # reverse number pyramid pattern


2 size = 5
3 for i in range(size):
4 # print spaces
5 for j in range(i):
6 print(" ", end="")
7 # print numbers
8 for k in range(2 * (size - i) - 1):
9 print(k+1, end="")
10 print()

Output:

123456789
1234567
12345
123
1

23. Hollow Number Pyramid Pattern Program

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.

1 # hollow number pyramid pattern


2 size = 5
3 for i in range(size):
4 # print spaces
5 for j in range(size - i - 1):
6 print(" ", end="")
7
8 # print numbers
9 count = 1;
10 for k in range(2 * i + 1):
11 if i == 0 or i == size - 1:
12 print(count, end="")
13 count += 1
14 else:
 Stay Ahead, Learn More 15 if k == 0 or k == 2 * i:
16
• Number Pattern Programs in Python print(count, end="")
17 count += 1
• Alphabet Pattern Programs in
18 else:
Python
19 print(" ", end="")
• Pyramid Pattern Programs in
20 print()
Python

Output:

1
1 2
1 2
1 2
123456789

24. Number Diamond Pattern Program

The number diamond pattern is a diamond pattern that is made of numbers.

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...

1 # number diamond pattern


2 size = 5
3 num = 1
4
5 # upside pyramid
6 for i in range(1, size + 1):
7 # printing spaces
8 for j in range(size, i - 1, -1):
9 print(" ", end="")
10 # printing star
11 for k in range(0, i * 2 - 1):
12 print(num, end="")
13 num += 1
14 # set the number to 1
15 num = 1
 Stay Ahead, Learn More 16 print()
17 # downside pyramid
• Number Pattern Programs in Python
18 for i in range(1, size):
• Alphabet Pattern Programs in 19 # printing spaces
Python 20 for j in range(0, i+1):
• Pyramid Pattern Programs in 21 print(" ", end="")
Python 22 # printing star
23 for k in range((size - i) * 2 - 1):
24 print(num, end="")
25 num += 1
26 # set num to 1
27 num = 1
28 print()

Output:

1
123
12345
1234567
123456789
1234567
12345
123
1

25. Hollow Number Diamond Pattern


Program

The hollow number diamond pattern is a diamond pattern that is made of


numbers and is hollow inside.

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...

1 # hollow diamond number pattern


2 size = 5
3 num = 1
4
5 # upward hollow pyramid
6 for i in range(size):
7 for j in range(size - i - 1):
8 print(' ', end='')
9 for j in range(2 * i + 1):
10 if j == 0 or j == 2 * i:
11 print(num, end='')
12 num += 1
13 else:
14 print(' ', end='')
15 # set num to 1
 Stay Ahead, Learn More 16 num = 1
17 print()
• Number Pattern Programs in Python
18
• Alphabet Pattern Programs in 19 # downward pyramid
Python 20 for i in range(size - 1):
• Pyramid Pattern Programs in 21 for j in range(i + 1):
Python 22 print(' ', end='')
23 for j in range(2*(size - i - 1) - 1):
24 if j == 0 or j == 2*(size - i - 1) - 2:
25 print(num, end='')
26 num += 1
27 else:
28 print(' ', end='')
29 # set num to 1
30 num = 1
31 print()

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.

26. Alphabet Pyramid Pattern Program

The alphabet pyramid pattern is a pyramid pattern that is made of alphabets.

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.

1 # alphabet pyramid pattern


2 size = 5
3 alpha = 65
4
5 for i in range(size):
6 # print spaces
7 for j in range(size - i):
8 print(" ", end="")
9 # print alphabets
10 for k in range(2 * i + 1):
11 print(chr(alpha + k), end="")
12 print()
 Stay Ahead, Learn More
Output:
• Number Pattern Programs in Python
• Alphabet Pattern Programs in
Python
• Pyramid Pattern Programs in
Python

A
ABC
ABCDE
ABCDEFG
ABCDEFGHI

27. Reverse Alphabet Pyramid Pattern


Program

The reverse alphabet pyramid pattern is a pyramid pattern that is made of


alphabets and is upside down.

ABCDEFGHI
ABCDEFG
ABCDE
ABC
A

The complete code for the reverse alphabet pyramid pattern is given below.

1 # reverse alphabet pyramid pattern


2 size = 5
3 alpha = 65
4
5 for i in range(size):
6 # print spaces
7 for j in range(i):
8 print(" ", end="")
9 # print alphabets
10 for k in range(2 * (size - i) - 1):
11 print(chr(alpha + k), end="")
12 print()

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...

28. Hollow Alphabet Pyramid Pattern

The hollow alphabet pyramid pattern is a pyramid pattern that is made of


 Stay Ahead, Learn More alphabets and is hollow inside.
• Number Pattern Programs in Python
• Alphabet Pattern Programs in A
B C
Python
D E
• Pyramid Pattern Programs in
F G
Python
HIJKLMNOP

The complete code for the hollow alphabet pyramid pattern is given below.

1 # hollow alphabet pyramid pattern


2 size = 5
3 alpha = 65
4 num = 0
5
6 for i in range(size):
7 for j in range(size - i - 1):
8 print(" ", end="")
9 for k in range(2 * i + 1):
10 if i == 0 or i == size - 1:
11 print(chr(alpha + num), end="")
12 num += 1
13 else:
14 if k == 0 or k == 2 * i:
15 print(chr(alpha + num), end="")
16 num += 1
17 else:
18 print(" ", end="")
19 print()

Output:

A
B C
D E
F G
HIJKLMNOP

29. Alphabet Diamond Pattern Program

The alphabet diamond pattern is a diamond pattern that is made of alphabets.

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.

1 # alphabet diamond pattern


2 size = 5
3 alpha = 65
4 num = 0
5
6 # upside pyramid
7 for i in range(1, size + 1):
8 # printing spaces
9 for j in range(size, i - 1, -1):
10 print(" ", end="")
11 # printing alphabets
12 for k in range(0, i * 2 - 1):
 Stay Ahead, Learn More 13 print(chr(alpha + num), end="")
14 num += 1
• Number Pattern Programs in Python
15 num = 0
• Alphabet Pattern Programs in
16 print()
Python
17 #downward pyramid
• Pyramid Pattern Programs in 18 for i in range(1, size):
Python 19 # printing spaces
20 for j in range(0, i+1):
21 print(" ", end="")
22 # printing alphabets
23 for k in range((size - i) * 2 - 1):
24 print(chr(alpha + num), end="")
25 num += 1
26 num = 0
27 print()

Output:

A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A

30. Hollow Alphabet Diamond Pattern

The hollow alphabet diamond pattern is a diamond pattern that is made of


alphabets and is hollow inside.

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...

1 # hollow alphabet diamond pattern


2
3 size = 5
4 alpha = 65
5 num = 0
6
7 # upward hollow pyramid
8 for i in range(size):
9 for j in range(size - i - 1):
10 print(' ', end='')
11 for j in range(2 * i + 1):
12 if j == 0 or j == 2 * i:
13 print(chr(alpha+num), end='')
14 num += 1
15 else:
 Stay Ahead, Learn More 16 print(' ', end='')
17 # set num to 0
• Number Pattern Programs in Python
18 num = 0
• Alphabet Pattern Programs in 19 print()
Python 20
• Pyramid Pattern Programs in 21 # downward pyramid
Python 22 for i in range(size - 1):
23 for j in range(i + 1):
24 print(' ', end='')
25 for j in range(2*(size - i - 1) - 1):
26 if j == 0 or j == 2*(size - i - 1) - 2:
27 print(chr(alpha+num), end='')
28 num += 1
29 else:
30 print(' ', end='')
31 # set num to 0
32 num = 0
33 print()

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...

 Stay Ahead, Learn More


• Number Pattern Programs in Python
• Alphabet Pattern Programs in
Python
• Pyramid Pattern Programs in
Python



25 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

About Us

About us

Contact us

Privacy Policy

Write for us

Disclaimer

Tutorials

 HTML5

 CSS3

 JavaScript

 Bootstrap 4

 Python

 Practice Problems

Tools

HTML Editor

Advance 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...

 Stay Ahead, Learn More


• Number Pattern Programs in Python
• Alphabet Pattern Programs in
Python
• Pyramid Pattern Programs in
Python



27 of 27 20/09/23, 05:59

You might also like