Do you want to do some mathematical calculations through NumPy in Python? In this NumPy tutorial, I will explain the **np.diff() function in Python**, its syntax, the parameters required, and the return values with some examples.

**To understand the np.diff() function in Python, it’s essential to recognize that it calculates the difference between successive elements of an array. By adjusting its parameters, users can specify the number of times this operation is performed (n), the axis along which the differences are calculated (axis), and optionally prepend or append values.**

## np.diff() function in Python

The **np.diff() function in Python** NumPy library calculates the discrete difference between consecutive elements of an array. An input array computes the output as **a[i+1] – a[i]** for each element **i**, where **i** ranges over the array’s length minus one.

Here’s a brief overview of how **np.diff() function in Python** works:

### NumPy diff() function syntax

The basic syntax of the **np.diff() function in Python** is as follows:

`numpy.diff(arr, n=1, axis=-1, prepend=<no value>, append=<no value>)`

### diff function Python NumPy

Here,

arr | Input array in Python. The differences are calculated along this array. |

n | (optional) The number of times to perform the differentiation in Python. The default is 1. |

axis | (optional) The axis along which the difference is taken in the Python array. By default, it is the last axis. |

prepend | (optional) Value to prepend to a along the specified axis before performing the difference in Python. |

append | (optional) Value to append to a along the specified axis before performing the difference in Python. |

**np.diff() function in Python**.

### Python diff() function in NumPy return value

The **np.diff() function in Python** returns a new array of the same type as a, except along the specified axis where the dimension is smaller by n units. The returned array holds the calculated differences.

## np.diff Python usecases

Let’s see different use cases for the **np.diff() function in Python**:

### 1. np diff function in Python basic use

Calculates the difference between each pair of consecutive elements in a one-dimensional array in Python. For example:

```
import numpy as np
temperatures = np.array([58, 60, 62, 65, 63, 66, 68])
temperature_change = np.diff(temperatures)
print(temperature_change)
```

**Output:**

`[ 2 2 3 -2 3 2]`

The output from running the code in PyCharm is visually represented in the screenshot below.

### 2. numpy.diff function in Python with n Parameter

Computes the second-order difference, which is the difference of the consecutive differences in the array in Python.

```
import numpy as np
stock_prices = np.array([120, 125, 123, 130, 128])
price_change = np.diff(stock_prices, n=2)
print(price_change)
```

**Output:**

`[-7 9 -9]`

Displayed below is a screenshot capturing the outcome of the code execution in the PyCharm editor.

### 3. diff NumPy function in a multidimensional array

Finds the difference along the default last axis (axis=-1) in a multidimensional array in Python, effectively performing the operation on each sub-array.

```
import numpy as np
rainfall = np.array([[0.1, 0.2, 0.0, 0.3],
[0.3, 0.4, 0.5, 0.2],
[0.0, 0.0, 0.1, 0.2]])
daily_change = np.diff(rainfall)
print(daily_change)
```

**Output:**

```
[[ 0.1 -0.2 0.3]
[ 0.1 0.1 -0.3]
[ 0. 0.1 0.1]]
```

The following screenshot illustrates the results obtained from executing the code in the PyCharm editor.

### 4. Python NumPy diff() function with axis=0

Computes the difference between elements along the vertical axis (row-wise) in a multidimensional Python array.

```
import numpy as np
rainfall = np.array([[0.1, 0.2, 0.0, 0.3],
[0.3, 0.4, 0.5, 0.2],
[0.0, 0.0, 0.1, 0.2]])
city_change = np.diff(rainfall, axis=0)
print(city_change)
```

**Output:**

```
[[ 0.2 0.2 0.5 -0.1]
[-0.3 -0.4 -0.4 0. ]]
```

After executing the code in Pycharm, one can see the output in the below screenshot.

### 5. np.diff in Python with axis=1

Calculates the difference between elements along the horizontal axis (column-wise) in a multidimensional array in Python.

```
import numpy as np
rainfall = np.array([[0.1, 0.2, 0.0, 0.3],
[0.3, 0.4, 0.5, 0.2],
[0.0, 0.0, 0.1, 0.2]])
city_change = np.diff(rainfall, axis=1)
print(city_change)
```

**Output:**

```
[[ 0.1 -0.2 0.3]
[ 0.1 0.1 -0.3]
[ 0. 0.1 0.1]]
```

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

## Conclusion

Understanding The **np.diff() function in Python**, that computes differences. And its flexibility with parameters like **n** and **axis** makes it an essential function for data analysis, signal processing, and scientific computing.

You may also like to read:

- How to do NumPy Matrix Multiplication in Python
- NumPy average filter in Python
- ValueError: setting an array element with a sequence error in Python
- How to Create a Matrix in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etcâ€¦ for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.