Python Pascal’s Triangle Program

Beginner
7 min read
Updated: Oct 2026
2 programs
Live preview

What Is Pascal’s Triangle?

Pascal’s triangle is a triangular array of numbers. Edges are always 1. Every inner value is the sum of the two numbers above it. Row n (0-based) holds C(n, 0) … C(n, n).

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

Rule: value = left_parent + right_parent
(Edges have only one parent → stay 1)
RowsOutput
11
31 / 1 1 / 1 2 1
5Up through 1 4 6 4 1

How to Generate Pascal’s Triangle in Python

Four clear steps — then pick an additive row build or a multiplicative coefficient fill.

  1. Decide how many rows r to print (start rows at 0 or 1 — stay consistent).
  2. Start with row [1].
  3. For each next row, edges are 1; inner cells are the sum of adjacent values from the previous row.
  4. Or fill one row at a time with C = C * (n - k) // (k + 1) (multiplicative update).
MethodIdeaTimeBest for
AdditiveSum adjacent values from the previous rowO(r²)Teaching the geometric rule
MultiplicativeBuild each row with binomial updatesO(r²)No previous-row storage

Pseudocode

Pseudocode
function print_pascal(rows):
    prev = [1]
    print prev

    for i from 1 to rows - 1:
        curr = [1]
        for j from 1 to length(prev) - 1:
            curr.append(prev[j - 1] + prev[j])
        curr.append(1)
        print curr
        prev = curr

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
EdgesAlways 1
Inner cellSum of the two parents above
Binomial linkRow n, spot k = C(n, k)
SymmetricC(n, k) = C(n, n - k)
Fast updateC = C * (n - k) // (k + 1)
Classic checkRow 4 → 1 4 6 4 1

Live Preview

Enter how many rows to print and watch the triangle grow.

Positive integers from 1 to 12. Tap a chip or type a value.

Result
rows = 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Worked Walkthrough — r = 5

Build each new row from the previous one by adding neighbors.

Calculation
Row 0: 1

Row 1: 1 , 1

Row 2: 1 , (1+1)=2 , 1
       → 1 2 1

Row 3: 1 , (1+2)=3 , (2+1)=3 , 1
       → 1 3 3 1

Row 4: 1 , (1+3)=4 , (3+3)=6 , (3+1)=4 , 1
       → 1 4 6 4 1

Python Programs

Start with the additive method so you can explain every cell. Then use the multiplicative update when you want each row without storing the previous one.

Example 1 — Additive Rows

Keep the previous row and build the next by summing adjacent values.

python
def print_pascal(rows):
    if rows <= 0:
        print("Enter a positive number of rows.")
        return

    prev = [1]
    print(" ".join(map(str, prev)))

    for i in range(1, rows):
        curr = [1]
        for j in range(1, len(prev)):
            curr.append(prev[j - 1] + prev[j])
        curr.append(1)

        print(" ".join(map(str, curr)))
        prev = curr


print_pascal(5)

How It Works

  1. Reject non-positive row counts before looping.
  2. Start with prev = [1] — the top of the triangle.
  3. Each new row begins and ends with 1.
  4. Inner values are prev[j - 1] + prev[j] — the classic “two parents” rule.
  5. Replace prev with curr and repeat until rows lines are printed.

Example 2 — Multiplicative Row

Fill each row with the binomial update — no previous row required.

python
def print_pascal(rows):
    if rows <= 0:
        print("Enter a positive number of rows.")
        return

    for n in range(rows):
        c = 1
        parts = []
        for k in range(n + 1):
            parts.append(str(c))
            c = c * (n - k) // (k + 1)
        print(" ".join(parts))


print_pascal(5)

How It Works

  1. Outer loop chooses row index n from 0 to rows - 1.
  2. Start each row with c = 1, which is C(n, 0).
  3. Append c, then update c = c * (n - k) // (k + 1) to get C(n, k + 1).
  4. Integer division is exact here because binomial ratios stay whole.
  5. Same triangle as Example 1, with O(1) extra space beyond the printed line.

Edge Cases & Pitfalls

Check these before calling the solution done.

r ≤ 0

Invalid rows

Ask for a positive row count before printing.

Indexing

0-based vs 1-based

Be clear whether row 1 is [1] or [1 1]. Stay consistent in code and tests.

Growth

Large rows

Values grow fast. Python ints expand automatically, but printing huge rows still costs time and memory.

Division order

Multiply before divide

Use c * (n - k) // (k + 1), not divide first, so intermediate results stay exact.

Time and Space Complexity

ProgramTimeExtra space
Additive rowsO(r²)O(r) for previous row
Multiplicative rowO(r²)O(1) beyond output

Both print about 1 + 2 + … + r = r(r+1)/2 numbers, so time is quadratic in the row count.

Key Takeaways

  • Rule: edges are 1; inner = sum of the two parents above.
  • Binomial: row n is C(n, 0) … C(n, n).
  • Update: c = c * (n - k) // (k + 1) fills a row fast.
  • Test set: 5 rows end with 1 4 6 4 1.

One line: each inner entry is the sum of the two numbers above it — which is also C(n, k).

Frequently Asked Questions

A triangle of numbers where each edge value is 1, and every inner value is the sum of the two numbers directly above it. Row n holds the binomial coefficients C(n, 0) … C(n, n).
Build each row from the previous by adding adjacent pairs (additive method), or fill a row with the multiplicative update C = C * (n - k) // (k + 1).
C(n, 0) and C(n, n) are always 1. Geometrically, nothing sits outside the triangle to add into those corners.
Yes. Each binomial update lands on an exact integer, so Python's // is correct for that step.
Printing r rows needs O(r²) time. Additive uses O(r) extra space for the previous row; multiplicative can use O(1) extra beyond the output.

Next: Perfect Number

Check whether a number equals the sum of its proper divisors.

Perfect number tutorial →

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.

7 people found this page helpful