In this shot, we’ll learn to generate a right arrow pattern using asterisks in Python. We’ll use simple for
loops to generate a right arrow pattern using asterisks.
We’ll use nested for
loops. An outer for
loop will iterate through the rows, and an inner for
loop will iterate through the columns.
Let’s look at the code snippet below:
# Number of Rows n=9 # Upper Portion u = (n - 1) // 2; # Lower Portion l = 3 * n // 2 - 1; for i in range(n): for j in range(n): # Checking conditions to print right arrow if (j - i == u or i + j == l or i == u): print("*", end = ""); # Otherwise print space else: print(" ", end = ""); print()
u
, that is, the condition for drawing the upper portion of the right arrow pattern.l
, that is, the condition for the lower portion of the right arrow pattern.for
loop to iterate through the number of rows.for
loop to iterate through the number of columns.j - i == u
⇒ prints the upper portioni + j == l
⇒ prints the lower portioni == u
⇒ prints the middle portionThe end
statement is used to stay on the same line. And the print()
statement is used to move to the next line.
RELATED TAGS
CONTRIBUTOR
View all Courses