Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between '/' and '//' when used for division?

Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:

>>> 6/3 2 >>> 6//3 2 
like image 332
Ray Avatar asked Oct 08 '08 17:10

Ray


People also ask

What is the difference between division '/' and modulus division?

Modulo − Represents as % operator. And gives the value of the remainder of an integer division. Division − represents as / operator. And gives the value of the quotient of a division.

Why is a slash used for division?

The slash is an oblique slanting line punctuation mark /. Once used to mark periods and commas, the slash is now used to represent exclusive or inclusive or, division and fractions, and as a date separator.

What is the difference '/' and operator?

These operators are mathematical operators and both have different uses. / Only perform the division operation in mathematics and returns results as the quotient. While % is known as modulus. / divides and returns the answer.

What is the difference between '/' and operator class 7?

Answer: First of all, / is an assignment operator and // is a comparison operator. / operator is used to assign value to a variable and //operator is used to compare two-variable or constants.


2 Answers

In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.

In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.

Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.

You can find a detailed description at PEP 238: Changing the Division Operator.

like image 86
Eli Courtwright Avatar answered Oct 12 '22 09:10

Eli Courtwright


Python 2.x Clarification:

To clarify for the Python 2.x line, / is neither floor division nor true division.

/ is floor division when both args are int, but is true division when either of the args are float.

like image 44
Yichun Avatar answered Oct 12 '22 10:10

Yichun