In this article, we will talk about division operators in Python. The logical quotient of its operands is obtained by using the / (division) operator. Even when both operands will be integers, the remaining fraction is discarded.
Python Integer Division
There are two types of division operators in python.
- Float division: The quotient returned by this operation will always be a float number, regardless of whether the two numbers are integers or not.
- Integer division (Floor division): The quotient produced by this operation depends on the argument. When any of the quantities is float, it gives output in float. It is also called as floor division since the output is floored if any integer is negative.
However, float division is performed by the “/” operator, and integer division is performed by “//”.
- /: Produces a floating value by dividing the number on the left by the value on the right.
5/2=2.5 |
5//2=2 |
- //: Divides the value to the left by the number to the right, rounds the result, and gives whole number.
E.g.
5/2=2.5 |
5//2=2 |
Integer Division in Python
Let’s look at a division example in Python now that we understand how the operators function and what type of division each operator performs.
“Dividing an integer by an integer”
print(“The solution to 6/2: “)
print(6/2)
print(“The solution to 6//2: “)
print(6//2)
print(“\n”)
“# Dividing an integer by a float”
print(“The solution to 8/2.75: “)
print(8/2.75)
print(“The solution to 8//2.75: “)
print(8//2.75)
print(“\n”)
“#Dividing a float by an integer”
print(“The solution to 9.5/2: “)
print(9.5/2)
print(“The solution to 9.5//2: “)
print(9.5//2)
print(“\n”)
“#Dividing a float by a float”
print(“The solution to 4.5/2.5: “)
print(4.5/2.5)
print(“The solution to 4.5//2.5: “)
print(4.5//2.5)
The output will be:
The solution to 6/2:
3.0
The solution to 6//2:
3
The solution to 8/2.75:
2.909090909090909
The solution to 8//2.75:
2.0
The solution to 9.5/2:
4.75
The solution to 9.5//2:
4.0
The solution to 4.5/2.5:
1.8
The solution to 4.5//2.5: 1.0
Python 3 Integer Division
Because the slash operator (“/”) in Python 3.x executes true division for all kinds, including integers, 3/2==1.5[1][2]. Despite the fact that all arguments are integers, the output is a floating-point number: 4 divided by 2 = 2.0.
In Python 3.x and later 2.x, the double slash (“//”) operator is used to execute floor division across both integer and floating-point arguments. This varies from integer division in C for negative outcomes since -3 / 2 == -2 in Python whereas -3 / 2 == -1 in C: Python rounds negative results to negative infinity, whereas C rounds to zero.
Also Read: Ray Out Of Memory Error Python: Causes, Solutions, and Strategies
Rounding errors can produce unexpected outcomes due to the limits of floating point arithmetic. Division of two integers or longs with the slash operator (“/”) in Python 2.x utilizes floor division (applies floor function after divide) and yields an integer or long. As a result, 5/2 = 2 and -3/2 = -2. This method of division using “/” is no longer recommended; instead, use “//” for floor division (found in Python 2.2 and later). Python will employ true division when dividing by or into a floating point number. Thus, in Python 2.x, “x=3; y=2; float(x)/y == 1.5” to assure proper division.
Conclusion
We have covered a few concepts in this article such as what is division operators, how to use the division operator in python, and later on, using python 3. We can conclude that there are two types of division in Python: “integer division and float division”. Integer division yields the division’s floor. The values following the decimal place are ignored.
FAQs
Python supports how many division operators?
Python contains two division operators: a single slash for classic division and a double slash for “floor” division (rounds to closest whole number)
What is the function of the /= operator?
The /= operator splits the data point or property (on the operator’s left side) by the number of the statement (on the right-hand side of the operator). The operation then allocates the floating-point value of such a function to a variable or attribute.