Do you need a quick and easy way to print out Pascal triangle? If so, you’ll be happy to know that there are python programs that can help you out in generating Pascal Triangles.

In this article, we will teach you how to create a **Pascal Triangle in Python** using different methods. This tutorial is going to be easy to follow, and we will also provide helpful examples. So if you want to learn how to print the pascal triangle, then keep reading.

Table of Contents

## What is Pascal Triangle ?

The Pascal triangle(named after Blaise Pascal, a famous French Mathematician and Philosopher) is an arrangement of numbers that give the coefficients of any binomial expression. The outer side of the triangle has a value **of 1**, and to create the inner pattern, we add the above two numbers to generate the series of numbers in each step.

###### Uses of Pascal Triangle

- It is widely used in probability theory, combinatorics, and algebra.
- It is used to find the possibilities of heads and tails.
- It is used to find combinations of certain things.
- It is used to check the given number, whether the number is prime or not.

## Pascal Triangle in Python: 5 Methods

The Pascal Triangle is a simple and practical tool that can be used for solving mathematical problems. There are many ways to print Pascal triangle in Python. The most common five methods are listed below:

- By using nCr formula.
- By using built-in function ( pascalSpot).
- By using Binomial Coefficient.
- By computing power of 11.
- By printing Pascal triangle in a List.

## Method 01: Using Formula (nCr)

By importing the math module function, we will calculate a Pascal Triangle by using For loop, and formula of nCr is used to compute the possible arrangements of digits.

###### Algorithm:

- Input n for a number of rows to be printed,
- Make an empty list [i] to store numbers.
- Create an outer loop a from 0 to n times to print rows of triangle.
- Initiate an inner loop for b from 0 to a to create the inner pattern of triangle.
- Calculate the triangle by the formula: nCr= n!/ c(n-r)! r!
- Close the inner loop.

Following example shows how to print Pascal’s triangle by nCr formula.

```
# print pascal triangle
# import math function
#n is the no of rows in triangle
n = int(input("Enter the number of rows:"))
for a in range(n):
for b in range(n-a+1):
print(end=" ")
for b in range(a+1):
# formula to calculate Pascal Triangle nCr = n!/((n-r)!*r!)
print(factorial(a)//(factorial(b)*factorial(a-b)), end=" ")
print()
```

#### Output

## Method 02: Using Function (pascalSpot)

PascalSpot is a built-in- debugger that allows you to inspect variables at runtime or step through the code line by line. This makes it easy to create and analyze Pascal programs. In addition, it offers modules for object-oriented programming.

Lets learn the following code for better understanding of pascalSpot.

```
# print Pascal Triangle by using formula
# import math function
def pascalSpot(r,c):
if (c==1):
return 1
if (c==r):
return 1
upLeft=pascalSpot(r-1,c-1)
upRight=pascalSpot(r-1,c)
return upLeft+upRight
for r in range(1,6):
for c in range(1,r+1):
print(pascalSpot(r,c),end=" ")
print("")
```

#### Output

## Method 03: Using Binomial Coefficient

The binomial coefficient, also known as the Bernoulli number or the Pascal’s Triangle in mathematics, is used to calculate the probability of success when multiple events are combined.

Here is an example showing how to print Pascal triangle using binomial coefficient.

```
# print pascal triangle by using binomial coefficient
# import math function
n = int(input("Enter the number of rows:"))
for a in range(1, n+1):
for b in range(0, n-a+1):
print(' ', end='')
C = 1
for b in range(1, a+1):
print(' ', C, sep='', end='')
# using Binomial Coefficient formula
C = C * (a - b) // b
print()
```

#### Output

## Method 04: Computing Powers of 11

By analyzing the pattern of the Pascal Triangle, it is concluded that it can also be done by computing the powers of 11

Logically, following code snippet shows how it computes.

```
# print Pascal Triangle by computing power 11
# import math function
n = int(input("Enter the number of rows:"))
for a in range(n):
print(' '*(n-a), end='')
print(' '.join(map(str, str(11**a))))
```

#### Output

## Method 05: Using Data Structure (List)

A list is a data structure that allows you to store multiple objects in memory. It is similar to an array, but it has the added ability to add/remove elements at any time. Values are stored or displayed in square [ ] brackets.

** **

Let’s understand from the following example.

```
# print Pascal Triangle in a list
# import math function
l=[1]
for a in range(9):
print(l)
list=[]
list.append(l[0])
for a in range(len(l)-1):
list.append(l[a]+l[a+1])
list.append(l[-1])
l=list
```

#### Output

## How to Print Invert Pascal Triangle in Python?

To print the inverted pattern of the Pascal Triangle, formula and method will be the same as before; however, in this, we use loops for outer, inner, and spaces calculation.

Lets have a look on the example.

```
# print invert Pascal Triangle
# import math function
def pascalSpot(r,c):
# r=> row ,, c=> column
# pascalSpot is function to calculate Pascal Triangle
if (c==1):
return 1
if (c==r):
return 1
upLeft=pascalSpot(r-1,c-1)
upRight=pascalSpot(r-1,c)
return upLeft+upRight
for r in range(5, 0, -1):
for c in range(r,0,-1):
print(pascalSpot(r,c),end=" ")
print(" ")
```

#### Output

## FAQs

### What are the patterns found in Pascal’s triangle?

Patterns found in the Pascal triangle are:

- Odd and even pattern
- Symmetry pattern
- Diagonal pattern
- Horizontal sum pattern

### What is the 5th row of Pascal’s triangle?

This row corresponds to adding the square in column three (3*3), plus the square in column four (4*4), plus the square in column five (5*5).

### What is concept of pascal triangle work for combinations?

The basic idea is to use the Pascal Triangle to generate all possible combinations of objects from a given set. This can be helpful when trying to find the value of an unknown combination, or solving other related puzzles.

## Conclusion

This article has discussed what is Pascal triangle and how we can use different ways of printing pascal triangle using Python. I hope the above code examples were helpful in learning different techniques for pascal triangle in Python. Thanks for reading.