Generate Pascal’s Triangle in Python
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
forloops 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
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1Each interior number is sum of two numbers directly above it.
Live preview
Choose row count between 1 and 14 for this preview.
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
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) Multiplicative update
Classic direct coefficient generation for each row.
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.
Build each row from previous row
Directly follows the sum-of-two-above definition using lists.
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
🔄 Input / output
Change the row count argument in either function call.
Edge cases
No output rows
Validate input before printing.
Only top value
Triangle should contain a single 1.
⏱️ Time and space complexity
| Program | Time | Extra space |
|---|---|---|
| Multiplicative method | O(rows^2) | O(1) |
| Additive row method | O(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.
Each entry in Pascal’s triangle is a binomial coefficient “n choose k”, which also appears in (a + b)n expansion.
8 people found this page helpful
