Top 18 Python Pattern Programs You Must Know About [Snippets]
We’ve sorted a list of multiple ideas for pattern printing in Python to start your preparations with multiple kinds of Python pattern programs in this list.
Join the DZone community and get the full member experience.
Join For FreePreparing for technical interviews takes a lot of preparation, and it’s highly probable that you might have to create Python pattern programs there. That’s why we’ve sorted a list of multiple ideas for pattern printing in Python to start your preparations.
We have multiple kinds of Python pattern programs in this list, so choose your expertise and experience. Make sure that you understand what part of code does what before you move onto the next pattern. Without a proper understanding of how the system works, you would face a lot of difficulty in explaining its working.
Many times, the interviewer asks for an explanation of how you performed pattern printing in Python. Knowing how everything works will help you in answering those questions effectively.
You’ll find the Python code along with every pattern below:
Pattern #1: Simple Number Triangle Pattern
Pattern:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Code:
rows = 6
for num in range(rows):
for i in range(num):
print(num, end=” “) # print number
# line after each row to display pattern correctly
print(” “)
Pattern #2: Inverted Pyramid of Numbers
Pattern:
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
Code:
xxxxxxxxxx
rows = 5
b = 0
for i in range(rows, 0, -1):
b += 1
for j in range(1, i + 1):
print(b, end=’ ‘)
print(‘\r’)
Pattern #3: Half Pyramid Pattern of Numbers
Pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Code:
x
rows = 5
for row in range(1, rows+1):
for column in range(1, row + 1):
print(column, end=’ ‘)
print(“”)
Pattern #4: Inverted Pyramid of Descending Numbers
Pattern:
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Code:
xxxxxxxxxx
rows = 5
for i in range(rows, 0, -1):
num = i
for j in range(0, i):
print(num, end=’ ‘)
print(“\r”)
Pattern #5: Inverted Pyramid of the Same Digit
Pattern:
5 5 5 5 5
5 5 5 5
5 5 5
5 5
5
Code:
xxxxxxxxxx
rows = 5
num = rows
for i in range(rows, 0, -1):
for j in range(0, i):
print(num, end=’ ‘)
print(“\r”)
Pattern #6: Reverse Pyramid of Numbers
Pattern:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
Code:
xxxxxxxxxx
rows = 6
for row in range(1, rows):
for column in range(row, 0, -1):
print(column, end=’ ‘)
print(“”)
Pattern #7: Inverted Half Pyramid Number Pattern
Pattern:
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
Code:
xxxxxxxxxx
rows = 5
for i in range(rows, 0, -1):
for j in range(0, i + 1):
print(j, end=’ ‘)
print(“\r”)
Pattern #8: Pyramid of Natural Numbers Less Than 10
Pattern:
1
2 3 4
5 6 7 8 9
Code:
xxxxxxxxxx
currentNumber = 1
stop = 2
rows = 3 # Rows you want in your pattern
for i in range(rows):
for column in range(1, stop):
print(currentNumber, end=’ ‘)
currentNumber += 1
print(“”)
stop += 2
Pattern #9: Reverse Pattern of Digits from 10
Pattern:
1
3 2
6 5 4
10 9 8 7
Code:
xxxxxxxxxx
start = 1
stop = 2
currentNumber = stop
for row in range(2, 6):
for col in range(start, stop):
currentNumber -= 1
print(currentNumber, end=’ ‘)
print(“”)
start = stop
stop += row
currentNumber = stop
Pattern #10: Unique Pyramid Pattern of Digits
Pattern:
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
Code:
xxxxxxxxxx
rows = 6
for i in range(1, rows + 1):
for j in range(1, i – 1):
print(j, end=” “)
for j in range(i – 1, 0, -1):
print(j, end=” “)
print()
Pattern #11: Connected Inverted Pyramid Pattern of Numbers
Pattern:
5 4 3 2 1 1 2 3 4 5
5 4 3 2 2 3 4 5
5 4 3 3 4 5
5 4 4 5
5 5
Code:
xxxxxxxxxx
rows = 6
for i in range(0, rows):
for j in range(rows – 1, i, -1):
print(j, ”, end=”)
for l in range(i):
print(‘ ‘, end=”)
for k in range(i + 1, rows):
print(k, ”, end=”)
print(‘\n’)
Pattern #12: Even Number Pyramid Pattern
Pattern:
10
10 8
10 8 6
10 8 6 4
10 8 6 4 2
Code:
xxxxxxxxxx
rows = 5
LastEvenNumber = 2 * rows
evenNumber = LastEvenNumber
for i in range(1, rows+1):
evenNumber = LastEvenNumber
for j in range(i):
print(evenNumber, end=’ ‘)
evenNumber -= 2
print(“\r”)
Pattern #13: Pyramid of Horizontal Tables
Pattern:
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
Code:
xxxxxxxxxx
rows = 7
for i in range(0, rows):
for j in range(0, i + 1):
print(i * j, end=’ ‘)
print()
Pattern #14: Pyramid Pattern of Alternate Numbers
Pattern:
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
Code:
xxxxxxxxxx
rows = 5
i = 1
while i <= rows:
j = 1
while j <= i:
print((i * 2 – 1), end=” “)
j = j + 1
i = i + 1
print()
Pattern #15: Mirrored Pyramid (Right-angled Triangle) Pattern of Numbers
Pattern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Code:
xxxxxxxxxx
rows = 6
for row in range(1, rows):
num = 1
for j in range(rows, 0, -1):
if j > row:
print(” “, end=’ ‘)
else:
print(num, end=’ ‘)
num += 1
print(“”)
Pattern #16: Equilateral Triangle with Stars (Asterisk Symbol)
Pattern:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Code:
xxxxxxxxxx
print(“Print equilateral triangle Pyramid using stars “)
size = 7
m = (2 * size) – 2
for i in range(0, size):
for j in range(0, m):
print(end=” “)
m = m – 1 # decrementing m after each loop
for j in range(0, i + 1):
# printing full Triangle pyramid using stars
print(“* “, end=’ ‘)
print(” “)
Pattern #17: Downward Triangle Pattern of Stars
Pattern:
* * * * * *
* * * * *
* * * *
* * *
* *
*
Code:
xxxxxxxxxx
rows = 5
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(“”)
Pattern #18: Pyramid Pattern of Stars
Pattern:
*
* *
* * *
* * * *
* * * * *
Code:
xxxxxxxxxx
rows = 5
for i in range(0, rows):
for j in range(0, i + 1):
print(“*”, end=’ ‘)
print(“\r”)
Published at DZone with permission of Shriram K. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments