One of the many operators in Python is the double slash
// operator, which refers to an integer division (or a division with no remainder/decimals) which is called Floor Division. When dividing two numbers using this
// operator, the result will always be an integer, ignoring the decimal part of the result.
The double slash
// has the same function as the Python method math.floor (which we will discuss later), as its name implies, rounds the result down (rounds to its floor) to the closest integer.
In this article, we will review more thoroughly how the Floor Division differs from a normal division, how the
// operator works with different types of numbers (negative numbers and decimal numbers) and some extra methods similar to a Floor Division.
//and Regular Division
A normal division
/ in Python will always return a number with its decimal part, that is, a floating number, while a floor division will always return an integer number.
The result of regular division (using the
/ operator) is
45/6=7.5, but using
// has floored
7.5 down to
We can see another example as follows:
To understand how to work with negative numbers using floor division, we need to understand that the floor of a positive number is different from the floor of a negative number. For example, the floor of
3, but the floor of
3. For greater understanding, we could say that the floor of a positive number would be the integer below and that the floor of a negative number would be the integer that is above (maintaining the negative sign).
We can see another example as follows using the same numbers as before:
When we work with float numbers using floor division, the result will always be a float number, and as we already know, the result will be the nearest integer rounded to its floor, but in this case, it will be represented by a float number.
Let's have a look at the following float-based floor division example:
As a result, the floor division operation is carried out using floats, and a float with an integer returns the value rounded down to the nearest integer represented by the floats.
In Python, there are several methods similar to operations with
// (floor division). But, how would these other methods be useful to us? It all depends on our use case, in some situations we will find ourselves needing to handle only integers in our projects, and since floor division operations can get both integers and floats, we could use a hand in these cases.
This is where these two additional methods come in, which are
math.ceil, they will allow us to manipulate the result so that it is always an integer.
math.floor()Function in Python
Python comes with a built-in math module that includes a variety of practical calculating tools. The
math.floor() function is one of the math module's built-in features. This function takes a numeric input and rounds it down to the closest integer to return the floor value.
math.floor() is an alternative to the
// operator since they accomplish the same goal in the background, but always returning an integer.
Let's have a look at the following example:
math.ceil()Function in Python
math.ceil() is an alternative to
math.floor() but it will always round up to the nearest whole number rather than down.
The next video will be of great help to better understand what was explained in this What Does // Mean in Python? article