Working With Python Double Slash Operator

Python provides two different kinds of division – one is floating-point division, and the other one is an integer division or floor division. If we want our answer with decimal values, we use ‘/,’ and if we wish our answer as the floor value (integer), we should use a double slash in python.

Floor value is the value, which is the closest (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 we use 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 operator. We just used to single slash for getting the floor value.

Python 2.7 program-

print 5/2
print -5/2
python double slash
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 got –2.5, but as –3 is just smaller than –2.5, we have got 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 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 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 that unlike python, here we are getting 2.5 as the output. And if we want our result in floating points we need to do 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:

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!

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments