As we know that python works on two-division operators which are single slash and double slash. Similarly, python supports two kinds of divisions which are floating-point division and integer division. If the user wishes that the output should be in decimal values, he makes the use of a single slash which is denoted by ‘/’. However, if the user wishes to have an answer as the floor value or integer, he can use a double slash denoted by ‘//’.
For example: 8/3: 2.666 8//3: 2
Python program to use double slash
a=3 b=4 print(a//b) a=-3 b=4 print(a//b)
In the above program, we are calculating the floor value with the help of a division operator called a double slash. The floor of a digit is the value that is nearest, majorly small than the actual value.
As in the program, 3//4 is 1 and when we calculate its floor value, it will be 0. Python 2 supports a single slash division operator however we get to work with double slash since the launch of python 3. The user can simply use ‘/’ if he wants the value in the floating number or the quotient of the floating number. Otherwise, he can use ‘//’ to get the returned value as an integer value. In the ‘//’ case, the factorial part will be discarded and the result will be just integer.
In Python strings, “\” backslash is a special character. We can also term it as the “escape” character. There are a lot of such quick functions that are defined on strings and we call them string methods. For instance, the escape character is used in representing certain whitespace characters: “\t” is a tab, “\n” is a newline, and “\r” is a carriage return.
As we have a below statement where we can understand better.
txt = "We are the people of "India" also called Hindustan."
This will show an error as “Invalid Syntax”. You can check out the error below:
File "<string>", line 1 txt = "We are the people of "India" also called Hindustan." ^ SyntaxError: invalid syntax >
So now, the escape character allows you to use double quotes when you normally would not be allowed:
txt = "We are the people of \“India\” also called Hindustan" print(txt)
We are the people of "India" also called Hindustan.
Python Single slash vs Double slash
The concept of both single slash and double slash in python comes under slash operators.
There is a concept in python called Classic Division. It means that if the operands are both integers, it will perform floor division, while for floating-point numbers, it represents true division. So single slash(/) in python is used to perform classic division whereas double slash in python (//) is used to perform the floor division. Floor division means rounding down to the nearest whole number.
Python Double slash in Path
In python, a double slash does not always work for defining paths. Path manipulation is not so easy which working with division operators. Each language has its own set of rules. Here also the use of single slash ‘/’ and double slash ‘//’ is made differently. So to conclude with the division operators in python, ‘/’ is known as division operator and ‘//’ is known as the floor division operator.
If you have any query regarding this post, please feel free to comment below.