Python pattern programs pdf download

In this article, you will learn about many different pattern program in Python. You will learn how to create and print patterns in Python using for loops and while loops.

Star Pattern In Python

Star pattern is a common pattern program created in any programming language. It is a pattern that consists of a series of stars that create some sort of shape.

In the image below you can see some of the star patterns.

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.

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
  2. Hollow Square Pattern in Python
  3. Left triangle star Pattern in Python
  4. Right triangle star Pattern in Python
  5. Left downward triangle pattern
  6. Right downward triangle pattern
  7. Hollow triangle star Pattern
  8. Pyramid Pattern in Python
  9. Hollow Pyramid Pattern in Python
  10. Reverse pyramid Pattern in Python
  11. Diamond star pattern in Python
  12. Hollow diamond star pattern in Python
  13. Hourglass star pattern in python
  14. Right Pascal star Pattern in Python
  15. Left Pascal star Pattern in python
  16. Heart Pattern in Python
  17. Plus star pattern
  18. Cross star pattern
  19. Left triangle number pattern
  20. Right triangle number pattern
  21. Number pyramid pattern
  22. Number pyramid reverse pattern
  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
  29. Alphabet diamond pattern
  30. Hollow alphabet diamond pattern

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. Since it is a square, it is very easy to create and print. Let's see how to create and print a square pattern.

To create a square star pattern run 2 nested for loops. The outer loop will create the rows and the inner loop will create columns. The internal loop will print stars [*] and create a new line after every row.

The following is the code for creating a square pattern.

  • Beginner
  • Pro

# Square pattern program

# Create a list of rows
for i in range[0, 5]:
    # Create a list of columns
    for j in range[0, 5]:
        print["*", end=""]
    print[]

# Square pattern program

for i in range[0, 5]:
    # printing * for 5 times and a new line
    print["*" * 5]

Output:

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

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.

To create a hollow square pattern, we will again run 2 nested for loops and use conditional statements. The outer loop will run for a number of times as the size of the square. The inner loop will print only * in the first and last row and in other rows print * only at the first and last position and in the middle print spaces. The following is the code for creating a hollow square pattern.

  • Beginner
  • Pro

# hollow square pattern
size = 5
for i in range[size]:
    for j in range[size]:
        # print * completely in first and last row
        # print * only in first and last position in other rows
        if i == 0 or i == size - 1 or j == 0 or j == size - 1:
            print['*', end='']
        else:
            print[' ', end='']
    print[]

# hollow square pattern
size = 5
for i in range[size]:
    # print star in first and last row
    if i == 0 or i == size - 1:
        print['*' * size]
    else:
        # print * in first and last position in other rows
        print['*' + ' ' * [size - 2] + '*']

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.

To create a left triangle star pattern, run 2 nested loops where the internal loop will run for a number of times as the number of times external has run and print start.

  • Beginner
  • Pro

# Left triangle star pattern
n = 5

for i in range[1, n+1]:
    # internal loop run for i times
    for k in range[1, i+1]:
        print["*", end=""]
    print[]

# Left triangle star pattern
n = 5

for i in range[1, n+1]:
    print["*" * i]

Output:

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.

To create a right triangle star pattern there will be 2 nested loops, where external will run for a number of times as the size of the triangle and inside this, there will be 2 internal loops one will print * and the other will print spaces. The following is the code for creating a right triangle star pattern.

  • Beginner
  • Pro

# right triangle star pattern
size = 5
for i in range[size]:
    for j in range[1, size - i]:
        print[" ", end=""]
    for k in range[0, i + 1]:
        print["*", end=""]
    print[]

# Left triangle star pattern
size = 5
for i in range[1, size+1]:
    print[" " * [size - i] + "*" * i]

Output:

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.

Run 2 nested loops where the internal loop will run for 'n' time, then 'n - 1' times till 0 and print star.

  • Beginner
  • Pro

# downward triangle star pattern
n = 5

for i in range[n]:
    # internal loop run for n - i times
    for j in range[n - i]:
        print['*', end='']
    print[]

# downward triangle star pattern
n = 5
for i in range[n]:
    print['*' * [n - i]]

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.

