×

Search anything:

20 Python Programs to Print Different Patterns

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

Table of Contents

  1. Introduction
  2. Code
  3. Complexity
  4. Applications
  5. Questions
  6. Conclusion

Introduction

  • Python is a versatile programming language that can be used for a wide range of tasks. One of its many applications is creating patterns using code. In this article, we will explore 20 different Python programs that can be used to print various patterns.

  • List of patterns covered :-

  1. Square Pattern
  2. Right Triangle Pattern
  3. Left Triangle Pattern
  4. Inverted Right Triangle Pattern
  5. Inverted Left Triangle Pattern
  6. Pyramid Pattern
  7. Inverted Pyramid Pattern
  8. Diamond Pattern
  9. Hourglass Pattern
  10. Checkerboard Pattern
  11. Hollow Square Pattern
  12. Hollow Right Triangle
  13. Hollow Pyramid Pattern
  14. Hollow Diamond Pattern
  15. Hollow Hourglass Pattern
  16. Cross Pattern
  17. Plus Pattern
  18. Left Arrow Pattern
  19. Right Arrow Pattern
  20. Heart Pattern

Code

1. Square Pattern

  • To print a square pattern of n x n stars, we can use the following code:
n = 5
for i in range(n):
    for j in range(n):
        print('*', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the square. The inner loop prints a star followed by a space n times, while the outer loop moves to the next line after each row is printed.

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

2. Right Triangle Pattern

  • To print a right triangle pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(i+1):
        print('*', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the triangle. The inner loop prints a star followed by a space i+1 times, where i is the current row number. The outer loop moves to the next line after each row is printed.

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

3. Left Triangle Pattern

  • To print a left triangle pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i+1):
        print('*', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the triangle. The first inner loop prints n-i-1 spaces to align the stars to the left, while the second inner loop prints a star followed by a space i+1 times. The outer loop moves to the next line after each row is printed.

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

4. Inverted Right Triangle Pattern

  • To print an inverted right triangle pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(n-i):
        print('*', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the triangle. The inner loop prints a star followed by a space n-i times, where i is the current row number. The outer loop moves to the next line after each row is printed.

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

5. Inverted Left Triangle Pattern

  • To print an inverted left triangle pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(i):
        print(' ', end=' ')
    for k in range(n-i):
        print('*', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the triangle. The first inner loop prints i spaces to align the stars to the left, while the second inner loop prints a star followed by a space n-i times. The outer loop moves to the next line after each row is printed.

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

6. Pyramid Pattern

  • To print a pyramid pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i*2+1):
        print('*', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the pyramid. The first inner loop prints n-i-1 spaces to center the stars, while the second inner loop prints a star followed by a space i*2+1 times. The outer loop moves to the next line after each row is printed.

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

7. Inverted Pyramid Pattern

  • To print an inverted pyramid pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(i):
        print(' ', end=' ')
    for k in range((n-i)*2-1):
        print('*', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the pyramid. The first inner loop prints i spaces to center the stars, while the second inner loop prints a star followed by a space (n-i)*2-1 times. The outer loop moves to the next line after each row is printed.

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

8. Diamond Pattern

  • To print a diamond pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i*2+1):
        print('*', end=' ')
    print()
for i in range(n-2, -1, -1):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i*2+1):
        print('*', end=' ')
    print()

This code uses two sets of nested for loops to iterate over the rows and columns of the diamond. The first set of loops prints the top half of the diamond, while the second set prints the bottom half. The inner loops are used to print spaces and stars in the appropriate positions.

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

9. Hourglass Pattern

  • To print an hourglass pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(i):
        print(' ', end=' ')
    for k in range((n-i)*2-1):
        print('*', end=' ')
    print()
for i in range(1, n):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i*2+1):
        print('*', end=' ')
    print()

This code uses two sets of nested for loops to iterate over the rows and columns of the hourglass. The first set of loops prints the top half of the hourglass, while the second set prints the bottom half. The inner loops are used to print spaces and stars in the appropriate positions.

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

10. Checkerboard Pattern

  • To print a checkerboard pattern of n x n squares, we can use the following code:
n = 5
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the checkerboard. The inner loop uses a conditional statement to determine whether to print a star or a space based on the current row and column numbers. The outer loop moves to the next line after each row is printed.

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

11. Hollow Square Pattern

  • To print a hollow square pattern of n x n stars, we can use the following code:
n = 5
for i in range(n):
    for j in range(n):
        if i == 0 or i == n-1 or j == 0 or j == n-1:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the square. The inner loop uses a conditional statement to determine whether to print a star or a space based on the current row and column numbers. The outer loop moves to the next line after each row is printed.

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

12. Hollow Right Triangle

  • To print a hollow right triangle pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(i+1):
        if i == n-1 or j == 0 or j == i:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the triangle. The inner loop uses a conditional statement to determine whether to print a star or a space based on the current row and column numbers. The outer loop moves to the next line after each row is printed.

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

13. Hollow Pyramid Pattern

  • To print a hollow pyramid pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i*2+1):
        if k == 0 or k == i*2 or i == n-1:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the pyramid. The first inner loop prints n-i-1 spaces to center the stars, while the second inner loop uses a conditional statement to determine whether to print a star or a space based on the current row and column numbers. The outer loop moves to the next line after each row is printed.

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

14. Hollow Diamond Pattern

  • To print a hollow diamond pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i*2+1):
        if k == 0 or k == i*2:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()
