Here’s an example Python code to print a downward half-pyramid pattern with stars:
# Set the number of rows num_rows = 5 # Loop through each row from num_rows to 1 for i in range(num_rows, 0, -1): # Loop through each column from 1 to i for j in range(1, i+1): # Print a star and a space, separated by nothing print("* ", end="") # Print a newline character to move to the next row print()
In this code, we use a nested loop to print the downward half-pyramid pattern. The outer loop iterates over each row from the specified number of rows down to 1, using the range()
function with a step value of -1. The inner loop iterates over each column from 1 to the current row number, using the range()
function with the i+1
argument. For each column, we print a star and a space, separated by nothing, using the end
parameter of the print()
function. Finally, we use another print()
statement without any arguments to add a newline character and move to the next row.
When you run this code, it should output the downward half-pyramid pattern with stars:
* * * * * * * * * * * * * * *
Note that you can change the value of num_rows
to print the pattern with a different number of rows.