Why does -22 // 10 return -3 in Python?


In Python, -22//10 returns -3 because of the concept of Floor Division i.e. Double Slash Operator. The // is the Double Slash i.e. Arithmetic Operator. Let us first understand about it.

Floor Division in Python

The division of operands where the result is the quotient in which the digits after the decimal point are removed. But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity).

In Python, the // is the double slash operator i.e. Floor Divison. The // operator is used to perform division that rounds the result down to the nearest integer. The usage of the // operator is quite easy. We will also compare the results with single slash division. Let us first see the syntax −

The a and b are 1st and 2nd number:

a // b

Example of // (double slash) Operator

Let us now see an example to implement the double slash operator in Python −

a = 37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)

Output

('The 1st Number = ', 37)
('The end Number = ', 11)
('Result of floor division = ', 3)

Implementing // (double slash) operator with a negative number

Example

We will try to use the double slash operator with a negative number as input. Let’s see the example

# A negative number with a positive number a = -37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)

Output

('The 1st Number = ', -37)
('The end Number = ', 11)
('Result of floor division = ', -4)

Example

As you can see in the above output, using the negative number did not impact the rounding off. The result rounded down. Now, we can check -22 // 10 with the double slash operator −

# A negative number with a positive number a = -22 b = 10 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)

Output

('The 1st Number = ', -22)
('The end Number = ', 10)
('Result of floor division = ', -3)

Updated on: 19-Sep-2022

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements