Star Triangle
2 min readOct 1, 2023
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:
- The first line,
rows = int(6)
, assigns the integer value 6 to the variablerows
. This variable will be used to determine the number of rows in the pyramid. - The second line,
for row in range(rows)
, starts a for loop that will iterate over the values from 0 torows - 1
. This means that the loop will executerows
times. - The third line,
for _ in range(row + 1)
, starts a nested for loop that will iterate over the values from 0 torow
. This means that the inner loop will executerow + 1
times. - The fourth line,
print('*', end=' ')
, prints an asterisk (*) to the console. Theend=' '
argument tells Python to print a space after the asterisk, but not to start a new line. - 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 :