Pascal’s triangle is a nice shape formed by the arrangement of numbers. Each number is generated by taking the sum of the two numbers above it. The outside edges of this triangle are always 1. The triangle is as shown below.

Briefly explaining the triangle, the first line is 1. The line following has 2 ones. This is the second line.

The third line is `1 2 1`

which is formed by taking sum of the ones in the previous line. Similarly, the forth line is formed by sum of 1 and 2 in an alternate pattern and so on.

## Coding Pascal’s Triangle in Python

Let’s begin by creating the `PascalTriangle`

Function.

In this function, we will initialize the top row first, using the `trow`

variable. We also initialize variable y=0. Now we will use a for loop to run the code for `n`

iterations.

Inside the for loop we will print the list initialized by `trow`

variable. Now we will add the left and right elements of the trow. Along with that, we’ve used the zip function here. The function is shown below.

```
def PascalTriangle(n):
trow = [1]
y = [0]
for x in range(n):
print(trow)
trow=[left+right for left,right in zip(trow+y, y+trow)]
return n>=1
```

Now just give a function call with parameter stating number of rows needed. It is as shown below.

```
PascalTriangle(6)
```

Output of the code is as shown below:

```
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
```

## Conclusion

This comes to the end of our tutorial on the creation of a Pascal’s triangle using Python. Try out this code and let us know your feedback in the comment section below.