Print Left Arrow Pattern & Right Arrow Pattern in Python
Print left arrow pattern & right arrow pattern in python. In this article, you will learn how to print the left and right arrows pattern of stars using the for loop in the python language.
You should have knowledge of the following topics in python programming to understand this program:
- Python
def
keyword - Python
print()
function - Python
for
statement - Python
if
statement - Python
input()
function
In this program, we are using the user-defined function to print the pattern.
Source Code
# Print Left Arrow Pattern & Right Arrow Pattern in Python
def leftRightArrowsPattern(rows):
r, r1, c, row1 = None, None, None, None
row1 = rows
rows = rows * 4
print ("\n----Ths is the left arrow pattern-----\n")
for r in range (1, rows):
# to print the left arrow
if r <= row1:
for c in range (1, (row1 - r) + 1):
print (end=" ")
for c in range (r, row1 + 1):
print (end="*")
print (end="\n")
if r > row1 and r <= row1 * 2:
for c in range (1, (r - row1) + 1):
print (end=" ")
for c in range (1, (r - (row1 - 1)) + 1):
if (r - row1) < row1:
print (end="*")
print (end="\n")
# to print the right arrow
if r > row1 * 2 and r <= row1 * 3:
if r == (row1 * 2) + 1:
print ("\n----Ths is the right arrow pattern-----\n")
for c in range ((r - 1) - (row1 * 2), 0, -1):
print (end=" ")
for c in range ((3 * row1) - (r - 1), 0 ,-1):
print (end="*")
print (end="\n")
if r > row1 * 3:
for c in range ((row1 * 4) - (r + 1), 0, -1):
print (end=" ")
for c in range ((r + 1) - (3 * row1), 0 , -1):
print (end="*")
print (end="\n")
print ("-----Enter the number of rows to print the left & right arrows pattern-----")
rows = int (input ())
if rows > 0:
# calling function to print the pattern
leftRightArrowsPattern(rows)
Output
-----Enter the number of rows to print the left & right arrows pattern-----
4
----Ths is the left arrow pattern-----
****
***
**
*
**
***
****
----Ths is the right arrow pattern-----
****
***
**
*
**
***
****
Explanation
In the above program, we have taken input 4
sizes of the asked pattern and made a custom function named leftRightArrowsPattern()
.
In this function we passed the size of the pattern and make calculations with for loop statement and if condition statement to print the asked pattern.