Print Pascal’s and Invert Pascal’s Triangle Using Python

Here we are going to see How to print pascal’s triangle in python? and what is pascal’s triangle?. It is one of the most interesting patterns in Mathematics. Pascal’s triangle looks like a neatly arranged stack of numbers. We can also use pascal’s triangle to check if a number is prime or not. Let us see how to print pascal’s triangle in python.

Pascal’s triangle is a neatly arranged stack of numbers and it is a triangular array. The given below number is the sum of the above two numbers. In Pascal’s triangle, all the numbers on the outside are 1. It is also useful to check whether the given number is prime or not.

What is the Pascal’s triangle?

pascal's triangle

The above-shown image is known as pascal’s triangle. We can clearly understand pascal’s triangle using the above image. As we already said the outside number of the triangle is one. And the below number is a sum of the above two numbers. There it shows the sum of the numbers.

What are the uses of the pascal’s triangle?

  • Used to check the given number is prime or not.
  • Used to check how many times heads and tails will combine.
  • show the probability of a combination.

What is the formula for pascal’s triangles?

formula for pascals triangle

How to print pascal’s triangle in python?

Code

n=int(input("Enter no of rows:"))
print('\n')
a=[]
for i in range(n):
    a.append([])
    a[i].append(1)
    for j in range (1,i):
        a[i].append(a[i-1][j-1]+a[i-1][j])
    if (n!=0):
        a[i].append(1)
for i in range(n):
    print("    "*(n-i),end=" ",sep=" ")
    for j in range(0,i+1):
        print("{0:6}".format(a[i][j]),end=" ",sep=" ")
    print()

Explanation

Here we are printing the pascal’s triangle in a simple method. First getting the number of rows from the user. Then creating one empty list named as a. Now creating a for a loop. Using append() function to add up the elements. Creating a nested for loop to get the perfect structure of the pascal’s triangle. If n is not equal to 0 then it appends to a. For that, if a statement is used. Finally creating another for loop to print the pattern of the pascal’s triangle.

Output

Enter no of rows:6


                             1
                              
                          1     1
                          
                      1      2     1
                      
                  1     3     3     1
                  
               1     4     6     4     1
               
            1    5    10    10     5     1

How to print pascal’s triangle in python using function?

Code

def pascalSpot(row,col):
    if (col==1):
        return 1
    if (col==row):
        return 1
    upLeft=pascalSpot(row-1,col-1)
    upRight=pascalSpot(row-1,col)
    return upLeft+upRight
for r in range(1,10):
    for c in range(1,r+1):
        print(pascalSpot(r,c),end=" ")
    print("")

Explanation

Here we are going to print a pascal’s triangle using function. First, create a function named pascalSpot. If a column is equal to one and a column is equal to a row it returns one. For that, if a statement is used. Once calculus figures out the two numbers so the ones in the upper-left and the other in the upper-right. Now we passing row-1, col-1, and row-1, col to the upper-left and upper-right respectively. Next, creating a for loop to print the pattern of the pascal’s triangle. You may think that the output structure looks different. But the properties are the same.

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 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 

How to print pascal’s triangle in a list?

Code

lst=[1] 
for i in range(5): 
    print(lst) 
    mylist=[] 
    mylist.append(lst[0]) 
    for i in range(len(lst)-1): 
        mylist.append(lst[i]+lst[i+1]) 
    mylist.append(lst[-1]) 
    lst=mylist 

Explanation

Now we are going to see how to print the pascal’s triangle using the list. Creating a list named lst. Creating for loop and giving no of rows as a parameter. And then creating another empty list named mylist. Now creating a nested for loop. Using append() function to add the elements in the list. Finally giving as lst=mylist. So that both the lists are the same. Now printing the pascal’s triangle in a list.

Output

[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]

How to print invert pascal’s triangle?

Code

def pascalSpot(row,col):
    if (col==1):
        return 1
    if (col==row):
        return 1
    upLeft=pascalSpot(row-1,col-1)
    upRight=pascalSpot(row-1,col)
    return upLeft+upRight
for r in range(9, 0, -1):
    for c in range(r,0,-1):
        print(pascalSpot(r,c),end=" ")
    print("")

Explanation

Here we are going to print a pascal’s triangle using function. First, create a function named pascalSpot. If a column is equal to one and a column is equal to a row it returns one. For that, if a statement is used. Once calculus figures out the two numbers so the ones in the upper-left and the other in the upper-right. Now we passing row-1, col-1, and row-1, col to the upper-left and upper-right respectively. Next to creating a for loop to print the inverted pascal’s triangle.

Output

1 8 28 56 70 56 28 8 1 
1 7 21 35 35 21 7 1 
1 6 15 20 15 6 1 
1 5 10 10 5 1 
1 4 6 4 1 
1 3 3 1 
1 2 1 
1 1 
1 

FAQs

1. What is mean by pascal’s triangle?

Pascal’s triangle is a neatly arranged stack of numbers and it is a triangular array. The given below number is the sum of the above two numbers. In Pascal’s triangle, all the numbers on the outside are 1.

2. What are the uses of pascal’s triangle?

  • Used to check the given number is prime or not. Used to check how many times heads and tails will combine. show the probability of a combination.
  • Conclusion

    Here we have seen about pascal’s triangle and different ways to print pascal’s triangle in python. Pascal’s triangle is one of the most interesting patterns and it is useful for day-to-day life. The above-shown methods are just examples. We can use some other ways to print it.

    Subscribe
    Notify of
    guest
    0 Comments
    Inline Feedbacks
    View all comments