1.
Pyramid Pattern
Print a pyramid pattern of stars.
python
Copy code
n = 5 # Number of rows
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
Output:
markdown
Copy code
***
*****
*******
*********
2. Inverted Pyramid Pattern
Print an inverted pyramid pattern of stars.
python
Copy code
n = 5 # Number of rows
for i in range(n, 0, -1):
print('*' * (2 * i - 1).center(2 * n - 1))
Output:
markdown
Copy code
*********
*******
*****
***
*
3. Right-angled Triangle Pattern
Print a right-angled triangle of numbers.
python
Copy code
n = 5 # Number of rows
for i in range(1, n + 1):
print(' '.join(str(j) for j in range(1, i + 1)))
Output:
Copy code
12
123
1234
12345
4. Diamond Pattern
Print a diamond shape pattern.
python
Copy code
n = 5 # Number of rows for the upper part
for i in range(n):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
for i in range(n - 2, -1, -1):
print(' ' * (n - i - 1) + '*' * (2 * i + 1))
Output:
markdown
Copy code
***
*****
*******
*********
*******
*****
***
5. Hollow Square Pattern
Print a hollow square pattern.
python
Copy code
n = 5 # Size of the square
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print('*', end='')
else:
print(' ', end='')
print()
Output:
markdown
Copy code
*****
* *
* *
* *
*****
6. Fibonacci Pattern
Print the Fibonacci series in a triangular format.
python
Copy code
n = 5 # Number of rows
a, b = 0, 1
for i in range(n):
for j in range(i + 1):
print(a, end=' ')
a, b = b, a + b
print()
Output:
Copy code
11
235
8 13 21 34
7. Pascal’s Triangle
Print Pascal's Triangle.
python
Copy code
n = 5 # Number of rows
for i in range(n):
coef = 1
print(' ' * (n - i - 1), end='')
for j in range(i + 1):
print(coef, end=' ')
coef = coef * (i - j) // (j + 1)
print()
Output:
markdown
Copy code
1
11
121
1331
14641
8. Number Pyramid
Print a number pyramid.
python
Copy code
n = 5 # Number of rows
for i in range(n):
print(' ' * (n - i - 1), end='')
for j in range(1, i + 2):
print(j, end=' ')
print()
Output:
markdown
Copy code
12
123
1234
12345
9. Alphabet Pattern
Print an alphabet pattern.
python
Copy code
n = 5 # Number of rows
for i in range(n):
for j in range(i + 1):
print(chr(65 + j), end=' ')
print()
Output:
css
Copy code
AB
ABC
ABCD
ABCDE
10. Z Pattern
Print a Z pattern using stars.
python
Copy code
n = 5 # Size of Z
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == n - 1 - i:
print('*', end='')
else:
print(' ', end='')
print()
Output:
markdown
Copy code
*****
*
*****