Pattern Programs

Pattern Programs

The next topic is Pattern Programs. Pattern programs are a great way to demonstrate the power of loops and how they can be used to create visually interesting outputs. The concept of nested loops is often used in pattern printing, and different types of patterns help reinforce the understanding of loops, conditions, and formatting in Python.

Here are 20 different Pattern Programs in Python to explore.


Pattern Programs


1. Right-Angled Triangle Pattern of Stars

Description: Prints a right-angled triangle of stars.

# Program to print a right-angled triangle pattern using stars

n = int(input("Enter the number of rows: "))
for i in range(n):
    for j in range(i + 1):
        print("*", end="")
    print()

2. Inverted Right-Angled Triangle Pattern of Stars

Description: Prints an inverted right-angled triangle pattern of stars.

# Program to print an inverted right-angled triangle pattern using stars

n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
    for j in range(i):
        print("*", end="")
    print()

3. Pyramid Pattern of Stars

Description: Prints a pyramid pattern using stars.

# Program to print a pyramid pattern of stars

n = int(input("Enter the number of rows: "))
for i in range(1, n + 1):
    print(" " * (n - i) + "*" * (2 * i - 1))

4. Diamond Pattern of Stars

Description: Prints a diamond shape using stars.

# Program to print diamond pattern of stars

n = int(input("Enter the number of rows for the upper half: "))
# Upper half of the diamond
for i in range(1, n + 1):
    print(" " * (n - i) + "*" * (2 * i - 1))
# Lower half of the diamond
for i in range(n - 1, 0, -1):
    print(" " * (n - i) + "*" * (2 * i - 1))

5. Number Pyramid Pattern

Description: Prints a number pyramid.

# Program to print number pyramid pattern

n = int(input("Enter the number of rows: "))
for i in range(1, n + 1):
    print(" " * (n - i) + " ".join(str(j) for j in range(1, i + 1)))

6. Inverted Number Pyramid

Description: Prints an inverted number pyramid.

# Program to print an inverted number pyramid pattern

n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
    print(" " * (n - i) + " ".join(str(j) for j in range(1, i + 1)))

7. Square Pattern of Numbers

Description: Prints a square pattern using numbers.

# Program to print a square pattern of numbers

n = int(input("Enter the size of the square: "))
for i in range(1, n + 1):
    for j in range(1, n + 1):
        print(i, end=" ")
    print()

8. Hollow Square Pattern

Description: Prints a hollow square using stars.

# Program to print a hollow square pattern

n = int(input("Enter the size of the square: "))
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()

9. Floyd’s Triangle

Description: Prints Floyd’s Triangle with consecutive numbers.

# Program to print Floyd's Triangle

n = int(input("Enter the number of rows: "))
num = 1
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(num, end=" ")
        num += 1
    print()

10. Butterfly Pattern

Description: Prints a butterfly pattern using stars.

# Program to print butterfly pattern using stars

n = int(input("Enter the number of rows: "))
# Upper part of the butterfly
for i in range(1, n + 1):
    print("*" * i + " " * (2 * (n - i)) + "*" * i)
# Lower part of the butterfly
for i in range(n, 0, -1):
    print("*" * i + " " * (2 * (n - i)) + "*" * i)

11. Hollow Triangle Pattern

Description: Prints a hollow triangle pattern using stars.

# Program to print hollow triangle pattern

n = int(input("Enter the number of rows: "))
for i in range(1, n + 1):
    for j in range(1, i + 1):
        if j == 1 or j == i or i == n:
            print("*", end="")
        else:
            print(" ", end="")
    print()

12. Zigzag Pattern

Description: Prints a zigzag pattern using stars.

# Program to print zigzag pattern using stars

n = int(input("Enter the number of rows: "))
for i in range(n):
    for j in range(n):
        if (i + j) % 2 == 0:
            print("*", end="")
        else:
            print(" ", end="")
    print()

13. Pascal’s Triangle

Description: Prints Pascal’s Triangle.

# Program to print Pascal's Triangle

n = int(input("Enter the number of rows: "))
for i in range(n):
    num = 1
    for j in range(i + 1):
        print(num, end=" ")
        num = num * (i - j) // (j + 1)
    print()

14. Right-Angled Triangle of Numbers

Description: Prints a right-angled triangle with numbers.

# Program to print a right-angled triangle of numbers

n = int(input("Enter the number of rows: "))
for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(j, end="")
    print()

15. Right-Angled Triangle of Alphabets

Description: Prints a right-angled triangle with alphabets.

# Program to print a right-angled triangle of alphabets

n = int(input("Enter the number of rows: "))
for i in range(n):
    for j in range(i + 1):
        print(chr(65 + j), end="")
    print()

16. Inverted Triangle of Stars

Description: Prints an inverted triangle using stars.

# Program to print an inverted triangle of stars

n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
    print("*" * i)

17. Hollow Diamond Pattern

Description: Prints a hollow diamond shape using stars.

# Program to print hollow diamond pattern

n = int(input("Enter the number of rows for the upper half: "))
# Upper half of the diamond
for i in range(1, n + 1):
    print(" " * (n - i) + "*" + " " * (2 * i - 3) + "*" * (i > 1))
# Lower half of the diamond
for i in range(n - 1, 0, -1):
    print(" " * (n - i) + "*" + " " * (2 * i - 3) + "*" * (i > 1))

18. Hourglass Pattern

Description: Prints an hourglass pattern using stars.

# Program to print hourglass pattern

n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
    print(" " * (n - i) + "*" * (2 * i - 1))
for i in range(2, n + 1):
    print(" " * (n - i) + "*" * (2 * i - 1))

19. Diamond of Numbers

Description: Prints a diamond shape with numbers.

# Program to print diamond of numbers

n = int(input("Enter the number of rows: "))
for i in range(1, n + 1):
    print(" " * (n - i) + " ".join(str(j) for j in range(1, i + 1)))
for i in range(n - 1, 0, -1):
    print(" " * (n - i) + " ".join(str(j) for j in range(1, i + 1)))

20. Cross Pattern

Description: Prints a cross pattern using stars.

# Program to print cross pattern using stars

n = int(input("

Enter the size of the cross (odd number): "))
for i in range(n):
    for j in range(n):
        if i == j or i + j == n - 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()

Explanation

These pattern programs use loops, typically nested loops, to print different shapes, numbers, or alphabets in specific patterns. Mastering these patterns helps in understanding how to control loop flow and use spaces effectively to create the desired shapes.

See also  Getting Started with Python: A Collection of Basic Programs

Let me know if you need additional explanation or are ready for the next topic!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Contact Form Demo