. Advertisement .
. Advertisement .
“How can I print Pascal’s triangle in Python?” is a question the ITtutoria support team receives quite often. Today, we will tackle this popular issue with detailed analyses and illustrations.
What Is Pascal’s Triangle?
Pascal’s triangle is a popular mathematics phenomenon, referring to triangular arrays of binomial coefficients, which thrives in algebra, combinatorics, and probability theories.
Most Western countries name it after Blaise Pascal – a famous mathematician from France – even though many mathematicians from Italy, Germany, China, Persia, and India analyzed it several centuries before Pascal did.
Each row in the triangle undergoes conventional enumerations that begin with the “n=0” row from the top (also known as a 0th row). After that, every entry is numbered using “k=0,” starting from the left, bearing stagger relations to adjacent rows’ values.
The formula of this triangle could be described like this:
- On the top row (row 0), we can see unique nonzero entries “1”.
- Then, in every subsequent row, we build the content by adding all the values above, from the left and the right. Blank entries are treated as “0”.
Let’s say the first row’s original number is 1 (by adding 1 and 0). Similarly, the values 3 and 1 in our third row will be combined together to generate the new value “4” in our fourth row.
How Can We Print A Pascal’s Triangle in Python?
We have two main concerns:
- How can we express our entries in the triangle?
- How can we print the triangle using proper formatting and spacing?
This section will analyze both.
1. How Can We Express Our Entries In The Triangle?
It seems an entry in Pascal triangles can be yielded easily via the “nCr” formula. Cannot recall what it is? Then let’s wind back the time to a few years when you were still in high school. “nCr” refers to several ways to pick “r” objects among a quantity of “n” objects.
The formula is:
nCr = n! / ((n – r)! r!)
- “n” represents the total number of objects
- “r” denotes the number of objects taken in one go.
2. How Can We Print The Triangle Using Proper Formatting and Spacing?
In triangles with numRows, we can see that row 1 boasts one entry, while row 2 features two entries, et cetera. Hence, in order to print these patterns as Pascal’s triangle, you have to get I space within row #i. It is not difficult to observe that Python range functions are adopted along with loops for this task.
As these range functions extract endpoints by default, you have to remember to add “1” in every required number from the leading spaces.
That’s how you adjust space and represent entries while printing. The next thing to tackle is to use our function “pascal-tri”:
def pascal_tri(numRows): '''Print Pascal's triangle with numRows.''' if numRows == 1: return [] # base case is reached! else: res_arr = pascal_tri(numRows-1) # recursive call to pascal_tri # use previous row to calculate current row cur_row =  # every row starts with 1 prev_row = res_arr[-1] for i in range(len(prev_row)-1): # sum of 2 entries directly above cur_row.append(prev_row[i] + prev_row[i+1]) cur_row +=  # every row ends with 1 res_arr.append(cur_row) return res_arr
Now, adjust the spaces:
Example (Solution) (cont):
tri_array = pascal_tri(5) for i,row in enumerate(tri_array): for j in range(len(tri_array) - i + 1): print(end=" ") # leading spaces for j in row: print(j, end=" ") # print entries print("\n") # print new line Example (Output):
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
This article has shown our readers how to print Pascal’s triangle in Python. Our guidelines have been very intensive and detailed, with clear examples to facilitate understanding.For other printing tasks in Python (such as dictionary prints), feel free to browse ITtutoria for more.