Python provides two different kinds of division – one is a floating-point division, and the other one is an integer division or floor division. If we want our answer to have decimal values, we use ‘/,’ and if we wish our answer to be the floor value (integer), we should use a double slash in Python.
Floor value is the closest value (must be less) or equal to the given number. For example- if we get to use a single slash (floating division) like this – 5/2, we get 2.5, but if we use a double slash like this- 5//2, we get the answer as 2. ‘2’ is just lesser than ‘2.5’ as we discussed above. Let us know more about double slash in Python.
Python 2 vs. Python 3
In Python 2.7, we did not have the concepts of two different kinds of division operators. We just used to single slash to get the floor value.
Python 2.7 program-
print 5/2
print -5/2
Output-
2
-3
It is different from what we are used to in python3. But in python2.7, when we divided –5/2, we got –3. Usually, we would have gotten –2.5, but as –3 is just smaller than –2.5, we have this answer.
But it is not like we cannot print output as float values. To print values as the float in python2.7, we can do the following.
print 5.0/2.0
print -5.0/2.0
Output-
2.5
-2.5
But in python3, if we want our answer in float, we can use a single slash (‘/’). And to get the answer in integer, we have to use a double slash (“//”) in Python.
# Integer Division Positive Value
a=5
b=2
print(a//b)
# Integer Division Negative Value
a=-5
b=2
print(a//b)
Output-
2
-3
#Float Division Positive Value
a=5
b=2
print(a/b)
# Float Division Negative Value
a=-5
b=2
print(a/b)
Output-
2.5
-2.5
We have seen how we used forward double slash (‘//’) in Python to perform integer division. Now, let us see how we can use double slash ‘\’ to avoid backslash problems in the string.
Comparing the double slash of Python with other programming languages
Each programming language tackles integer/integer division differently. If we try to perform 5/2 in C++ or in Java, we will get the following result-
C++
#include <iostream>
using namespace std;
int main()
{
cout<<5/2;
return 0;
}
Output-
2
Java-
public class Division{
public static void main(String []args){
System.out.println(5/2);
}
}
Output-
2
As you must have observed, unlike Python, we are getting 2.5 as the output here. And if we want our result in floating points, we need to do a divide like this- 5.0/2.
Python Double Forward slash
To include ‘\’ in our string like a path –
“C:\Users\Owner\Documents\ashwini” in python. If we just print like this-
print(“C:\Users\Owner\Documents\ashwini”)
The output will be –
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
So, we have to use a double backslash (‘\’) instead of (‘\’).
s="C:\Users\Owner\Documents\ashwini"
print(s)
Output-
C:\Users\Owner\Documents\ashwini
To print \n and \t which are newline and tab, we have to use the double backslash in Python
print("\n and \t")
Output- \n and \t
When we need to open a file, we use a double backslash in the path. For example, if we have a file at location – C:\Users\Owner\Documents\ashwini\ and the file name is xyz.txt. Let’s see how can we open this file –
file=open("C:\Users\Owner\Documents\ashwini\xyz.txt")
print(file.read())
Output- This is 1st line
Must Read
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
Conclusion
We now know how vital each operator in Python is. If there were no double slash operator in Python, it would have been challenging for us to print escape characters such as ‘\.’ Not only this, but the concept of integer division has also proved to be very handy for programmers.
Try to run the programs on your side, and let us know if you have any queries.
Happy Coding!