Example-1: Division using single slash (/) and double slash (//) operator
Create a python file with the following script to check the difference between the output of the single slash and double slash operator for the division operation. In the script, 5 is defined as the divider value, and 2 is defined as the divisor value. The division result and the type of the result of 5/2, 5//2, 5//2.0, and 5.0//2 will be printed after executing the script.
num1 = 5
# Define the divisor value
num2 = 2
# Divide using single slash
result = num1 / num2
print("The division result of %d/%d = %0.2f" % (num1, num2, result))
print("The type of the result", type(result))
# Divide using double slash
result = num1 // num2
print("The division result of %d//%d = %0.2f" % (num1, num2, result))
print("The type of the result", type(result))
# Divide using double slash and float divisor value
result = num1 // float(num2)
print("The division result of %d//%0.2f = %0.2f" % (num1, num2, result))
print("The type of the result", type(result))
# Divide using double slash and float divider value
result = float(num1) // num2
print("The division result of %0.2f//%d = %0.2f" % (num1, num2, result))
print("The type of the result", type(result))
Output:
The following output will appear after executing the script. The result of 5/2 is appropriate, and the return type is float. The result of 5//2 is not appropriate, and the return type is an integer. The fractional part has been omitted from the output. The result of 5//2.00 is not appropriate, and the return type is float. The fractional part has been omitted from this output also. The result of 5.00//2 is not appropriate, and the return type is float. The fractional part has been omitted from this output also.
Example-2: Replace the path defined by the double slash (//) operator
The backward slash (\) is used to define the path in windows, and slash (/) is used to define the path in Linux operating system. When any windows path is defined in a python variable, then the backward slash (\) is stored by the double slash (\\). So, the double slash (\\) requires to convert into forward-slash (/) to define the path in Linux format. Create a python file with the following script that assigns a windows path into a variable and replaces the double slash of the path with the forward-slash (/). The original and updated paths will be printed after executing the script.
pathVal = r"C:\Windows\System\Speech"
# Print the path value
print("The original path value:\n", pathVal)
# Replace the path by forward slash(/)
updated_path = pathVal.replace("\", "/")
# Print the updated path
print("The updated path value:\n", updated_path)
Output:
The following output will appear after executing the script. The output shows that the windows path has been converted into the Linux path format.
Conclusion:
The use of the double slash (//) operator in Python 3+ has been shown in this tutorial by using simple examples to understand the purposes of using this operator.