0% found this document useful (0 votes)
2 views5 pages

All_Python_Pattern_Programs

The document provides a collection of Python pattern programs, showcasing various patterns such as right-angled triangles, pyramids, diamonds, and more. Each pattern is accompanied by code snippets and their respective outputs for a specified number of rows (n). It serves as a resource for learning and practicing Python programming through visual patterns.

Uploaded by

warzonevivo123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

All_Python_Pattern_Programs

The document provides a collection of Python pattern programs, showcasing various patterns such as right-angled triangles, pyramids, diamonds, and more. Each pattern is accompanied by code snippets and their respective outputs for a specified number of rows (n). It serves as a resource for learning and practicing Python programming through visual patterns.

Uploaded by

warzonevivo123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

All Python Pattern Programs

Right-angled triangle (increasing)


n = 5
for i in range(1, n + 1):
print("*" * i)
Output:
*
**
***
****
*****

Right-angled triangle (decreasing)


n = 5
for i in range(n, 0, -1):
print("*" * i)
Output:
*****
****
***
**
*

Pyramid pattern
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))
Output:
*
***
*****
*******
*********

Inverted pyramid pattern


n = 5
for i in range(n, 0, -1):
print(" " * (n - i) + "*" * (2 * i - 1))
Output:
*********
*******
*****
***
*

Diamond pattern
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))
for i in range(n - 1, 0, -1):
print(" " * (n - i) + "*" * (2 * i - 1))
Output:
*
***
*****
*******
*********
*******
*****
***
*

Number triangle
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end="")
print()
Output:
1
12
123
1234
12345

Floyd's Triangle
n = 5
num = 1
for i in range(1, n + 1):
for j in range(i):
print(num, end=" ")
num += 1
print()
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Alphabet Triangle
n = 5
for i in range(n):
for j in range(i + 1):
print(chr(65 + j), end=" ")
print()
Output:
A
A B
A B C
A B C D
A B C D E

Inverted Number Triangle


n = 5
for i in range(n, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print()
Output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Pascal's Triangle
n = 5
for i in range(n):
num = 1
print(" " * (n - i), end="")
for j in range(i + 1):
print(num, end=" ")
num = num * (i - j) // (j + 1)
print()
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Butterfly Pattern
n = 5
for i in range(1, n + 1):
print("*" * i + " " * (2 * (n - i)) + "*" * i)
for i in range(n, 0, -1):
print("*" * i + " " * (2 * (n - i)) + "*" * i)
Output:
* *
** **
*** ***
**** ****
**********
**********
**** ****
*** ***
** **
* *

Hourglass Pattern
n = 5
for i in range(n, 0, -1):
print(" " * (n - i) + "* " * i)
for i in range(2, n + 1):
print(" " * (n - i) + "* " * i)
Output:
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *

Hollow Square
n = 5
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:
* * * * *
* *
* *
* *
* * * * *

You might also like