Python Programs for Creating Patterns with Loops
Patterns can be created using nested loops, where the outer loop controls the number of rows and the inner loop controls the content of each row. Below are some common examples of pattern programs:
1. Right-Angled Triangle
Pattern:
*
**
***
****
*****
rows = 5
for i in range(1, rows + 1):
print("*" * i)
Explanation:
- Outer loop (
for i
) runs for each row. - The
*
symbol is repeatedi
times for each row.
2. Inverted Right-Angled Triangle
Pattern:
*****
****
***
**
*
rows = 5
for i in range(rows, 0, -1):
print("*" * i)
Explanation:
- Outer loop runs from
rows
down to 1. - The
*
symbol is repeatedi
times for each row.
3. Pyramid
Pattern:
*
***
*****
*******
*********
rows = 5
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
Explanation:
rows - i
spaces are printed before the stars for alignment.2 * i - 1
stars are printed in each row to form the pyramid shape.
4. Inverted Pyramid
Pattern:
*********
*******
*****
***
*
rows = 5
for i in range(rows, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
Explanation:
- Similar to the pyramid but starts with the widest row and decreases.
5. Diamond
Pattern:
*
***
*****
*******
*********
*******
*****
***
*
rows = 5
# Upper part
for i in range(1, rows + 1):
print(" " * (rows - i) + "*" * (2 * i - 1))
# Lower part
for i in range(rows - 1, 0, -1):
print(" " * (rows - i) + "*" * (2 * i - 1))
Explanation:
- Combines the pyramid and inverted pyramid patterns.
6. Number Triangle
Pattern:
1
12
123
1234
12345
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end="")
print()
Explanation:
- Outer loop controls rows.
- Inner loop (
for j
) prints numbers from 1 toi
.
7. Floyd’s Triangle
Pattern:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
rows = 5
num = 1
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(num, end=" ")
num += 1
print()
Explanation:
- A running counter (
num
) is printed and incremented for each number.
8. Pascal’s Triangle
Pattern:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
rows = 5
for i in range(rows):
print(" " * (rows - i), end="")
num = 1
for j in range(i + 1):
print(num, end=" ")
num = num * (i - j) // (j + 1) # Calculate next number
print()
Explanation:
- Uses a mathematical formula to compute the numbers in each row.
9. Hollow Square
Pattern:
*****
* *
* *
* *
*****
size = 5
for i in range(size):
if i == 0 or i == size - 1:
print("*" * size) # Print full row for the first and last row
else:
print("*" + " " * (size - 2) + "*") # Print hollow row
Explanation:
- Outer loop controls rows.
- Prints
*
with spaces for hollow rows.
10. Hollow Pyramid
Pattern:
*
* *
* *
* *
*********
rows = 5
for i in range(1, rows + 1):
if i == rows:
print("*" * (2 * rows - 1)) # Full row at the base
else:
print(" " * (rows - i) + "*" + " " * (2 * i - 3) + ("*" if i > 1 else ""))
Explanation:
- The base row is full of stars.
- For other rows, spaces are printed in the middle to make it hollow.
Summary
These examples showcase how nested loops work together to form various shapes and patterns. Modify the number of rows or the inner logic to create custom patterns!