Python Pattern programs for Practice
Write a Python program to display three different patterns:
1. An equilateral triangle pattern with ‘*’ using 8 rows.
2. An empty square pattern with only the border formed by ‘+’ signs using 8 rows.
3. An equilateral pyramid pattern with even numbers starting from 2 using 10 rows.
Follow these steps to complete these Python challenges:
1. For the equilateral triangle pattern, use nested loops to iterate through the rows and columns. Print spaces for left-side padding and ‘*’ symbols to form the triangle.
2. For the empty square pattern, use nested loops to iterate through the rows and columns. Print ‘+’ signs for the border and spaces for the inner part of the square.
3. For the equilateral pyramid pattern, use nested loops to iterate through the rows and columns. Print spaces for left-side padding and ‘*’ symbols in a decrementing pattern, starting from 2.
Solutions
### 1) Equilateral Triangle
rows = 8
for i in range(1, rows + 1):
print(" " * (rows - i) + "* " * i)
## 2) Empty Square
rows = 8
for i in range(rows):
for j in range(rows):
if i == 0 or i == rows - 1 or j == 0 or j == rows - 1:
print("+", end=" ")
else:
print(" ", end=" ")
print()
### 3) Equilateral pyramid
n = 10
num = 2
for i in range(1, n+1):
# Print leading spaces
for j in range(n-i):
print(" ", end="")
# Print even numbers
for j in range(i):
print(num, end=" ")
num += 2 # Increase num by 2 to get the next even number
# Move to the next line
print()