Number Pattern Programs in Python

Number Pattern Programs in Python

The objective of this Python post, you will see various Python examples that cover the following:

  • Simple Numbers Pattern Program in Python
  • Pascal’s Triangle Number Pattern Program in Python
  • Half-Pyramid Pattern Program Python With Numbers
  • Number Descending Order Pattern Program in python
  • Binary Numbers Pattern Program in python

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

Output

1  
2 2  
3 3 3  
4 4 4 4  
5 5 5 5 5  

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

Output

1   
1  1   
1  2  1   
1  3  3  1   
1  4  6  4  1   
1  5  10  10  5  1   
1  6  15  20  15  6  1    

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 

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

Output

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

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("&#92r")
 
pattern(5)

Output

        10
       1010
      101010
     10101010
    1010101010

Leave a Reply

Your email address will not be published. Required fields are marked *