Python program to find difference between two given numbers
This tutorial guides you different ways to find difference between two given numbers using Python programing without knowing which number is biggest between two given numbers.
Python program to find difference between two given numbers
The following are the different methods or ways you can follow to find the difference between two given numbers in Python.
Β
Method 1: abs() built-in function
The built-in function abs() returns the absolute value of a number. Note, the argument can be an integer or floating point number or an object implementing __abs__(). And if the argument is complex number, then its magnitude is returned.
Also check the either x > y or x < y, you will get the difference between two given numbers the same value 1 i.e., this function will always return the net difference between two positive numbers.
For example,
>>> x=5 >>> y=4 >>> abs(4-5) 1 >>> abs(5-4) 1 >>> x=4 >>> y=5 >>> abs(4-5) 1 >>> abs(5-4) 1
Method 2: math.fabs(x)
We can also use mathematical functions as shown below. The function math.fabs(x) return the absolute value of x.
For example,
>>> import math >>> x=4 >>> y=5 >>> math.fabs(x-y) 1.0 >>> x=5 >>> y=4 >>> math.fabs(x-y) 1.0
Method 3: math.dist(x,y)
This mathematical function returns the euclidean distance between two points x and y. Note, the two points must have the same dimension.
For example, to find the difference between two given positive numbers using math.dist().
>>> import math >>> x=[4] >>> y=[5] >>> math.dist(x,y) 1.0 >>> x=[5] >>> y=[4] >>> math.dist(x,y) 1.0
Method 4: max() and min() built-in functions
You can also find the difference using max() and min() Python built-in functions.
For example,
>>> max(x-y, y-x) 1 >>> -min(x-y, y-x) 1
Also, we can use both max() and min() together in the following way.
>>> max(x,y)-min(x,y) 1
That’s it. Hope you had learnt numerous ways to find the difference between two given positive numbers in Python programming languauge.
Hope it helped π
You’ll also like:
- Convert floating point number to fixed point in Python
- How to convert single digit number to double digits string
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Reset jupyter notebook theme to default theme
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- Run a Jupyter Notebook .ipynb file from terminal or cmd prompt
- Amazon Linux AMI : apt-get command not found
- Check if a file or folder exists without getting exceptions ?
- Python program to find the greatest of three numbers
- Putty Fatal Error No supported authentication methods available
- Find which users belongs to a specific group in linux
- Embed HTML within IPython Notebook
- Check if Python Object is a Number ?
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not βstrβ β Python3
- Extract numbers from a string in python
- Java String substring() example program