
- 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
What Does the // Operator Do?
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:
Syntax of // (double slash) Operator
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
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
As you can see in the above output, using the negative number did not impact the rounding off. The result rounded down
Let us check the results with single slash and compare.
Example of / Operator
The / operator is the division operator in Python. Let us see an example −
a = 37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using the / operator res = a / b print("Result of division = ", res)
Output
The 1st Number = 37 The end Number = 11 Result of division = 3.3636363636363638
Implementing / operator with a negative number
In this example, we will learn how a slash operator works with a negative number:
a = -37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using the / operator res = a / b print("Result of division = ", res)
Output
The 1st Number = -37 The end Number = 11 Result of division = -3.3636363636363638
- Related Articles
- What does the &= operator do in Python?
- What does the >> operator do in Python?
- What does the "===" operator do in Kotlin?
- What does "?:" do in Kotlin? (Elvis Operator)
- What does the bitwise left shift operator do in Java?
- What does the bitwise right shift operator do in Java?
- What does 'is' operator do in Python?
- What does 'in' operator do in Python?
- What does colon ':' operator do in Python?
- What does the operator || do in a var statement in JavaScript?
- What does 'is not' operator do in Python?
- What does 'not in' operator do in Python?
- What does the Star operator mean in Python?
- What does operator ~= mean in Lua?
- What does the Double Star operator mean in Python?
