Printing Pascal’s Triangle using Python || The Easy Way!

harsh gupta
4 min readJul 30, 2021

--

To build Pascal’s triangle, start with “1” at the top, then continue placing numbers below it in a triangular pattern.

Each number is the numbers directly above it added together.

[ example(highlighted) — 1+3 = 4 ]

Now From A Programmers Perceptive -

we can see the above triangle in a matrix form -

Consider 0 as a space and green elements as row index and blue elements as col index.

We can easily make this matrix from scratch using python.

Step — 1 — Init a n , 2n+1 dimensional matrix with zeros

Here we are taking n = 5

NOTE — row and col index are for reference only.

Step — 2 — Now we need to init median of 0th row i.e 5th col as 1

Step — 3 — Now If we see we can make 1st row by adding cols from 0th row as following -

row[1][col1] = row[0][col0] + row[0][col2]

i.e row[i][j] = row[i-1][j-1] + row[i-1][j+1]

Step — 4 — Similarly we can make the next rows using step3 formula

NOTE — you need to take j from 1 to 9 not from 0 to 10 as they are already zero and we cannot calculate them. In other words i = 1 to n-1 (as i = 0 is already init) and j = 1 to 2n-1 (as j = 0 and 2n are already zero).

Code -

# number of iterations to make pascals triangle of 
n = int(input("Enter the number of iterations to do : "))
pasMat = []# step 1
# init pas matrix as zeros
for i in range(n):
# last row as n elements + (n - 1) spaces + 2 extra zeros to prevent index error
pasMat.append([0 for _ in range(n + n - 1 + 2)])
"""
ex - this is a matrix of 5 iterations
[[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 2, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 3, 0, 3, 0, 1, 0, 0],
[0, 1, 0, 4, 0, 6, 0, 4, 0, 1, 0]]
last row as 5 elements + n-1 zero or spaces in btw + 2 extra zeros one at start and one at end
"""
# step 2
# init median of row 0 as 1
pasMat[0][(n + n - 1 + 2)//2] = 1
# step 3 and step 4
# calculating the next rows 1 to n
for i in range(1 , n):
# we don't need to calulate the first and last value , it is always zero
for j in range(len(pasMat[i])-1):
# this row value will be previous row's neighbours
pasMat[i][j] = pasMat[i-1][j-1] + pasMat[i-1][j+1]
"""
ex - 0 1 0
(0 + 1)1 1(1 + 0)
"""
newPasTriangle = []# removing the zeros from pascals triangle
for i in pasMat:
tempList = []
for j in i:
if(j != 0):
tempList.append(j)
newPasTriangle.append(tempList)


# pretty printing the triangle
def print_pascals_triangle(triangle):
# finding largest element which is at the median of last row
largest_element = triangle[-1][len(triangle[-1]) // 2]
# finding its length
element_width = len(str(largest_element))
# function to format the row
def format_row(row):
# for element in row , center it using the element width and join all element at last using spaces
return ' '.join([str(element).center(element_width) for element in row])

# finding last row length
triangle_width = len(format_row(triangle[-1]))
for row in triangle: # centering the row according to last row length
print(format_row(row).center(triangle_width))
print_pascals_triangle(newPasTriangle)

Result -

Enter the number of iterations to do : 10
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
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Like and Share this Blog , Read more Awesome blog at https://www.blog.letscodeofficial.com/

Checkout my github profile here -> https://github.com/harshnative

Thanks for reading 😁

--

--

harsh gupta

Passionate Developer from India working on cool new projects everyday. My hobbies include programming, blogging, music etc.