Python Programming Examples Tutorial Index

Python Loop Programs

In a programming scenario, a half pyramid is a structure consisting of rows of blocks built with letters or numbers, with each row containing one more block than the previous row. The top row contains one block, the second row contains two blocks, and so on.



We can use a combination of loops and string formatting to print a half pyramid using alphabets in Python. We will use a for loop to iterate through the rows of the half pyramid, and within the loop, we will use another for loop to iterate through the columns.

We will use string formatting for each row to print the alphabet character corresponding to the column number. For example, in the first row, we will print the alphabet character corresponding to column number 1; in the second row, we will print the alphabet characters corresponding to column numbers 1 and 2, and so on.

Here is an example of how to print a half pyramid using alphabets in Python:

Example:

rows = int(input("Enter the number of rows: "))

ascii_value = 65

for i in range(rows):
    for j in range(i+1):
        alphabet = chr(ascii_value)
        print(alphabet, end=" ")
    
    ascii_value += 1
    print("\n")

The program above will print a half pyramid according to the input given by the user, with each line containing one more alphabetic character than the line before. The output of this code will be:

Program Output:

Enter the number of rows: 6
A

B B

C C C

D D D D

E E E E E

F F F F F F


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram