• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Pascal’s Triangle in Python

Pascal’s Triangle in Python

March 11, 2022 Leave a Comment

In Python, we can easily create Pascal’s Triangle with a loop to create a multi-dimensional list for a given number of rows.

def pascalsTriangle(rows):
    t = []
    for i in range(rows):
        t.append([])
        t[i].append(1)
        for j in range(1,i):
            t[i].append(t[i-1][j-1]+t[i-1][j])
        if i != 0:        
            t[i].append(1)
    return t

print(pascalsTriangle(5))

#Output:
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

To print a generated Pascal’s Triangle, we can loop over the triangle and print the elements.

def pascalsTriangle(rows):
    t = []
    for i in range(rows):
        t.append([])
        t[i].append(1)
        for j in range(1,i):
            t[i].append(t[i-1][j-1]+t[i-1][j])
        if i != 0:        
            t[i].append(1)
    return t

def printTriangle(t):
    printString = ""
    for i in range(0,len(t)):
        printString = " " * (len(t) - i)
        for j in range(0,len(t[i])):
            printString = printString + str(t[i][j]) + " "
        print(printString)

printTriangle(pascalsTriangle(5))

#Output:
     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1

Pascal’s Triangle is a very interesting construct from the field of mathematics. Pascal’s Triangle is a triangular array constructed from summing adjacent elements in preceding rows.

In Python, we can easily create Pascal’s Triangle for a given number of rows with a for loop that loops over the number of rows and sums the adjacent elements from the previous row.

We can easily define a function which will create a multi-dimensional list with each row of the triangle.

Below is an simple Python function which generates Pascal’s Triangle for a given number of rows.

def pascalsTriangle(rows):
    t = []
    for i in range(rows):
        t.append([])
        t[i].append(1)
        for j in range(1,i):
            t[i].append(t[i-1][j-1]+t[i-1][j])
        if i != 0:        
            t[i].append(1)
    return t

print(pascalsTriangle(5))

#Output:
[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]

How to Print Pascal’s Triangle in Python

The example above only shows how to create the numbers in Pascal’s Triangle. We can use Python to print a created Pascal’s Triangle by looping over each row and printing the elements.

We can print Pascal’s Triangle in a triangle so you can easily visualize the pattern and beauty of the construct.

Below are two examples printing Pascal’s Triangle with 5 and 10 rows to the console in Python.

def pascalsTriangle(rows):
    t = []
    for i in range(rows):
        t.append([])
        t[i].append(1)
        for j in range(1,i):
            t[i].append(t[i-1][j-1]+t[i-1][j])
        if i != 0:        
            t[i].append(1)
    return t

def printTriangle(t):
    printString = ""
    for i in range(0,len(t)):
        printString = " " * (len(t) - i)
        for j in range(0,len(t[i])):
            printString = printString + str(t[i][j]) + " "
        print(printString)

printTriangle(pascalsTriangle(5))
print()
printTriangle(pascalsTriangle(10))

#Output:
     1
    1 1
   1 2 1
  1 3 3 1
 1 4 6 4 1

          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

Hopefully this article has been useful for you to learn how to create Pascal’s Triangle in Python.

Other Articles You'll Also Like:

  • 1.  Drop Last Row of pandas DataFrame
  • 2.  Using Python to Create Empty DataFrame with pandas
  • 3.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 4.  Get Substring from String in Python with String Slicing
  • 5.  How to Count Vowels in a String Using Python
  • 6.  Replace Character in String in Python
  • 7.  Using Python turtle Module to Draw Square
  • 8.  Date Format YYYYMMDD in Python
  • 9.  How to Check if a Dictionary is Empty in Python
  • 10.  How to Cube Numbers in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy