Star Triangle

Osama Mohamed
2 min readOct 1, 2023

--

Star Triangle

The Python code provided is used to print a pyramid pattern of asterisks (*).

rows = int(6)
for row in range(rows):
for _ in range(row + 1):
print('*', end=' ')
print()

It works as follows:

  1. The first line, rows = int(6), assigns the integer value 6 to the variable rows. This variable will be used to determine the number of rows in the pyramid.
  2. The second line, for row in range(rows), starts a for loop that will iterate over the values from 0 to rows - 1. This means that the loop will execute rows times.
  3. The third line, for _ in range(row + 1), starts a nested for loop that will iterate over the values from 0 to row. This means that the inner loop will execute row + 1 times.
  4. The fourth line, print('*', end=' '), prints an asterisk (*) to the console. The end=' ' argument tells Python to print a space after the asterisk, but not to start a new line.
  5. The fifth line, print(), prints a new line to the console.

The inner for loop prints row + 1 asterisks on each row of the pyramid. This is because the range() function starts at 0, so the inner loop will iterate from 0 to row.

For example, if row is equal to 2, then the inner loop will print three asterisks (* * *).

The outer for loop iterates over the rows of the pyramid, so the pyramid will have rows rows.

For example, if rows is equal to 6, then the pyramid will have six rows.

Here is an example of the output of the code:

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

Here is the source code on GitHub :

Star Triangle

--

--