Home > Article > Backend Development > What does '//' mean in Python?
In this article, we will learn about the // operator in Python in detail.
To perform 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 nearest integer (or whole numbers).
To use the double slash // operator, follow the same steps as for regular division. The only difference is that you use double slash // instead of single slash / -
first_number// second_number
Below are the algorithms/steps that need to be followed to perform the required task. -
Create a variable to store the input number 1.
Create another variable to store the input number 2.
Using the double slash // operator, perform floor division by dividing inputNumber_1 by inputNumber_2 and create another variable to store it. Double slash(//)The operator returns the result as an integer by rounding to the nearest integer.
Print the floor result of dividing inputNumber_1 by inputNumber_2.
The following program uses the // operator in Python to return the base of the first number divided by the second number -
# 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)
floor division of inputNumber_1 by inputNumber_2 = 3
Below are the algorithms/steps that need to be followed to perform the required task. -
Create a variable to store the input number1.
Create another variable to store the input number2.
Use the double slash (//) operator to perform floor division by dividing inputNumber_1 by inputNumber_2. It returns the result as an integer by rounding to the nearest integer
Print the floor result of dividing inputNumber_1 by inputNumber_2
Use single slash(/)Perform division by dividing inputNumber_1 by inputNumber_2. It returns the result as a floating point number.
Print The result of dividing inputNumber_1 by inputNumber_2.
The following program returns floor division and the division of the first number by the second number 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)
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 the result of dividing two numbers down to the nearest integer.
Note - If we perform floor division with negative numbers, the result will still be rounded down (to the nearest integer)
In Python, math.floor() Like the double-slash // operator, rounds a number down to the nearest integer.
Because they perform the same operation 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)
Floor division of inputNumber_1 by inputNumber_2 = 3 math.floor of inputNumber_1 by inputNumber_2 = 3
When you use the //operator to divide two numbers, the __floordiv__() function is called behind the scenes.
The following program shows 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)
Floor division of inputNumber_1 by inputNumber_2 = 3 The floordiv method returns the same result as = 3
You've learned in this tutorial how to use the double slash // operator and how it works behind the scenes. Additionally, you learned about two // operator alternatives: the math.floor() and __floordiv__() functions.
Don't be confused about which one to use. All three floor division methods work in the same way. However, I recommend you use the double slash // operator as it allows you to type less.
The above is the detailed content of What does '//' mean in Python?. For more information, please follow other related articles on the PHP Chinese website!