Skip to content

Python Double Slash (//) Operator

  • by

Python Double Slash (//) Operator is used to perform floor division. It divides the first number by the second number and rounds the result down to the nearest integer (or whole number).

firstNum // secondNum

Python Double Slash (//) Operator

Simple example code.

num1 = 12
num2 = 9
num3 = num1 // num2

print("floor division of", num1, "by", num2, "=", num3)

Output:

Python Double Slash Operator

Division using single slash (/) and double slash (//) operator

num1 = 5
num2 = 2

result = num1 / num2

print("The division result of %d/%d = %0.2f" % (num1, num2, result))
print(type(result))

# Divide using double slash
result = num1 // num2

print("The division result of %d//%d = %0.2f" % (num1, num2, result))
print(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(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(type(result))

Output:

The division result of 5/2 = 2.50
<class 'float'>
The division result of 5//2 = 2.00
<class 'int'>
The division result of 5//2.00 = 2.00
<class 'float'>
The division result of 5.00//2 = 2.00
<class 'float'>

The floor division operator is useful when you want to get the quotient of a division operation without any fractional part. It’s often used in situations where you need to divide something into equal parts, especially when dealing with integers.

Do comment if you have any doubts or suggestions on this Python operator topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading