Coders Packet

Playing with triangle of numbers in PYTHON

By Kalki Pareshkumar Bhavsar

Printing patterns of triangles made with series of numbers using the fundamental concepts of PYTHON like print function and for loop only.

Triangles

Hey CodeSpeeder!

Everyone knows about functions like print() and iterative methods like for loop. Very basic, right? Yet enough to build beautiful and creative patterns to display! Explore this article further to code various triangles made using a series of number patterns.

Mountain Triangles

Pattern 1: Mountain shaped triangle pointing towards top

                                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

def mountain_triangle(n):
  for i in range(n):
    row=[]
    for j in range(i, 0, -1):
      row+=[str(j)]
    row=["\t"*(n-i-1)]+row[::-1]+[str(i+1)]+row
    print("\t".join(row))

mountain_triangle(5)

 

Pattern 2: Mountain shaped triangle pointing towards bottom

1      2      3      4      5      4      3      2      1

        1      2      3      4      3      2      1

                1      2      3      2      1

                        1      2      1

                                1    

def mountain_triangle_inverted(n):
  for i in range(n):
    row=[]
    for j in range(n-i-1, 0, -1):
      row+=[str(j)]
    row=["\t"*(i)]+row[::-1]+[str(n-i)]+row
    print("\t".join(row))

mountain_triangle_inverted(5)

 

Right-angled Triangles: TYPE 1

Pattern 1: Right-angled triangle towards left

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

def right_num_triangle1_left(n):
  for i in range(n):
    for j in range(1, i+2):
      print(j, end="\t")
    print()

right_num_triangle1_left(5)

 

Pattern 2: Inverted right-angled triangle towards left

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

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

right_num_triangle1_left_inverted(5)

 

Pattern 3: Right-angled triangle towards right

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

def right_num_triangle1_right(n):
  for i in range(n):
    row="\t"*(n-i-1)
    for j in range(1, i+2):
      row+=str(j)+"\t"
    print(row)

right_num_triangle1_right(5)

 

Pattern 4: Inverted right-angled triangle towards right

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

def right_num_triangle1_right_inverted(n):
  for i in range(n):
    row="\t"*(i)
    for j in range(1, n-i+1):
      row+=str(j)+"\t"
    print(row)

right_num_triangle1_right_inverted(5)

 

Right-angled Triangles: TYPE 2

Pattern 1: Right angled triangle towards left

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

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

right_num_triangle2_left(5)

 

Pattern 2: Inverted right-angled triangle towards left

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

def right_num_triangle2_left_inverted(n):
  for i in range(n):
    for j in range(n-i, 0, -1):
      print(j, end="\t")
    print()

right_num_triangle2_left_inverted(5)

 

Pattern 3: Right-angled triangle towards right

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

def right_num_triangle2_right(n):
  for i in range(n):
    row=""
    for j in range(1, i+2):
      row+="\t"+str(j)
    row="\t"*(n-i-1)+row[::-1] 
    print(row)

right_num_triangle2_right(5)

 

Pattern 4: Inverted right-angled triangle towards right

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

def right_num_triangle2_right_inverted(n):
  for i in range(n):
    row=""
    for j in range(1, n-i+1):
      row+="\t"+str(j)
    row="\t"*(i)+row[::-1] 
    print(row)

right_num_triangle2_right_inverted(5)

 

Right-angled Triangles: TYPE 3

Pattern 1: Right angled triangle towards left

      1
      2      3
      4      5      6
      7      8      9      10
      11    12    13     14      15

def right_num_triangle2_right_inverted(n):
  for i in range(n):
    row=""
    for j in range(1, n-i+1):
      row+="\t"+str(j)
    row="\t"*(i)+row[::-1] 
    print(row)

right_num_triangle2_right_inverted(5)

 

Pattern 2: Inverted right-angled triangle towards left

      11    12    13     14      15
      7      8      9      10
      4      5      6
      2      3
      1

def right_num_triangle3_left_inverted(n):
  count=n*(n+1)//2
  for i in range(n):    
    for j in range(n-i, 0, -1):
      print(count-j+1, end="\t")
    count=count-n+i
    print()

right_num_triangle3_left_inverted(5)

 

Pattern 3: Right-angled triangle towards right

                                      1
                              2      3
                      4      5      6
              7      8      9      10
      11    12     13    14    15

def right_num_triangle3_right(n):
  count=1
  for i in range(n):
    row="\t"*(n-i-1)
    for j in range(i+1):
      row+=str(count)+"\t"
      count += 1
    print(row)
  
right_num_triangle3_right(5)

 

Pattern 4: Inverted right-angled triangle towards right

      11    12     13    14    15
              7      8      9      10
                      4      5      6
                              2      3
                                      1

 

def right_num_triangle3_right_inverted(n):
  count=n*(n+1)//2
  for i in range(n):   
    row="\t"*i 
    for j in range(n-i, 0, -1):
      row+=str(count-j+1)+"\t"
    count=count-n+i
    print(row)

right_num_triangle3_right_inverted(5)

 

I have tried to display the most common types of triangles that are constructed using only one character.

Fact-point!

Your looping fundamentals will surely improve after practicing these patterns.

 

 

Download project

Reviews Report

Submitted by Kalki Pareshkumar Bhavsar (KalkiBhavsar)

Download packets of source code on Coders Packet