In this shot, we will discuss how to generate a left arrow pattern using stars in Python.
Different patterns can be generated using Python, once we have a strong grip over the concepts involving loops. Here we will use simple for
loops to generate a left arrow pattern using stars.
To do this, we will use 3 for
loops: one for the upper half, one for the middle, and the last for the lower half.
Let’s have a look at the code.
# Number of Rowsn=8# Upper Partfor i in range((n - 1) // 2, 0, -1):for j in range(i):print(" ", end = " ")print("*")# Middle Partfor i in range(n):print("*", end = " ")print()# Lower Partfor i in range(1, ((n - 1) // 2) + 1):for j in range(i):print(" ", end = " ")print("*")print()
Line 2: We take the input for the number of rows.
Lines 5-8: We create a for
loop to generate the upper portion of the arrow.
Lines 11-13: We create another for
loop to generate the middle portion of the arrow.
Lines 16-20: We create a for
loop to generate the lower portion of the arrow.
The
end
statement is used to stay on the same line, and theprint()
statement is used to move to the next line.