How to Use // Python Operator

The division operators in Python are / and /. But why is that?

There is, in fact, a backstory to it. In the early versions of Python, there was only one division operator (/). However, how it worked was unclear. It used to return integer values by floor division for integers, but it was returning float values for floats. In Python, there was no true-division operator.

To address this, Python 2.2 added a new floor-division operator (//) that allowed developers to convert their applications to use it wherever floor integer division was required. PEP-238 was used to make this adjustment. Finally, the division operator (/) became a true-division operator in Python 3.

To better grasp Python division operators, let’s look at some easy code examples.

Division Operators in Python 2

// in Python

// will not work if you are using Python 2.1 or a lower version.

Division Operators in Python 3

// Python

The output and explanation are shown in the table below for better understanding.

Division ExpressionPython 2Python 3Explanation
9/244.5Python 2 always returns int and returns the floor value for integers. Python 3 returns a float value.
-9/2-5-4.5Python 2 gives -5 because it returns the floor value.
9.0/24.54.5Both Python 2 and Python 3 return floats, and their behavior is identical.
-9.0/2-4.5-4.5The same as before.
9//244Both Python 2 and Python 3 have the same floor division operator.

//, also known as “Floor Division” in Python, is the division of operands with the result being the quotient with the decimal point removed. The result is either rounded away from zero or floored if any of the operands is negative (towards negative infinity).   

As an example,

9.0//2.0 equals 4.0, and 9//2 equals 4.

The / operator performs integer division in Python. Python’s integer division differs from the integer division provided by certain other languages (such as C) in that it rounds to negative infinity rather than zero. a == (a / b)*b + (a / b)*b + (a / b)*b + (a / b)*b + (a / b)*b + (a / b) (a%b).

When you divide two numbers (using the usual division operator /) in Python 2, floor division is the default behavior.

Python 3 has transformed to make “true” (floating point) division the standard for the division that would otherwise be rounded off, and it will only do “floor” division when explicitly requested since this could end up being surprising (especially if you’re not cautious about the sorts of numbers you receive as function parameters).

In Python 2, you can receive the new behavior by including the from __future__ import division at the top of your files. It is highly recommended.

How do you use the // operator in Python?

  1. The floor division operator in Python is //, while the modulo operator is %.
  2. This equation A = B * (A / B) + (A % B) is always satisfied if the numerator is A and the denominator is B.
  3. Make use of the floor division operator // or the math module’s floor() method to get the floor division of two numbers.  

How does the floor division operator work?

Floor division is similar to regular division, but it yields the largest possible integer. This number is less than or equal to the result of conventional division. This symbol represents the floor function mathematically (⌊ ⌋).

In Python, what is the objective of floor division?

If we want an integer result from division, we should use the / operator (floor division operator). It just removes the fractional element of a conventional division result.

Typical Examples of using // in Python

When you use the // operator, it returns the quotient’s floored value. Integer division, which would round the number, does not operate this way. Instead, the return value is the floor value.

Let’s look at a couple of instances to understand how this works in practice:

Examples of using // in Python

What happens with negative numbers is particularly interesting:

Division operators in python

This makes sense from a logical standpoint. The result will be rounded down (i.e., floored), so while we might expect it to be equal to -2.0, the value will actually be -3.0.

Leave a Comment