Floor Division in Python
When dividing two integer values in the Python programming language, floor division is employed, and the result is rounded to the nearest number. The only difference between floor division and regular division is that it always outputs the largest integer. The // sign is being used in mathematics to denote floor division. To compute floor division, various programming languages have a specific inbuilt method or expression. These include:
- The C++ programming language has a floor() function that we may utilize.
- The Java programming language has a floor() function that we would employ.
- Python’s // operator is a tool that we can employ to perform the floor division.
Syntax of the Floor Division
The syntax for using the floor division is given as follows:
- r represents the calculated value by using the floor division.
- variable1 represents the dividend.
- variable2 represents the divisor.
The Floor Division (//) operation that will be explained in this article is being used in the Python programming language.
Example no 1
Let’s look at an illustration of how floor division operates.
y = 6
l = x // y
m = x / y
print("The value obtained by floor division:", x, "//", y, "=", l)
print("The value obtained by normal division:", x, "/", y, "=", m)
At the beginning of the code, we initialize two variables, “x” and “y”. We have given these variables values “45” and “6”, respectively. Now, we will be utilizing the // operator. This operator is applied to get the value of floor division. This calculated value will be saved in a variable “l”. Then we will compare the value obtained by floor division with the value obtained by normal division.
So, we have been using the / operator to do a normal division. This value would be saved in variable “m”. In the end, we want to display the values obtained by floor division and normal division, so we call the print() function.
Example no 2
In this example, we will observe how the // operator and the floor() method works.
i = 89
j = 4
a = floor(i / j)
e = i // j
print("The value obtained by utilizing floor() function:", a)
print("The value obtained by utilizing // operator:", e)
First of all, we will integrate the floor() method from the math header file. We have assigned the values “89” and “4” to “i” and “j” variables accordingly. The floor() function will be used in the following step. This function is used to determine the value of the floor division. The variable “a” will store this determined value. The value acquired by using the floor() method and the values calculated by floor division will then be evaluated.
The double-backslash (//) symbol would be used to do floor division in Python. Variable “e” may store this value. Finally, we will present both values calculated by using the floor() method and floor division, so we invoke the print() method.
From the output of the above-mentioned code, we have observed that the values obtained by using the floor() method and the // operator will be the same.
Example no 3
Negative values could also be used to divide floors. When dealing with negative values, the outcome has always been rounded to the nearest value integer. Some users can be perplexed by the idea that rounding down non-positive values means deviating from zero. Let’s examine an instance of floor division using negative values.
z = 3
r = y // z
print("We get the result of floor Division:", y, "//", z, "=", r)
We are going to declare two variables named “y” and “z”. We have specified random values for these variables. The variable “y” holds a negative value, and the variable “z” has a positive integer. Here we declare a new variable “r”, and this variable stores the resultant value. To terminate the code, we have to show the obtained value with the help of the print() method.
Example no 4
In this illustration, we employ floor division and modulo. Modulo is a mathematical model mostly related to floor division. Modulo can alternatively be defined as the remaining value obtained after dividing two integer values. We may use it to calculate how many leftovers there are. The percentage (percent) operator in Python will be used to compute the modulo. Let’s look at an example that depicts the association between floor division and modulo.
Having 95 apples and 6 people, we will use the floor division to determine how many apples each person receives.
persons = 6
applesperperson = numofapples // persons
print("Total apples:", numofapples)
print("Total persons:", persons)
print("The number of apples each person eats:", applesperperson)
Here we have to initialize the variables “numofapples” and “persons” at the commencement of the program. There are a total of 95 apples, and the persons who want to eat an apple are 6. Now we divide the apples between every person by utilizing the floor division (// operator).
In the next step, we will apply the print() method three times: the first print statement displays the total apples, the second print statement prints the total persons, and the last print method shows the number of apples each person wants to eat.
We have talked about the use of the// operator in this article. There are various operators in Python that are utilized for specific functionalities. Python’s floor division functionality enables users to divide any two integer values and round the outcome to the nearest whole number. An operation can be carried out by a Python operator expression on single or maybe more operands. An attribute or value with which we execute the operation is known as an operand. The value of floor division is obtained by using the // operator. In this article, four different instances have been implemented. We acquire the value of floor division obtained by employing the // operator in these instances. In one example, we divide a negative number using the // operator. The modulo operator and the // operator have been used in the last illustration.