The Python Floor Division (//) Operator is part of the 3 advanced Arithmetic Operators, together with the Modulus (%) Operator and Exponent (**) Operator.
There are also 4 basic Arithmetic Operators found in Python, which are used to perform basic mathematical operations including the Addition (+), Subtraction (-), Multiplication (*) and Division (/) operators.
The Arithmetic Operators form part of the other Python Operators collection, which includes the Assignment Operators, Comparison Operators, Logical Operators, Identity Operators, Membership Operators and Bitwise Operators.
NOTE: This tutorial requires that Python is already installed, as well as an IDE (Integrated Development Environment), such as PyCharm. It’s also recommended that you have a basic understanding of How to Use Python if this is your first time learning Python.
With that said, let’s get started with this free Python Floor Division Operator tutorial.
How to use the Floor Division Operator in Python
Floor Division is used to divide and round down to the nearest Integer. This is useful when having to divide something that cannot be split into fractions or decimals.
For example, let’s say you have 10 tractors and want to divide them evenly among 3 farmers. Obviously we cannot split or cut up a tractor, so we can only give each farmer a certain number of full tractors.
If we were to use using the Python Division Arithmetic Operator (/) to divide 10 by 3:
print(10 / 3) # “10” and “3” are the “Operands”, while “/” is the “Operator”
#>> 3.3333333333333335
Python outputs a value of “3.3333333333333335”, which is a Floating Point value. Which we can check using the type() Function
x = 10 / 3
print(type(x))
#>> <class 'float'="">
This however wont work, as this is a decimal value and we cannot split a tractor in thirds.
To solve this, we need an output value that is a whole number or integer in Python. Do do that we need to make use of the Floor Division // operator instead:
print(10 // 3) # “10” and “3” are the “Operands”, while “//” is the “Operator”
#>> 3
Using the Floor Division Operator, instead of the Division Operator outputs a value of “3” and not “3.3333333333333335”, which is now an Integer output and not a Floating Point value:
x = 10 // 3
print(type(x))
#>> <class 'int'="">
Note that in this example:
- “10” and “3” are the Operands
- “/” and “//” are the Operators
Floor Division // with Modulus Operator %
We therefore now know that each farmer should receive 3 tractors. If we wanted to work out the remaining tractors, we can use the Modulus Operator (%):
print(10 % 3)
#>> 1
The Modulus Operator tells us that we will have “1” tractor remaining after each farmer has received 3 tractors each.
The link between Floor Division and Modulus is as follows:
- a = b * (a // b) + (a % b)
- 10 = 3 * (10 // 3) + (10 % 3)
We can check and confirm this in Python by printing out the expression:
print(3 * (10 // 3) + (10 % 3))
#>> 10
Or by using the Comparison Operator:
print(10 == 3 * (10 // 3) + (10 % 3))
#>> True
Floor Division and Modulus using the Python divmod() Function
Now that we know that Floor Division and Modulus are linked, I want to show you that there is actually a Python Function that calculates both for you at the same time, called the divmod() Function, which is based off the following equation:
- r = divmod(a, b)
We can again write this in Python, using our example as follows:
floor_div, modulus_div = divmod(10, 3)
print(floor_div) # tractors per farmer
#>> 3
print(modulus_div) # leftover tractors
#>> 1
As we can see, the divmod() Function is a quick way to calculate both the tractors per farmer, as well as the leftovers tractors in one expression, without having to calculate them separately using the Floor Division and then the Modulus operators.
Floor Division Operator using Float values in Python
The above example is done using Whole Numbers, or Integers in Python, let;s see what happens when we use Floating Point (Fractional or Decimal) values for both the Operands.
Let’s first divide 10.74 (float) by 2.88 (float) using the Division Arithmetic Operator.
print(10.74 / 2.88) # float ÷ float value
#>> 3.729166666666667 # outputs float value
Python outputs a value of “3.729166666666667”. As this output value is a decimal, it remains a Float Value in Python.
Now, let’s use the same Operands with Floor Division:
print(10.74 / 2.88) # float // float value
#>> 3.0 # outputs float value
The Floor Division rounded towards the Integer of “3”, however, as we used Floating Point Values for both Operands, the output value is also a Floating Point of “3.0”.
In order to change this Floating Point value to an Integer, we need to make use of the decimal.Decimal module.
Using the decimal.Decimal module to change Floor Division Float to Int
In order to change this Floating Point value to an Integer, we need to make use of the decimal.Decimal: module.
from decimal import Decimal
print(Decimal("10.74") // Decimal("2.88"))
#>> 3
By using the decimal.Decimal Module, Python outputs a Whole Number of “3”, instead of a Floating Point value of “3.0”, when using Floating point values as the Operands in a Floor Division Expression.
While this output of “3” might appear as an Integer, it’s in fact still a decimal.Decimal class, which we can confirm by using the type() Function:
from decimal import Decimal
floor_div = Decimal("10.74") // Decimal("2.88")
print(floor_div)
#>> 3
print(type(floor_div))
#>> <class 'decimal.decimal'="">
This will however still work when doing Arithmetic using other Integers, such as adding seven “+ 7”:
from decimal import Decimal
floor_div = Decimal("10.74") // Decimal("2.88")
print(floor_div + 7)
#>> 10
Although, it will not work when trying to use Floating point values such as “8.2”:
from decimal import Decimal
floor_div = Decimal("10.74") // Decimal("2.88")
print(floor_div + 7 + 8.2)
#>> TypeError: unsupported operand type(s) for +: 'decimal.Decimal' and 'float'
Using the ceil() and floor() Functions to change Floor Division Float to Int
An alternative and better way to convert the output value of a Floor Division expression from a Floating Point to an Integer value, is by using the Python math Module’s ceil() and floor() Functions.
import math
floor_div = math.floor(10.74 / 2.88)
print(floor_div)
#>> 3
print(type(floor_div))
#>> <class 'int'="">
This time the Python type() Function confirms that the output of “3” is in fact an Int. This means that we can now use any of the other Python Arithmetic Operators with it, as well as using any Floating Point or Integer values:
import math
floor_div = math.floor(10.74 / 2.88)
print(floor_div + 7 + 8.2)
#>> 18.2
print(type(floor_div))
#>> <class 'int'="">
The math Module’s ceil() Function will yield similar results, making it an option as well, although the output result will be different:
import math
floor_div = math.ceil(10.74 / 2.88)
print(floor_div + 7 + 8.2)
#>> 19.2
print(type(floor_div))
#>> <class 'int'="">
Note that the output result of “10.74 / 2.88 = 3.729166666666667”, therefore the ceil(3.729166666666667) is “4” and not “3” in the case of floor(3.729166666666667), which is why the output result of the ceil() Function is “19.5 “and not 18.5 as in the case of the floor() Function.
We can therefore conclude that the Floor Division and floor() Functions are identical:
print(10.74 // 2.88 == math.floor(10.74 / 2.88))
#>> False
However, at the same time, Floor Division and ceil() Functions are NOT identical.
Floor Division Operator using Float and Int values in Python
We can then also mix both int and float values as we please in Python, when using the Floor Division Operator:
print(36.75 // 21) # float // int value
#>> 1.0 # outputs float value
Python outputs a value of “1.0”, which is again a Float Value.
When using the Floor Division Arithmetic Operator, to divide a float value with an int value, a float value will always be the output result.
Here again, we can make use of the decimal.Decimal Module, or better yet, the floor() or ceil() Functions from the Python math Module, in order to change this Float to an Int.
import math
floor_div = 36.75 // 21
floor_div = math.ceil(floor_div)
print(floor_div)
#>> 1
print(type(floor_div))
#>> <class 'int'="">
Floor Division Operator using Multiple Float and Int values in Python
Multiple values of both int and float values can be used with the Python Floor Division Operator, in any combination. First let;s look at the output from using the Division Operator:
print(693.4 / 2 / 3.256 / 0.45) # float ÷ int ÷ float ÷ float
#>> 236.62298662298664 # outputs float value
Here again we see the output of a float (decimal) value of “236.62298662298664”.
Now, let’s use Floor Division instead:
print(693.4 // 2 // 3.256 // 0.45) # float // int // float // float
#>> 235.0 # outputs float value
The first thing you will notice is that the output result is “235.0” and not “236.0” as we would expect, so why is this? Well let’s break it down into individual parts:
print(693.4 // 2)
#>> 346.0
print(346.0 // 3.256)
#>> 106.0
print(106.0 // 0.45)
#>> 235.0
Here we can confirm that the output is again “235.0” and the reason for the difference between it and “236.0 is because with each Floor Division taking place along the Expression, Python is not factoring in the decimal values, meaning that the next Floor Division section is using a rounded down number and so forth.
By the time it gets to the last section, the output is below “236.0” and therefore Python rounds it down to 235.0 instead.
As before we can easily convert this output to an Integer using the floor() or ceil(0 Functions:
import math
floor_div = 693.4 // 2 // 3.256 // 0.45
floor_div = math.ceil(floor_div)
print(floor_div)
#>> 235
print(type(floor_div))
#>> <class 'int'="">
#>> 235.0
Python Expressions
All the above examples of using the Floor Division Operator are better known as Python Expressions, which is a combination of Operators and Operands (Integer or Floating Point numbers), which are then interpreted to produce an output value.
The Floor Division Operator can also be combined with the other Arithmetic Operators found in Python, to form an Arithmetic Expression. When this happens, an Expression is evaluated as per the Precedence of its operators.
print(10 // 3 + 6 * 4)
#>> 27
Here the Floor Division Operator is given presence after the Multiplication Operator, but before the Addition operator.
Should we make use of Parentheses () around the Addition Operator, the result will be different, as this is then given precedence:
print(10 // (3 + 6) * 4)
#>> 4
Here the Addition Operator is given presence, before the Floor Division Operator and Multiplication Operators.
Conclusion
I hope you enjoyed this tutorial on the Floor Division Arithmetic Operator in Python. This should give you the starting knowledge you need to easily implement it into your next Python code or project.
Python Division Operator Summary
Here is a summary of what we learnt about the Python Floor Division operator:
- How to use the Floor Division Operator with Integer numbers in Python.
- How Floor Division and the Modulus Operator are linked and can both be used at the same time with the divmod() Function
- How to use the Floor Division Operator with Floating Point numbers in Python.
- How to use the Floor Division Operator with a mix of both Integer and Floating Point numbers in Python.
- How to use the Python math Module’s floor() and ceil() Functions to change a Floor Division output from a Floating Point to an Integer.
- When using the Division Operator with an Operand such as an Integer or Floating Point value, you are creating an Expression in Python. The Floor Division Operator can also be combined with the other Python Arithmetic Operator in an Expression, which is then calculated based on Precedence.
Share the love
If you enjoyed this tutorial content and would like others to benefit from it as well, make sure to share it on your favorite Social Media platform, using the share buttons below.
Subscribe for News & Discount Offers
Subscribe right now to gain instant access to the latest news and tutorials while also being the first to know about, exclusive offers and discounts!