How to Print Patterns in Python
How to Print Patterns in Python
Printing patterns is a common exercise in Python programming, especially in coding interviews. The key to printing patterns is using nested loops, where the outer loop controls the number of rows and the inner loop determines the content of each row.
Complete Python Course with Advance topics:-Click here
Basic Concepts of Printing Patterns
- Outer loop: Controls the number of rows.
- Inner loop: Handles the number of columns or elements in each row.
- Whitespace management: Adds spaces to align patterns correctly.
In this tutorial, we will go through various pattern examples, including pyramid, star, and diamond shapes.
1. Simple Pyramid Pattern
n = int(input("Enter the number of rows: "))
for i in range(0, n):
for j in range(0, i + 1):
print("*", end=" ")
print()
Output:
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
2. Reverse Right-Angle Pyramid
rows = int(input("Enter the number of rows: "))
k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end=" ")
k = k - 2
for j in range(0, i + 1):
print("* ", end="")
print()
Output:
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
3. Downward Half-Pyramid
rows = int(input("Enter the number of rows: "))
for i in range(rows + 1, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print()
Output:
Enter the number of rows: 5
* * * * *
* * * *
* * *
* *
*
4. Triangle Pyramid
n = int(input("Enter the number of rows: "))
m = (2 * n) - 2
for i in range(0, n):
for j in range(0, m):
print(end=" ")
m = m - 1
for j in range(0, i + 1):
print("* ", end=' ')
print()
Output:
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
5. Downward Triangle Pattern
rows = int(input("Enter the number of rows: "))
k = 2 * rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(0, i + 1):
print("*", end=" ")
print()
Output:
* * * * *
* * * *
* * *
* *
*
6. Diamond Pattern
rows = int(input("Enter the number of rows: "))
k = 2 * rows - 2
for i in range(0, rows):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
print("* ", end="")
print()
k = rows - 2
for i in range(rows, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(0, i + 1):
print("* ", end="")
print()
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
7. Double Pyramid Pattern
rows = int(input("Enter the number of rows: "))
for i in range(0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print()
for i in range(rows + 1, 0, -1):
for j in range(0, i - 1):
print("*", end=' ')
print()
Output:
Enter the number of rows: 5
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE
Conclusion
Mastering pattern printing in Python strengthens your understanding of nested loops and helps in improving logical thinking. These patterns are frequently asked in interviews, so practice them well!
print star pattern in python using for loop
how to print patterns in python
how to print patterns in python number
how to print patterns in python different
how to print a pattern in python
number pattern in python
how to print patterns in python pyramid pattern in python using for loop
square pattern in python
star pattern in python using while loop
how to print patterns in python pattern programs pdf
reverse pyramid pattern in python
number pattern program in python using for loop
Post Comment