Blog

Blog

Learn How To Make Python Pattern Programs With Examples

Youtube banner Logo

Here are a few examples of pattern programs in Python that you can use as a reference:

1. Triangle pattern:

for i in range(1,6):
    print("*"*i)

This will print a triangle of stars like this:

*
**
***
****
*****

2. Reverse triangle pattern:

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

This will print a triangle of stars in the reverse order, like this:

*****
****
***
**
*

3. Diamond pattern:

for i in range(1,6):
    print(" "*(5-i) + "*"*(2*i-1))
for i in range(4,0,-1):
    print(" "*(5-i) + "*"*(2*i-1))

This will print a diamond-shaped pattern of stars like this:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

4. Pyramid pattern:

for i in range(1,6):
    print(" "*(5-i) + "*"*(2*i-1))

This will print a pyramid-shaped pattern of stars like this:

    *
   ***
  *****
 *******
*********

The pattern programs incorporate a lot of nested loops. So if you are not familiar with loops in python, make sure to check out the detailed tutorial on loops in python.

Star Pattern Programs

Following are a few star pattern programs in python.

1. Pyramid Pattern Program

def pattern(n):
k = 2 * n - 2
for i in range(0,n):
for j in range(0,k):
print(end=" ")
k = k - 1
for j in range(0, i+1):
print("*", end=" ")
print("\r")
pattern(5)

Output:

1*DqURUhmc89HbQ1ANcVH3QA

Reverse Pyramid Pattern Program

def pattern(n):
k = 2*n -2
for i in range(n,-1,-1):
for j in range(k,0,-1):
print(end=" ")
k = k +1
for j in range(0, i+1):
print("*", end=" ")
print("\r")
pattern(5)

Output:

Right Start Pattern Program

def pattern(n):
for i in range(0, n):
for j in range(0, i + 1):
print("* ", end="")
print("\r")
for i in range(n, 0 , -1):
for j in range(0, i + 1):
print("* ", end="")
print("\r")

pattern(5)

Output:

1*C8aPlqjKeJJARtz2rZ5mew

Left Start Pattern Program

def pattern(n):
k = 2 * n - 2
for i in range(0, n-1):
for j in range(0, k):
print(end=" ")
k = k - 2
for j in range(0, i + 1):
print("* ", end="")
print("\r")
k = -1
for i in range(n-1,-1,-1):
for j in range(k,-1,-1):
print(end=" ")
k = k + 2
for j in range(0, i + 1):
print("* ", end="")
print("\r")


pattern(5)

Output:

2. Half-Pyramid Pattern Program

def pattern(n):
for i in range(0,n):
for j in range(0, i+1):
print("* " , end="")
print("\r")

pattern(5)

Output:

Left Half-Pyramid Pattern Program

def pattern(n):
k = 2 * n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 2
for j in range(0, i + 1):
print("* ", end="")
print("\r")

pattern(5)

Output:

1*dUMgURC4GqV8v O6aQihXw

Downward Half-Pyramid Pattern Program

def pattern(n):
for i in range(n, -1, -1):
for j in range(0, i + 1):
print("* ", end="")
print("\r")

pattern(5)

Output:

1*pYM 1MA1SmwYV6LaLT wig

3. Diamond-Shaped Pattern Program

def pattern(n):
k = 2 * n - 2
for i in range(0, n):
for j in range(0 , k):
print(end=" ")
k = k - 1
for j in range(0 , i + 1 ):
print("* ", end="")
print("\r")
k = n - 2
for i in range(n , -1, -1):
for j in range(k , 0 , -1):
print(end=" ")
k = k + 1
for j in range(0 , i + 1):
print("* ", end="")
print("\r")

pattern(5)

Output:

1*

Diamond Star Pattern Program

for i in range(5):
for j in range(5):
if i + j == 2 or i - j == 2 or i + j == 6 or j - i == 2:
print("*", end="")
else:
print(end=" ")
print()

Output:

4. Hourglass Pattern Program

