In this tutorial, we will look what Is The Difference Between ‘/’ And ‘//’ When Used For Divide? while Dividing two numbers in Python, Here both the signs have different tasks to perform For example we need a complete answer in decimal the ‘/’ will be used whereas’//’ is used for getting the floor value or we can say Integer Value.
Use Of ‘/’ Operators In Python
There are different arithmetical operators used in python, So on the same note, we have / for dividing two numbers where it is used for separating two numbers to get the absolute value of division in decimal points.
This feature of difference in both the operates is raised when the python version 2.7, the python version newer than 2.7 have this feature to get the result as absolute that is in decimal irrespective of dividing number are whether Integer, Float or decimal.
It will give the result in decimal which was not there in older versions, In older versions, the results were data-driven meaning the type of data will decide the type of result.
If both the Dividend and divider are integers their result would also be integers.
In Java ‘/n’ is used for printing the output on the second line or we can say to change to the line where the control is printing. Mainly it is used when we need to switch the line either for printing in new line or accepting the input from the next line.
So we could conclude that in different programming languages, we have different sets of libraries or reserved keywords which have different meanings for it.
For example-
print (2/3) ----> 0 Python 2.7
print (2/3) ----> 0.6666666666666666 Python 3.5
As we can see in python 2.7 the answer is giving only 0 as it is giving floor value only whereas in the case of 3.5 the answer is absolute in decimals
But the latest version introduced another operator ‘//’ for floor value or the result only in integers.
Use Of ‘//’ Operators In Python
This is used when we only need only floor values or integer values of the answer it was already in all the versions.
Although It gives the answer as per the type of data we are dividing
Take an example of:-
print(5//2) print(5.0//2) print(5//2.0) print(5.0//2.0)
Output:
As we can see in all the cases the answer is only the floor values. And when both dividend and divisor are integers the result also seems to be integers. And if any of them is float the answer is float.
In the case of both the dividend and divider being integers, the answer is also an integer.
Learn More About Arithmetic Operators:
https://stackoverflow.com/questions/183853/what-is-the-difference-between-and-when-used-for-division