Generate Pascal’s Triangle in Python

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Nested loops

What you’ll learn

  • How Pascal’s triangle rows are formed.
  • How to print it using binomial coefficient update.
  • How to generate rows using neighbor-sum method.

Overview

Pascal’s triangle starts with 1 at the top. Every row begins and ends with 1, and middle values come from adding the two values above.

Binomial values

Entry at row i and column j is C(i, j).

Live preview

Choose row count and draw triangle instantly.

Two methods

Multiplicative update and additive row construction.

Prerequisites

Nested loops, integer arithmetic, and Python list basics.

  • Using for loops inside loops.
  • Basic understanding of combinations (optional but helpful).

The idea

Every row grows one item longer. Edge values remain 1 and inner values are sums of two values from previous row.

You can compute directly row-by-row without factorial functions.

First five rows

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

Each interior number is sum of two numbers directly above it.

Live preview

Choose row count between 1 and 14 for this preview.

For very deep rows, values get large quickly.

Live result
Press "Draw triangle".

Algorithm

Goal: print rows from top to bottom with aligned spacing and correct binomial values.

Loop rows

For row i, print leading spaces first.

Compute entries

Either update coefficient formula or sum neighbors from previous row.

Print row

Output values with fixed-width spacing for triangle shape.

📜 Pseudocode

Pseudocode
for i from 0 to rows - 1:
    print leading spaces
    coeff = 1
    for j from 0 to i:
        print coeff
        coeff = coeff * (i - j) / (j + 1)
1

Multiplicative update

Classic direct coefficient generation for each row.

python
def generate_pascals_triangle(rows: int) -> None:
    for i in range(rows):
        coefficient = 1

        for _ in range(rows - i - 1):
            print("   ", end="")

        for j in range(i + 1):
            print(f"{coefficient:6d}", end="")
            coefficient = coefficient * (i - j) // (j + 1)

        print()


generate_pascals_triangle(5)

Explanation

Each row starts with 1 and updates coefficient in-place for next position.

2

Build each row from previous row

Directly follows the sum-of-two-above definition using lists.

python
def generate_pascals_triangle_additive(rows: int) -> None:
    prev: list[int] = []

    for i in range(rows):
        cur = [1] * (i + 1)
        for j in range(1, i):
            cur[j] = prev[j - 1] + prev[j]

        for _ in range(rows - i - 1):
            print("   ", end="")
        for val in cur:
            print(f"{val:6d}", end="")
        print()

        prev = cur


generate_pascals_triangle_additive(5)

Explanation

Middle values are computed from previous row neighbors; edges remain 1.

Notes

Big numbers. Values grow quickly for large row counts.

Python advantage. Integers do not overflow like fixed-width C integers.

❓ FAQ

It is a triangle of numbers where every inner value equals the sum of the two values above it, and edges are always 1.
It is the number of ways to choose k items from n without caring about order.
It computes next value in a row directly without factorial calculations.
Yes, for binomial updates each step lands on an exact integer.
It builds each row from the previous row using sum of adjacent values.
Printing r rows needs O(r^2) time.

🔄 Input / output

Change the row count argument in either function call.

Edge cases

rows ≤ 0

No output rows

Validate input before printing.

rows = 1

Only top value

Triangle should contain a single 1.

⏱️ Time and space complexity

ProgramTimeExtra space
Multiplicative methodO(rows^2)O(1)
Additive row methodO(rows^2)O(rows)

Summary

  • Rule: edges are 1, inside values are neighbor sums.
  • Code patterns: multiplicative coefficient or additive row buffers.
  • Complexity: both methods are quadratic in rows.
Did you know?

Each entry in Pascal’s triangle is a binomial coefficient “n choose k”, which also appears in (a + b)n expansion.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful