How to Make Python Programs to Print Number Pyramid Star Pattern?

Oct 18, 2022

.

Admin

How to Make Python Programs to Print Number Pyramid Star Pattern?

Hi Dev,

This article goes in detailed on How to Make Python Programs to Print Number Pyramid Star Pattern. I would like to share with you Star Patterns in Python. you can understand a concept of Learn Pattern Programs in Python. This tutorial will give you simple example of create number pyramid pattern in python. follow bellow step for Python – Print Number Star Pattern Programs.

See the following python programs to print number pyramid, star, tringle pattern:

so let's see following examples with output:

Example 1: Simple Numbers Pattern Program in Python


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(4)

Output:

1

2 2

3 3 3

4 4 4 4

Example 2: Pascal’s Triangle Number Pattern Program in Python

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 and n - k):

k = n - k

for i in range(0, k):

res = res * (n - i)

res = res // (i + 1)

return res

pascal(5)

Output:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

Example 3: Half-Pyramid Pattern Program Python 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("")

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("")

pattern(5)

Output:

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

6 6 6 6 6 6

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

Example 4: Number Descending Order Pattern Program in python

def pattern(n):

for i in range(n, 0, -1):

for j in range(1, i + 1):

print(j, end=" ")

print("")

pattern(4)

Output:

1 2 3 4

1 2 3

1 2

1

Example 5: Binary Numbers Pattern Program in python

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(4)

Output:

10

1010

101010

10101010

I hope it can help you...

#Python