for i in range(n-2, -1, -1):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i*2+1):
        if k == 0 or k == i*2:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two sets of nested for loops to iterate over the rows and columns of the diamond. The first set of loops prints the top half of the diamond, while the second set prints the bottom half. The inner loops are used to print spaces and stars in the appropriate positions.

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

15. Hollow Hourglass Pattern

  • To print a hollow hourglass pattern of n rows, we can use the following code:
n = 5
for i in range(n):
    for j in range(i):
        print(' ', end=' ')
    for k in range((n-i)*2-1):
        if k == 0 or k == (n-i)*2-2 or i == 0:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()
for i in range(1, n):
    for j in range(n-i-1):
        print(' ', end=' ')
    for k in range(i*2+1):
        if k == 0 or k == i*2:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two sets of nested for loops to iterate over the rows and columns of the hourglass. The first set of loops prints the top half of the hourglass, while the second set prints the bottom half. The inner loops are used to print spaces and stars in the appropriate positions.

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

16. Cross Pattern

  • To print a cross pattern of n x n stars, we can use the following code:
n = 5
for i in range(n):
    for j in range(n):
        if i == j or j == n-i-1:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the cross. The inner loop uses a conditional statement to determine whether to print a star or a space based on the current row and column numbers. The outer loop moves to the next line after each row is printed.

*       * 
  *   *
    *
  *   *
*       *

17. Plus Pattern

  • To print a plus pattern of n x n stars, we can use the following code:
n = 5
for i in range(n):
    for j in range(n):
        if i == n//2 or j == n//2:
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the plus. The inner loop uses a conditional statement to determine whether to print a star or a space based on the current row and column numbers. The outer loop moves to the next line after each row is printed.

    *     
    *
* * * * *
    *
    *

18. Left Arrow Pattern

  • To print a leftward arrow pattern of n rows using asterisks, you can use the following code:
n = 7
for i in range(n):
    if i <= n//2:
        for j in range(n//2-i):
            print(' ', end=' ')
        for k in range(i+1):
            print('*', end=' ')
    else:
        for j in range(i-n//2):
            print(' ', end=' ')
        for k in range(n-i):
            print('*', end=' ')
    print()

This code uses a for loop to iterate over the rows of the arrow pattern. The inner loops use conditional statements to determine whether to print a space or a star based on the current row number. The outer loop moves to the next line after each row is printed.

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

19. Right Arrow Pattern

  • To print a rightward arrow pattern of n rows using asterisks, you can use the following code:
n = 7
for i in range(n):
    if i <= n//2:
        for j in range(n//2):
            print(' ', end=' ')
        for k in range(i+1):
            print('*', end=' ')
    else:
        for j in range(n//2):
            print(' ', end=' ')
        for k in range(n-i):
            print('*', end=' ')
    print()

This code uses a for loop to iterate over the rows of the arrow pattern. The inner loops use conditional statements to determine whether to print a space or a star based on the current row number. The outer loop moves to the next line after each row is printed.

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

20. Heart Pattern

  • To print a heart pattern of n rows, we can use the following code:
for row in range(6):
    for col in range(7):
        if (row == 0 and col % 3 != 0) or (row == 1 and col % 3 == 0) or (row - col == 2) or (row + col == 8):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

This code uses two nested for loops to iterate over the rows and columns of the heart pattern. The inner loop uses a conditional statement to determine whether to print a star or a space based on the current row and column numbers. The outer loop moves to the next line after each row is printed.

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

Complexity

  • The time complexity of these programs depends on the size of the pattern being printed.

  • For patterns with n rows and columns, the time complexity is generally *O(n^2) *since we need to iterate over all rows and columns.

Applications

  • These programs can be used for educational purposes to teach programming concepts such as loops and conditional statements.

  • They can also be used to create ASCII art or as part of larger programs that require pattern generation.

Questions

  1. Can these programs be modified to print patterns using different characters?
  2. How can we optimize these programs to run faster?
  3. Can these programs be extended to print more complex patterns?

Conclusion

In this article at OpenGenus, we have explored 20 different Python programs that can be used to print various patterns. These programs demonstrate the versatility and power of Python as a programming language and provide a starting point for further exploration and learning.

20 Python Programs to Print Different Patterns
Share this