Unleash the Power of Python: Printing Triangles and Beyond

The Art of Printing Half Pyramids

Ever wondered how to create stunning half pyramids using Python? With a few simple lines of code, you can master the art of printing half pyramids using asterisks, numbers, and characters.

Half Pyramid Using Asterisks

Our first example demonstrates how to print a half pyramid using asterisks. The program prompts the user to enter the height of the pyramid, and then uses nested loops to print the desired pattern.


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

Half Pyramid Using Numbers

In this example, we’ll print a half pyramid using numbers. The logic remains the same as before, but this time we’ll print numbers starting from 1 to j, where j ranges from 0 to i.


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

Half Pyramid Using Alphabets

Want to add some flair to your pyramid? Our third example shows how to print a half pyramid using alphabets. By leveraging the ASCII values of characters, we can create a pyramid that’s both visually striking and intellectually stimulating.


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

Inverted Half Pyramids: A New Perspective

Now that we’ve mastered the art of printing half pyramids, let’s flip things around and create inverted half pyramids using asterisks and numbers.


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

Full Pyramids: The Ultimate Challenge

Ready to take your skills to the next level? Our next set of examples will show you how to print full pyramids using asterisks, numbers, and even Pascal’s Triangle.

Full Pyramid Using Asterisks

In this example, we’ll print a full pyramid using asterisks.


n = int(input("Enter the height of the pyramid: "))
for i in range(1, n+1):
    for j in range(1, n-i+1):
        print(" ", end=" ")
    for k in range(1, 2*i):
        print("*", end=" ")
    print()

Full Pyramid of Numbers

Our next example demonstrates how to print a full pyramid of numbers.


n = int(input("Enter the height of the pyramid: "))
for i in range(1, n+1):
    for j in range(1, n-i+1):
        print(" ", end=" ")
    for k in range(1, i+1):
        print(k, end=" ")
    for l in range(i-1, 0, -1):
        print(l, end=" ")
    print()

Inverted Full Pyramid of Asterisks

Want to push your skills to the limit? Our final example shows how to print an inverted full pyramid of asterisks using a total of 4 for loops.


n = int(input("Enter the height of the pyramid: "))
for i in range(n, 0, -1):
    for j in range(1, n-i+1):
        print(" ", end=" ")
    for k in range(1, 2*i):
        print("*", end=" ")
    for l in range(2*i-2, 0, -1):
        print("*", end=" ")
    print()

Bonus Examples: Pascal’s Triangle and Floyd’s Triangle

As a special treat, we’ll also explore how to print Pascal’s Triangle and Floyd’s Triangle using Python.


def print_pascals_triangle(n):
    triangle = [[1 for _ in range(i+1)] for i in range(n)]
    for i in range(2, n):
        for j in range(1, i):
            triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]
    for row in triangle:
        print(' '.join(str(num) for num in row))

def print_floyds_triangle(n):
    num = 1
    for i in range(1, n+1):
        for j in range(1, i+1):
            print(num, end=" ")
            num += 1
        print()

print_pascals_triangle(5)
print_floyds_triangle(5)

Leave a Reply