In this Python tutorial, we will discuss the **python** **dot product** and** cross product**. Also, We will see these below topics as:

- What is
**dot product in python**? - What is Numpy and how to install NumPy in python
- Python dot product without NumPy
- Dot product in python using NumPy
- Dot product of two vectors in python
- Python compute the inner product of two given vectors
- Python dot product of 2-dimensional arrays
- Python cross product of two vectors
- Python dot product of two lists
- Python dot product of two arrays
- Python cross product of 2-dimensional arrays
- Python cross product of 3-dimensional arrays

**Table of Contents**show

## What is Python dot product?

The **Python dot product** is also known as a scalar product in algebraic operation which takes two equal-length sequences and returns a single number.

## What is Numpy and how to install NumPy in python

**Numpy**is a python library used for working with array and matrices.- If you have
**python**and**pip**already installed on a system, then the installation of NumPy is very easy. - Install Numpy by using the command
**“pip install numpy”**in cmd. - Once NumPy is installed, import it in your application by adding
**import**numpy.

## Python dot product without NumPy

If we don’t have a NumPy package then we can define 2 vectors **a** and** b**. Then use** zip** function which accepts two equal-length vectors and merges them into pairs. Multiply the values in each pair and add the product of each multiplication to get the dot product.

**Example:**

```
a = [5, 10, 2]
b = [2, 4, 3]
dotproduct=0
for a,b in zip(a,b):
dotproduct = dotproduct+a*b
print('Dot product is:', dotproduct)
```

After writing the above code, once you will print **” dotproduct “** then the output will be** ”Dot product is: 56”**. It will multiply the values in each pair and add the product into final values.

You can refer to the below screenshot for **python dot product without NumPy**.

## Dot product in python using NumPy

Python provides a very efficient method to calculate the **dot product of two vectors**. By using **numpy.dot()** method, which is available in the **Numpy module**.

**Example:**

```
import numpy as n
a = [5, 10, 2]
b = [2, 4, 3]
dotproduct = n.dot(a,b)
print('Dot product is:', dotproduct)
```

After writing the above code, once you will print **” dotproduct “** then the output will be** ”Dot product is: 56”**. It will calculate the dot product using the dot().

You can refer to the below screenshot for **python dot product using NumPy**.

## Dot product of two vectors in python

**Python dot product **of two vectors a1 and b1 will return the scalar. For two scalars, their dot product is equivalent to a simple multiplication.

**Example:**

```
import numpy as np
a1 = 10
b1 = 5
print(np.dot(a1,b1))
```

After writing the above code, once you will print **” np.dot(a1,b1) “** then the output will be** ” 50 ”**. It will calculate the dot product using the dot().

You can refer to the below screenshot for **python dot product of two vectors**.

## Python compute the inner product of two given vectors

An inner product is a generalization of the dot product. It is a way to multiply vectors together. By using the **dot()** method we can find the inner product.

**Example:**

```
import numpy as n
p
a1 = np.array([5, 6]
)
b1 = np.array([2, 5]
)
print("vectors:")
print(a1)
print(b1)
print("Inner product of vectors:")
print(np.dot(a1,b1))
```

After writing the above code, once you will print **” np.dot(a1,b1) “** then the output will be** ”Inner product of vectors: 40”**. It will compute the inner product of the vectors using the dot().

You can refer to the below screenshot for **python compute the inner product of two given vectors**

## Python dot product of 2-dimensional arrays

If the arrays are **2-dimensional**,** numpy.dot()** will result in matrix multiplication.

**Example:**

```
import numpy as np
p = [[2,5],[3,2]]
q = [[1,0],[4,1]]
dotproduct = np.dot(p,q)
print(dotproduct)
```

After writing the above code, once you will print **” dotproduct “** then the output will be** ”[[22 5] [11 2]]”**. By using the dot() method it returns the matrix product of the two vectors p and q.

You can refer to the below screenshot for **python dot product of 2-dimensional arrays**

## Python cross product of two vectors

To find the **cross product** of two vectors, we will use** numpy cross() **function.

**Example:**

```
import numpy as np
p = [4, 2]
q = [5, 6]
product = np.cross(p,q)
print(product)
```

After writing the above code, once you will print **” product “** then the output will be** ” 14 ”**. By using the **cross() **method it returns the cross product of the two vectors p and q.

You can refer to the below screenshot for python cross product of two vectors.

## Python dot product of two lists

Python provides a very efficient method to calculate the **dot product of two lists**. By using **numpy.dot()** method, which is available in the **Numpy module**.

**Example:**

```
import numpy as n
list1= [10, 3, 2]
list2= [2, 5, 3]
dotproduct = n.dot(list1,list2)
print('Dot product of two list is:', dotproduct)
```

After writing the above code, once you will print **” dotproduct “** then the output will be** ”Dot product of two list is: 41”**. It will calculate the dot product of the two lists** ” list1 and list2″** using the dot().

You can refer to the below screenshot for python dot product of two lists

## Python dot product of two arrays

The function **numpy.dot() **in python returns a dot product of two arrays **arr1 and arr2**. The dot() product returns scalar if both arr1 and arr2 are 1-D.

**Example:**

```
import numpy as np
arr1 = np.array([2,2])
arr2 = np.array([5,10])
dotproduct = np.dot(arr1, arr2)
print("Dot product of two array is:", dotproduct)
```

After writing the above code, once you will print **” dotproduct “** then the output will be** ”Dot product of two array is: 30”**. It will calculate the dot product of the two arrays** ” arr1 and arr2″** using the dot() and it will return a scalar value.

You can refer to the below screenshot for python dot product of two arrays

## Python cross product of 2-dimensional arrays

To find the cross product of 2-dimensional arrays we will use **numpy.cross()** function of numpy library.

**Example:**

```
import numpy as np
p = [[2, 2]
, [3, 1]]
q = [[6, 7]
, [5, 4]]
product = np.cross(p,q)
print(product)
```

After writing the above code, once you will print **” product “** then the output will be** ” [2 7] “**. By using the **cross()** method we will get the cross product of two given vectors p and q.

You can refer to the below screenshot for python cross product of 2-dimensional arrays

## Python cross product of 3-dimensional arrays

To find the cross product of 3-dimensional arrays, we will use **numpy.cross()** function of numpy library.

**Example:**

```
import numpy as np
p = ([3, 2, 5])
q = ([4, 7, 1]
)
product = np.cross(p,q)
print(product)
```

After writing the above code, once you will print **” product “** then the output will be** ” [-33 17 13] “**. By using the **cross()** method we will get the cross product of two given vectors p and q.

You can refer to the below screenshot for python cross product of 3-dimensional arrays

You may like the following Python tutorials:

- Python exit command (quit(), exit(), sys.exit())
- Python input and raw_input function
- Sorting algorithms in Python
- Working with JSON data in Python
- Send email using Python
- Python get an IP Address
- Python – stderr, stdin and stdout
- Python read a binary file

In this tutorial, we learned about ** python dot product and Python cross product** and also we have seen how to use it with an example like:

- What is dot product in python?
- What is Numpy and how to install NumPy in python
- Python dot product without NumPy
- Dot product in python using NumPy
- Dot product of two vectors in python
- Python compute the inner product of two given vectors
- Python dot product of 2-dimensional arrays
- Python cross product of two vectors
- Python dot product of two lists
- Python dot product of two arrays
- Python cross product of 2-dimensional arrays
- Python cross product of 3-dimensional arrays

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.