What does '//' mean in python?


In this article, we will learn about the // operator in Python in detail.

To do floor division in Python, use the double slash // operator. This // operator divides the first number by the second number and rounds the result to the closest integer (or whole number).

Syntax of // Operator

To utilize the double slash // operator, follow the same steps as in regular division. The only difference is that you use a double slash // instead of a single slash / −

Syntax

first_number// second_number

Floor Division

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Create a variable to store the input number 1.

  • Create another variable to store the input number 2.

  • Use the double slash // operator, to perform the floor division by dividing inputNumber_1 by inputNumber_2 and create another variable to store it. The double-slash(//) operator returns the result as an integer by rounding off to the nearest integer.

  • Print the result of the floor division of inputNumber_1 by inputNumber_2.

Example

The following program returns the floor division of the first number by the second one using the// operator in Python −

# input number 1 
inputNumber_1 = 10
# input number 2
inputNumber_2 = 3
# performing floor division by dividing inputNumber_1 by inputNumber_2
# it returns the result as an integer by rounding off to the nearest integer
result_number = inputNumber_1 // inputNumber_2

# printing the result of floor division of inputNumber_1 by inputNumber_2
print("floor division of inputNumber_1 by inputNumber_2 = ", result_number)

Output

floor division of inputNumber_1 by inputNumber_2 =  3

Showing the Difference Between // and / Operators

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Creating a variable to store the input numbe1.

  • Creating another variable to store the input numbe2.

  • Use the double slash(//) operator to perform the floor division by dividing inputNumber_1 by inputNumber_2. It returns the result as an integer by rounding off to the nearest integer

  • Print the result of the floor division of inputNumber_1 by inputNumber_2

  • Use the single slash(/) to perform division by dividing inputNumber_1 by inputNumber_2. It returns the result as a floating-point number.

  • Print the result of the division of inputNumber_1 by inputNumber_2.

Example

The following program returns the floor division and division of the first number by the second one using the // and / operators in Python −

# input number 1 
inputNumber_1 = 10
# input number 2
inputNumber_2 = 3

# performing floor division by dividing inputNumber_1 by inputNumber_2 using //
# it returns the result as an integer by rounding off to the nearest integer
result_floordiv = inputNumber_1 // inputNumber_2
 
# printing the result of floor division of inputNumber_1 by inputNumber_2
print("Floor division of inputNumber_1 by inputNumber_2 = ", result_floordiv)

# performing division by dividing inputNumber_1 by inputNumber_2 using /
# it returns the result as a floating-point number 
result_div = inputNumber_1 / inputNumber_2

# printing the result of the division of inputNumber_1 by inputNumber_2
print("Division of inputNumber_1 by inputNumber_2 = ", result_div)

Output

Floor division of inputNumber_1 by inputNumber_2 =  3
Division of inputNumber_1 by inputNumber_2 =  3.3333333333333335

The above code shows that the double slash(//) operator rounds down the result of the division of two numbers to the nearest whole number.

NOTE − If we do floor division with a negative number, the result would still be rounded down(nearest integer)

Double Slash // Operator Functions Similarly to math.floor()

In Python, math.floor(), like the double slash // operator, rounds down a number to the nearest integer.

Example

Because they do the same thing behind the scenes, math.floor() is an alternative to the // operator.

# importing math module
import math

# input number 1 
inputNumber_1 = 10
# input number 2
inputNumber_2 = 3

# performing floor division by dividing inputNumber_1 by inputNumber_2 using //
# it returns the result as an integer by rounding off to the nearest integer
result_floordiv = inputNumber_1 // inputNumber_2
 
# printing the result of floor division of inputNumber_1 by inputNumber_2
print("Floor division of inputNumber_1 by inputNumber_2 = ", result_floordiv)

# performing division by dividing inputNumber_1 by inputNumber_2 using /
# it returns the result as a floating-point number 
result_mathfloor = math.floor(inputNumber_1 / inputNumber_2)

# printing the result of the division of inputNumber_1 by inputNumber_2
print("math.floor of inputNumber_1 by inputNumber_2 = ", result_mathfloor)

Output

Floor division of inputNumber_1 by inputNumber_2 =  3
math.floor of inputNumber_1 by inputNumber_2 =  3

Behind the Scenes of the Double Slash // Operator

When you use the // operator to divide two numbers, the __floordiv__() function is called behind the scenes.

Example

The following program shows the how the // operator works −

# importing math module
import math

# input number 1 
inputNumber_1 = 10
# input number 2
inputNumber_2 = 3

# performing floor division by dividing inputNumber_1 by inputNumber_2 using //
# it returns the result as an integer by rounding off to the nearest integer
result_floordiv = inputNumber_1 // inputNumber_2
 
# printing the result of floor division of inputNumber_1 by inputNumber_2
print("Floor division of inputNumber_1 by inputNumber_2 = ", result_floordiv)

# performing division by dividing inputNumber_1 by inputNumber_2 using /
# it returns the result as a floating-point number 
floordiv = inputNumber_1.__floordiv__(inputNumber_2)

# printing the result of the division of inputNumber_1 by inputNumber_2
print("The floordiv method returns the same result as = ", floordiv)

Output

Floor division of inputNumber_1 by inputNumber_2 =  3
The floordiv method returns the same result as =  3

Conclusion

You've learned how to utilize the double slash // operator and how it works behind the scenes in this tutorial. In addition, you learned about two // operator alternatives: math.floor() and the __floordiv__() function.

Don't get mixed up on which to use. The three methods of floor division all work in the same way. However, I recommend that you utilize the double slash // operator because it allows you to type less.

Updated on: 31-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements