Python Program to Print Pascal Triangle

Write a Python program to print a pascal triangle number pattern using for loop.

from math import factorial

rows = int(input("Enter Pascals Triangle Number Pattern Rows = "))

print("====Pascals Triangle Number Pattern====")

for i in range(0, rows):
    for j in range(rows - i + 1):
        print(end = ' ')
    for k in range(0, i + 1):
        print(factorial(i)//(factorial(k) * factorial(i - k)), end = ' ')
    print()
Python Program to Print Pascal Triangle

This Python example program prints the pascal triangle of numbers using a while loop.

# using while loop
from math import factorial

rows = int(input("Enter Rows = "))

print("====Pattern====")
i = 0
while(i < rows):
    j = 0
    while(j <= rows - i):
        print(end = ' ')
        j = j + 1
    k = 0
    while(k <= i):
        print(factorial(i)//(factorial(k) * factorial(i - k)), end = ' ')
        k = k + 1
    print()
    i = i + 1
Enter Rows = 9
====Pattern====
          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 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.