Pascal’s triangle python – Python program to print Pascal’s Triangle

Pascal’s triangle python: Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

Pascal Triangle Definition:

Pascal triangle python: Pascal’s triangle is a lovely shape produced by arranging numbers. The sum of the two numbers above it is used to produce each number. This triangle’s outside edges are always 1. The triangle is depicted in the diagram below.

Explanation:

To illustrate the triangle in a nutshell, the first line is 1. There are two ones in the line after that. The second line is here.

1 2 1 is the third line, which is created by adding the ones from the previous line. Similarly, the number of 1 and 2 in an alternative pattern forms the fourth line, and so on.

Examples of pascal Triangle

Example 1:

Input :

height =5

Output:

       1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1

Example 1:

Input :

height =10

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
  1 9 36 84 126 126 84 36 9 1

Pascal Triangle in Python

Below are the methods to print the pascal triangle.

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Method #1: Implementation Using zip function

Using the oneval variable, we will first initialise the top row in this function. Variable zerovalue=0 is also set to zero. To run the code for n iterations, we’ll use a for loop.

We’ll print the list generated by the oneval variable within the for loop. We’ll now add the onevalue to left and right elements. We’ve also used the zip feature in this case.

Below is the implementation:

def printPascal(height):
    onevalue = [1]
    zero = [0]
    for i in range(height):
        print(*onevalue)
        onevalue = [left+right for left,
                    right in zip(onevalue+zero, zero+onevalue)]


# given height
height = 5
# passing the height as paramter to printPascal function
printPascal(height)

Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Method #2:Using NCR

Approach:

  • Consider the number of rows to be printed; let’s say it’s n.
  • To print the rows, repeat iteration I from 0 to n times.
  • Iterate for j from 0 to (N – 1) in the inner loop.
  • ” “ is a single blank space to be printed.
  • It’s enough to close the inner loop (j loop) for left spacing.
  • Make an inner iteration from 0 to I for j.
  • nCr of I and j should be printed.
  • Close the inner loop.
  • For each inner iteration, print a newline character (\n).

Below is the implementation:

# impoerting factorial fucntion from math module
from math import factorial


def printPascal(height):
    for i in range(height):
        for j in range(height-i+1):

            # for spacing in left
            print(end=" ")

        for j in range(i+1):
            print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")

    # printing in new line
        print()


# given height
height = 5
# passing the height as paramter to printPascal function
printPascal(height)

Output:

      1 
     1 1 
    1 2 1 
   1 3 3 1 
  1 4 6 4 1

Method #3:Using Binomial Coefficients

The following definition of a Binomial Coefficient may be used to optimise the above code the i’th entry in a line number line is Binomial Coefficient C(line, i) and all lines begin with value 1. The idea is to use C to measure C(line, i) using C(line, i-1).

C(line, i) = C(line, i-1) * (line - i + 1) / i

Below is the implementation:

def printPascal(height):
    for i in range(1, height+1):
        for j in range(0, height-i+1):
            print(' ', end='')

        # since first eelement is always 1
        onevalue = 1
        for j in range(1, i+1):

            # first value of every line is 1 always.
            print(' ', onevalue, sep='', end='')

            # using binomial cofficients to calculat using onevalue
            onevalue = onevalue * (i - j) // j
        print()


# given height
height = 5
# passing the height as paramter to printPascal function
printPascal(height)

Output:

       1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1

Related Programs: