Python Program to Print Pascal Triangle
A Pascal’s Triangle is a triangle of numbers where the sum of the two numbers that are above and to the left and right of a number is equal to that number. It is initiated with a 1 at the top, and each subsequent row consists of one more number than the last row.
Example of how Pascal’s Triangle looks to 5 rows:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Structure of Pascal’s Triangle
- The triangle is symmetrical.
- The first and last number of any row is
1. - Any other number in row
nis the sum of the two numbers directly above it in rown-1.
Python Program to Print Pascal’s Triangle
def print_pascals_triangle(n):
# Initialize an empty list to hold the rows of the triangle
triangle = []
for i in range(n):
# Start each row with a single 1
row = [1]
# Fill the row with appropriate values (except the first and last)
if i > 0:
for j in range(1, i):
# Add the two numbers directly above in the triangle
value = triangle[i - 1][j - 1] + triangle[i - 1][j]
row.append(value)
# End each row with a single 1
row.append(1)
# Append the completed row to the triangle
triangle.append(row)
# Print the triangle
for row in triangle:
print(" " * (n - len(row)), end="") # Add spacing for alignment
print(" ".join(map(str, row)))
# Input: number of rows for Pascal's triangle
num_rows = int(input("Enter the number of rows for Pascal's Triangle: "))
print_pascals_triangle(num_rows)
Detailed Explanation of the Code
1. Function Definition
def print_pascals_triangle(n):
- The function
print_pascals_triangle(n)takes one argumentn, which specifies the number of rows in Pascal’s Triangle.
2. Triangle Initialization
triangle = []
- An empty list
triangleis created to store all the rows of Pascal’s Triangle.
3. Outer Loop for Rows
for i in range(n):
- This loop iterates
ntimes, where each iteration corresponds to a row in Pascal’s Triangle.
4. Row Initialization
row = [1]
- Each row starts with a
1.
5. Filling the Row
if i > 0:
for j in range(1, i):
value = triangle[i - 1][j - 1] + triangle[i - 1][j]
row.append(value)
row.append(1)
- For rows after the first row (
i > 0), the middle values are calculated as the sum of two values directly above it in the previous row. - The loop
for j in range(1, i)iterates over the indices where the sum needs to be calculated. - The value is calculated using:
triangle[i - 1][j - 1] + triangle[i - 1][j]
- The row ends with another
1.
6. Adding the Row to the Triangle
triangle.append(row)
- The completed row is added to the
triangle.
7. Printing the Triangle
for row in triangle:
print(" " * (n - len(row)), end="") # Add spacing for alignment
print(" ".join(map(str, row)))
- Each row is printed with appropriate spacing for alignment to create the triangular structure.
- The function
map(str, row)converts all numbers in the row to strings so they can be joined and printed.
8. Taking User Input
num_rows = int(input("Enter the number of rows for Pascal's Triangle: "))
print_pascals_triangle(num_rows)
- The program asks the user for the number of rows and then calls the function to print Pascal’s Triangle.
Example Output
Input:
Enter the number of rows for Pascal's Triangle: 5
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Key Concepts Covered:
- Nested Loops: In this method, the outer loop computes rows, while the inner one calculates elements of a row.
- Operations Lists: Appending values to rows of the triangle, retrieving previous rows.
- String Formatting: It is used for spacing to align the triangle properly.
- Dynamic Programming: Compute Pascal’s Triangle row by row, using answers to previous rows.