In this shot, we will learn how to generate a left arrow pattern with the use of alphabets in Python.
We can generate different patterns using Python, once we have developed a strong grip over the concepts involving loops. Here, we will use simple for
loops and alphabets to generate a left arrow pattern.
To execute a left arrow pattern using Python programming, we will use 3 for
loops, one for the upper half, one for the middle, and the last for the lower half.
Let’s look at the code snippet given below:
# Number of Rows n=9 # Upper Part for i in range((n - 1) // 2, 0, -1): for j in range(i): ch = chr(64+i) print(" ", end = " ") print(ch) # Middle Part for i in range(n): ch = chr(65+i) print(ch, end = " ") print() # Lower Part for i in range(1, ((n - 1) // 2) + 1): for j in range(i): ch = chr(64+i) print(" ", end = " ") print(ch) print()
In the code written above:
In line 2, we take the input for the number of rows.
In lines 6–10, we create a for
loop to generate the upper portion of the arrow.
In lines 13–16, we create another for
loop to generate the middle portion of the arrow.
In lines 19–24, we create a for
loop to generate the lower portion of the arrow.
In lines 8, 14, and 21, we use the end statement to stay on the same line.
We use the print()
statement to move on to the next line.
RELATED TAGS
CONTRIBUTOR
View all Courses