You have to manage 2 internal loops and 1 external loop. The external loop will run for 'n' times and the internal loops will print stars and spaces.

# downward triangle star pattern
size = 5
for i in range[size]:
    for j in range[i]:
        print[" ", end=""]
    for j in range[size, i, -1]:
        print["*", end=""]
    print[]

Output:

7. Hollow triangle star Pattern

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

The hollow triangle star pattern is a star pattern in the shape of the triangle made of stars but hollow. You can see the pattern up there.

You can see in the above pattern that in every row except the first and last row, there will be stars only at the start and end of the row. And in the last row, there will be stars only at the start, end, or of the row.

# hollow triangle star pattern
n = 6
for i in range[1, n+1]:
    for j in range[i]:
        # print star only at start and end of the row
        if j == 0 or j == i-1:
            print['*', end='']
        # print only star if it's last row
        else:
            if i != n:
                print[' ', end='']
            else:
                print['*', end='']
    print[]

Output:

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.

There will be 2 loops of which the first one will print spaces 'n - 1' in the first row, 'n - 2' in the second row, and so on and the second loop will print stars '2*i - 1' in the first row, '2*i - 2' in the second row and so on.

  • Beginner
  • Pro

# pyramid star pattern
n = 5
for i in range[n]:
    for j in range[n - i - 1]:
        print[' ', end='']
    for k in range[2 * i + 1]:
        print['*', end='']
    print[]

# pyramid star pattern
n = 5
for i in range[n]:
    print[' ' * [n - i - 1] + '*' * [2 * i + 1]]

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.

You can see in the above pattern in every row there are 3 changes, first, there is a series of spaces, then a star and then another series of spaces, and finally a star. And in the last row, there are only stars.

So to create this pattern, first run a loop and print spaces. Then run another loop that prints a star then a series of spaces and finally print a star.

# hollow pyramid star pattern
n = 5
for i in range[n]:
    # printing spaces
    for j in range[n - i - 1]:
        print[' ', end='']

    # printing stars
    for k in range[2 * i + 1]:
        # print star at start and end of the row
        if k == 0 or k == 2 * i:
            print['*', end='']
        else:
            if i == n - 1:
                print['*', end='']
            else:
                print[' ', end='']
    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.

You can simply create it by running 2 nested loops where there will be 2 internal loops on to print spaces and another loop will print stars.

Let's see the full code here.

# reverse pyramid pattern
n = 5

for i in range[n]:
    # printing spaces
    for j in range[i]:
        print[' ', end='']
    # printing stars
    for j in range[2*[n-i]-1]:
        print['*', end='']
    print[]

Output:

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

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.

# diamond star pattern
n = 5

# upward pyramid
for i in range[n]:
    for j in range[n - i - 1]:
        print[' ', end='']
    for j in range[2 * i + 1]:
        print['*', end='']
    print[]

# downward pyramid
for i in range[n - 1]:
    for j in range[i + 1]:
        print[' ', end='']
    for j in range[2*[n - i - 1] - 1]:
        print['*', end='']
    print[]

Output:

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

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.

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.

# hollow diamond star pattern
n = 5

# upward hollow pyramid
for i in range[n]:
    for j in range[n - i - 1]:
        print[' ', end='']
    for j in range[2 * i + 1]:
        if j == 0 or j == 2 * i:
            print['*', end='']
        else:
            print[' ', end='']
    print[]

# downward pyramid
for i in range[n - 1]:
    for j in range[i + 1]:
        print[' ', end='']
    for j in range[2*[n - i - 1] - 1]:
        if j == 0 or j == 2*[n - i - 1] - 2:
            print['*', end='']
        else:
            print[' ', end='']
    print[]

Output:

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

13. Hourglass Star Pattern 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.

# hourglass star pattern
n = 5

# downward pyramid
for i in range[n-1]:
    for j in range[i]:
        print[' ', end='']
    for k in range[2*[n-i]-1]:
        print['*', end='']
    print[]
# upward pyramid
for i in range[n]:
    for j in range[n-i-1]:
        print[' ', end='']
    for k in range[2*i+1]:
        print['*', end='']
    print[]

