
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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)
- Related Articles
- Why doesn’t list.sort() return the sorted list in Python?
- Why would you use the return statement in Python?
- How does == operator works in Python 3?
- Why does Python Not Support Multithreading?
- Why does php's in_array return true if passed a 0?
- Why does Python code run faster in a function?
- 10 Reasons why you should Learn Python
- What does "return@" mean in Kotlin?
- MySQL update datetime column values and add 10 years 3 months 22 days and 10 hours, 30 minutes to existing data?
- Why does my MongoDB group query return always 0 in float conversion? How to fix it?
- Does constructor return any value in Java?
- Solve:frac{9}{11} \ \times \ \frac{22}{3}$
- Decimal Equivalents of 1/2, 1/3, . . . ,1/10 in Python
- Why does C code run faster than Python's?
- The return Statement in Python
