# Difference – NumPy uFuncs (Python Tutorial)

This is a detailed tutorial of the NumPy Difference Universal Functions. Learn the usage of these functions with the help of examples.

## NumPy Difference

When we are trying to find the difference between two values, we are actually subtracting them from each other. When we subtract these two values, we get the difference between them. So we get a discrete difference by subtracting two successive elements with each other.

And in order to find the difference between the values, we use `diff()` the function. so let us take an example to understand it better:

```#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the set of values which we are going to find difference  for
a=np.array([1, 3, 5, 7])
#we will use the diff() function here
c=np.diff(a)
#now we will print the array
print(c)```

Output.

`[2 2 2]`

So here in result, we are getting [2 2 2] which is because of 3-1=2, 5-3=2 and 7-3=2. So this is how we get the discrete difference between the values. Let us take another example :

```#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the set of values which we are going to find difference for
a=np.array([2, 3, 8, 14])
#we will use the diff() function here
c=np.diff(a)
#now we will print the array
print(c)```

Output.

`[1 5 6]`

Here in this example, we are getting the output as [1 5 6] because 3-2=1, 8-3=5 and 14-8=6.

We can aloe perform the operation of difference for n number of times. We can do this by giving parameter. `n`.

Let us take an example to understand it better:

```#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the set of values which we are going to find difference for
a=np.array([1, 3, 5, 7])
#we will use the diff() function here
c=np.diff(a,n=2)
#now we will print the array
print(c)```

Output.

`[0 0]`

So here in this example, we have given a parameter n which defines that the difference will be taken two times. So when the difference is to take for the first time, we get [2 2 2] which is because of 3-1=2, 5-3=2 and 7-3=2. But the second time we get [0 0] because 2-2=0 and 2-2=0 . So we can have n number of differences to get our results.

Let us take another example:

```#now we will import the numpy package and then make an alias as np
import numpy as np
# here we will take the set of values which we are going to find difference for
a=np.array([2,4,6,8,10])
#we will use the diff() function here
c=np.diff(a,n=2)
#now we will print the array
print(c)```

Output.

`[0 0 0]`

So here in this example also first we get [2 2 2 2] because 4-2=2, 6-4=2, 8-6=2 and 10-8=2 . Whereas the second time we get [0 0 0] because 2-2=0 ,2-2=0 and 2-2=0 . So at the end the result is [0 0 0].

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!