Output:

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

14. Right Pascal Star Pattern In Python

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

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.

  • Beginner
  • Pro

# right pascal triangle
n = 5

# upper triangle
for i in range[n]:
    for j in range[i + 1]:
        print['*', end=""]
    print[]
# lower triangle
for i in range[n]:
    for j in range[n - i - 1]:
        print['*', end=""]
    print[]

# right pascal triangle
n = 5

for i in range[n]:
    print['*' * [i + 1]]
for i in range[n]:
    print['*' * [n - i - 1]]

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.

# left pascal triangle
n = 5

# upper triangle
for i in range[n]:
    # print spaces
    for j in range[n - i - 1]:
        print[' ', end='']
    # print stars
    for k in range[i + 1]:
        print['*', end='']
    print[]

# lower triangle
for i in range[n - 1]:
    # print spaces
    for j in range[i + 1]:
        print[' ', end='']
    # print stars
    for k in range[n - i - 1]:
        print['*', end='']
    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.

# heart pattern 
n = 6

# upper part of the heart
for i in range[n//2, n, 2]:
    # print first spaces
    for j in range[1, n-i ,2]:
        print[" ", end=""]
    # print first stars
    for j in range[1, i+1, 1]:
        print["*", end=""]
    # print second spaces
    for j in range[1, n-i+1, 1]:
        print[" ", end=""]
    # print second stars
    for j in range[1, i+1, 1]:
        print["*", end=""]
    print[]

# lower part
for i in range[n,0,-1]:
    for j in range[i, n, 1]:
        print[" ", end=""]
    for j in range[1, i*2, 1]:
        print["*", end=""]
    print[]

Output:

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

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.

# plus pattern in python

size = 5

for i in range[size]:
    for j in range[size]:
        if i == size // 2:
            print['*', end='']
        else:
            if j == size // 2:
                print['*', end='']
            else:
                print[' ', end='']
    print[]

Output:

18. Cross pattern program in Python

The cross pattern is a pattern with the shape of a cross sign [X].

*   *
 * * 
  *  
 * * 
*   *

Here is the complete code to create the cross pattern.

# cross pattern in python
size = 5

for i in range[size]:
    for j in range[size]:
        if i == j or i + j == size - 1:
            print["*", end=""]
        else:
            print[" ", end=""]
    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.

# left number triangle pattern
size = 5
for i in range[size]:
    for j in range[i+1]:
        print[j+1, end=""]
    print[]

Output:

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
  123
 1234
12345

The complete code for the right number triangle pattern is given below.

# right number triangle pattern
size = 5
for i in range[size]:
    # print spaces
    for j in range[1, size - i]:
        print[" ", end=""]
    # print numbers
    for k in range[i + 1]:
        print[k + 1, end=""]
    print[]

Output:

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.

# number pyramid pattern
size = 5
for i in range[size]:
    # print spaces
    for j in range[size - i - 1]:
        print[" ", end=""]
    # print numbers
    for k in range[2 * i + 1]:
        print[k+1, end=""]
    print[]

Output:

    1
   123
  12345
 1234567
123456789

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.

# reverse number pyramid pattern
size = 5
for i in range[size]:
    # print spaces
    for j in range[i]:
        print[" ", end=""]
    # print numbers
    for k in range[2 * [size - i] - 1]:
        print[k+1, end=""]
    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
   1 2
  1   2
 1     2
123456789

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

# hollow number pyramid pattern
size = 5
for i in range[size]:
    # print spaces
    for j in range[size - i - 1]:
        print[" ", end=""]

    # print numbers
    count = 1;
    for k in range[2 * i + 1]:
        if i == 0 or i == size - 1:
            print[count, end=""]
            count += 1
        else:
            if k == 0 or k == 2 * i:
                print[count, end=""]
                count += 1
            else:
                print[" ", end=""]
    print[]

Output:

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.

# number diamond pattern
size = 5
num = 1

# upside pyramid
for i in range[1, size + 1]:
    # printing spaces
    for j in range[size, i - 1, -1]:
        print[" ", end=""]
    # printing star
    for k in range[0, i * 2 - 1]:
        print[num, end=""]
        num += 1
    # set the number to 1
    num = 1
    print[]
# downside pyramid
for i in range[1, size]:
    # printing spaces
    for j in range[0, i+1]:
        print[" ", end=""]
    # printing star
    for k in range[[size - i] * 2 - 1]:
        print[num, end=""]
        num += 1
    # set num to 1
    num = 1
    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.

# hollow diamond number pattern
size = 5
num = 1

# upward hollow pyramid
for i in range[size]:
    for j in range[size - i - 1]:
        print[' ', end='']
    for j in range[2 * i + 1]:
        if j == 0 or j == 2 * i:
            print[num, end='']
            num += 1
        else:
            print[' ', end='']
    # set num to 1
    num = 1
    print[]

# downward pyramid
for i in range[size - 1]:
    for j in range[i + 1]:
        print[' ', end='']
    for j in range[2*[size - i - 1] - 1]:
        if j == 0 or j == 2*[size - i - 1] - 2:
            print[num, end='']
            num += 1
        else:
            print[' ', end='']
    # set num to 1
    num = 1
    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
ABCDEFGHI

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

# alphabet pyramid pattern
size = 5
alpha = 65

for i in range[size]:
    # print spaces
    for j in range[size - i]:
        print[" ", end=""]
    # print alphabets
    for k in range[2 * i + 1]:
        print[chr[alpha + k], end=""]
    print[]

Output:

    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.

# reverse alphabet pyramid pattern
size = 5
alpha = 65

for i in range[size]:
    # print spaces
    for j in range[i]:
        print[" ", end=""]
    # print alphabets
    for k in range[2 * [size - i] - 1]:
        print[chr[alpha + k], end=""]
    print[]

Output:

ABCDEFGHI
 ABCDEFG
  ABCDE
   ABC
    A

28. Hollow Alphabet Pyramid Pattern

The hollow alphabet pyramid pattern is a pyramid pattern that is made of alphabets and is hollow inside.

    A
   B C
  D   E
 F     G
HIJKLMNOP

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

# hollow alphabet pyramid pattern
size = 5
alpha = 65
num = 0

for i in range[size]:
    for j in range[size - i - 1]:
        print[" ", end=""]
    for k in range[2 * i + 1]:
        if i == 0 or i == size - 1:
            print[chr[alpha + num], end=""]
            num += 1
        else:
            if k == 0 or k == 2 * i:
                print[chr[alpha + num], end=""]
                num += 1
            else:
                print[" ", end=""]
    print[]

Output:

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
    A

The complete code for the alphabet diamond pattern is given below.

# alphabet diamond pattern
size = 5
alpha = 65
num = 0

# upside pyramid
for i in range[1, size + 1]:
    # printing spaces
    for j in range[size, i - 1, -1]:
        print[" ", end=""]
    # printing alphabets
    for k in range[0, i * 2 - 1]:
        print[chr[alpha + num], end=""]
        num += 1
    num = 0
    print[]
#downward pyramid
for i in range[1, size]:
    # printing spaces
    for j in range[0, i+1]:
        print[" ", end=""]
    # printing alphabets
    for k in range[[size - i] * 2 - 1]:
        print[chr[alpha + num], end=""]
        num += 1
    num = 0
    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.

# hollow alphabet diamond pattern
# hollow diamond alphabet pattern
size = 5
alpha = 65
num = 0

# upward hollow pyramid
for i in range[size]:
    for j in range[size - i - 1]:
        print[' ', end='']
    for j in range[2 * i + 1]:
        if j == 0 or j == 2 * i:
            print[chr[alpha+num], end='']
            num += 1
        else:
            print[' ', end='']
    # set num to 0
    num = 0
    print[]

# downward pyramid
for i in range[size - 1]:
    for j in range[i + 1]:
        print[' ', end='']
    for j in range[2*[size - i - 1] - 1]:
        if j == 0 or j == 2*[size - i - 1] - 2:
            print[chr[alpha+num], end='']
            num += 1
        else:
            print[' ', end='']
    # set num to 0
    num = 0
    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.

Bài mới nhất

Chủ Đề