Pascal triangle pattern in Python
In this post, you will learn different ways to print the Pascal triangle pattern using the Python programming language.
Pascal triangle is a number pattern that reflects a triangle. Firstly, 1 is set at the top, and afterward, we begin placing the numbers in a three-sided design. The numbers that we get in each progression are the addition of the last two numbers. It is like the concept of triangular numbers. Each new number lies between two existing numbers and below them, and its value is the sum of the two numbers above it.
Algorithm to Print Pascal's Triangle in Python
- Get an input value from the user.
- Using a for loop that ranges from 0 to n-1, append the sub-lists into the list.
- Then append 1 to the sub-lists.
- Then use a for loop to determine the value of the number inside the triangle.
- Print the Pascal’s triangle according to the format.
- Exit
Python program to print Pascal's Triangle
In the given Python program, we use the nested for loop to print the Pascal triangle pattern. First, we ask the user to enter the number of rows that the Pascal triangle has. We use a for loop which ranges from 0 to a number of rows. Inside the loop, append sub-lists into an empty list. Then append 1 to all the sub-lists. The for loop determines the value of the number inside the triangle, which is the sum of the two numbers above it. The other for loop prints the Pascal's triangle.
num = int(input("Enter number of rows: "))
x=[]
for i in range(num):
x.append([])
x[i].append(1)
for j in range(1,i):
x[i].append(x[i-1][j-1]+x[i-1][j])
if(num!=0):
x[i].append(1)
for i in range(num):
print(" "*(num-i),end=" ",sep=" ")
for j in range(0,i+1):
print('{0:6}'.format(x[i][j]),end=" ",sep=" ")
print()
Output1:
Enter number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Output2:
Enter number of rows: 7
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
Python program to print Pascal's Triangle using the Binomial Coefficient
Here, we have used another method by using the binomial coefficient. All of the lines in the Binomial Coefficient C(line, i) begin with 1 and the ith entry in a row number line. The formula is given below-
C(line, i) = C(line, i-1) * (line - i + 1) / i
Here is the Python program to print the Pascal's triangle using the binomial coefficient.
# Print Pascal's Triangle in Python
# input number
num = 5
for i in range(1, num+1):
for j in range(0, num-i+1):
print(' ', end='')
# first element is always 1
C = 1
for j in range(1, i+1):
# first value in a line is always 1
print(' ', C, sep='', end='')
# using Binomial Coefficient
C = C * (i - j) // j
print()
Output of the above code:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Python program to print Pascal's Triangle by Computing the Power of 11
Here, we will print the Pascal's triangle based on the power of the number 11. This is the most optimised approach to print the Pascal's triangle.
11*0 = 1
11*1 = 11
11*2 = 121
11*3 = 1331
11*4 = 14641
The given code implements the above logic-
# Print Pascal's Triangle in Python
num = int(input("Enter the number of rows:"))
for i in range(num):
print(' '*(num-i), end='')
print(' '.join(map(str, str(11**i))))
Output of the above code:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 6 1 0 5 1
1 7 7 1 5 6 1
Related Articles
Python capitalize first letter of sentence
Python program to multiply two numbers
Fibonacci series in Python with recursion
Find the intersection of two lists in Python
Bubble sort program in Python
Merge sort program in Python
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python add list to list
Inverted star pattern in Python
Selection sort program in Python
Exponential function in Python
Python read csv into list
Python program to sum all the numbers in a list
Python print without newline
Prettytable in Python
Python add list to list
Python convert xml to dict
Calculate a Percentage in Python
Python convert dict to xml
Python weather api
Python raise keyword