def pattern(n):
k = n - 2
for i in range(n, -1 , -1):
for j in range(k , 0 , -1):
print(end=" ")
k = k + 1
for j in range(0, i+1):
print("* " , end="")
print("\r")
k = 2 * n - 2
for i in range(0 , n+1):
for j in range(0 , k):
print(end="")
k = k - 1
for j in range(0, i + 1):
print("* ", end="")
print("\r")

pattern(5)

Output:

1*IWT8HRQK557amfhUlDn7wA

Number Pattern Programs

Here are a few programs with numeric patterns in java.

1. Simple Numbers Program

def pattern(n):
x = 0
for i in range(0 , n):
x += 1
for j in range(0, i + 1):
print(x , end=" ")
print("\r")
pattern(5)

Output:

1*rwF3z5gqHGQUnABOfS2icQ

2. Pascal’s Triangle Program

def pascal(n):
for i in range(0, n):
for j in range(0, i + 1):
print(function(i, j)," ", end="")
print()

def function(n, k):
res = 1
if (k > n - k):
k = n - k
for i in range(0, k):
res = res * (n - i)
res = res // (i + 1)

return res

pascal(7)

Output:

1*DJFqfuqUn

Half-Pyramid Pattern With Numbers

def pattern(n):
for i in range(1, n):
for j in range(1, i + 1):
print(j, end= " ")
print("\r")
pattern(5)

Output:

1*M0o3ligs 2EiwvstD3OtGA

3. Diamond Pattern With Numbers

def pattern(n):
k = 2 * n - 2
x = 0
for i in range(0, n):
x += 1
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
print(x, end=" ")
print("\r")
k = n - 2
x = n + 2
for i in range(n, -1, -1):
x -= 1
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(0, i + 1):
print(x, end=" ")
print("\r")

pattern(5)

Output:

1*s7d9gHEyn4762d df5WDLQ

Descending Order Pattern Program

def pattern(n):
for i in range(n, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")

print("\r")

pattern(5)

Output:

1*TJEH6Tv6W6aD5PlFmeb5Ag

Binary Numbers Pattern Program

def pattern(n):
k = 2 * n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
print('10', end="")

print("\r")

pattern(5)

Output:

1*2FC AxVkj9Kat hpwyEYVA

Characters Pattern Programs

Here are a few pattern programs in python with characters.

Right Alphabetical Triangle

def pattern(n):
x = 65
for i in range(0, n):
ch = chr(x)
x += 1
for j in range(0, i + 1):
print(ch, end=" ")
print("\r")

pattern(5)

Output:

Character Pattern Program

def pattern(n):
k = 2 * n - 2
x = 65
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
ch = chr(x)
print(ch, end=" ")
x += 1
print("\r")


pattern(7)

Output:

1*x4hYWl qJXCFIjKIQ4f97w

K Shape Character Program

for i in range(7):
for j in range(7):
if j == 0 or i - j == 3 or i + j == 3:
print("*", end="")
else:
print(end=" ")
print()

Output:

1*g2

Triangle Character Pattern Program

def pattern(n):
k = 2 * n - 2
x = 65
for i in range(0, n):
ch = chr(x)
x += 1
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
print(ch, end=" ")
print("\r")


pattern(5)

Output:

1*rbaa5FMt49N 6rbKCaZBtQ

Diamond Shaped Character Pattern Program

def pattern(n):
k = 2 * n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 1
x = 65
for j in range(0, i + 1):
ch = chr(x)
print(ch, end=" ")
x += 1
print("\r")
k = n - 2
x = 65
for i in range(n, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(0, i + 1):
ch = chr(x)
print(ch, end=" ")
x += 1
print("\r")


pattern(5)

Output:

1*o2aKiGKCEXWGC ZbL1nFQA

we have learned how we can implement different patterns in python using asterisks, numbers, and characters with the help of loops in python.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Subscribe to Newsletter

Stay ahead of the rapidly evolving world of technology with our news letters. Subscribe now!