In this article, you will learn how to print the Butterfly Pattern in Python language of the numbers using for loop statement.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Enter the height of the pattern: 5 This the butterfly pattern: 1 1 12 21 123 321 1234 4321 123454321 1234 4321 123 321 12 21 1 1 |
You should have knowledge of the following topics in Python programming to understand this program:
- Python
input()
function - Python
for
loop statement - Python
print()
function
In this program, we used normal functions and statements to print the butterfly pattern in Python language.
Python Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # Butterfly Pattern in Python language of the numbers using for loop rows, height, digits, space = None, None, None, None print ("Enter the height of the pattern: ", end="") height = int (input ()) print ("\nThis the butterfly pattern:\n\n") for rows in range (1, height): print (end="\t") for digits in range (1, rows + 1): print (digits, end="") for space in range (1, (2 * (height - rows)) + 1): print (end=" ") print (end="\b") for digits in range (rows, 0, -1): print (digits, end="") print (end="\n") for rows in range (height, 0, -1): print (end="\t") for digits in range (1, rows + 1): print (digits, end="") for space in range (1, (2 * (height - rows)) + 1): print (end=" ") print (end="\b") for digits in range (rows, 0, -1): print (digits, end="") print (end="\n") |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Enter the height of the pattern: 5 This the butterfly pattern: 1 1 12 21 123 321 1234 4321 123454321 1234 4321 123 321 12 21 1 1 |