What does '//' mean in Python?

王林
Release: 2023-09-01 08:53:02
forward
3350 people have browsed it

\'//\' 在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).

// Operator syntax

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 / -

grammar

first_number// second_number
Copy after login

Floor division

Algorithm (steps)

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.

Example

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)
Copy after login

Output

floor division of inputNumber_1 by inputNumber_2 =  3
Copy after login

Show the difference between // and / operators

Algorithm (steps)

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.

Example

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)
Copy after login

Output

Floor division of inputNumber_1 by inputNumber_2 =  3
Division of inputNumber_1 by inputNumber_2 =  3.3333333333333335
Copy after login

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)

Double slash // Operator function similar to math.floor()

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

Example

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)
Copy after login

Output

Floor division of inputNumber_1 by inputNumber_2 =  3
math.floor of inputNumber_1 by inputNumber_2 =  3
Copy after login

Double Slash // Behind the Scenes of Operator

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

Example

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)
Copy after login

Output

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

in conclusion

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template