Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 -
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">a <span class="token operator">=</span> <span class="token number">37</span> b <span class="token operator">=</span> <span class="token number">11</span> <span class="token comment"># 1st Number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The 1st Number = "</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span> <span class="token comment"># 2nd Number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The end Number = "</span><span class="token punctuation">,</span>b<span class="token punctuation">)</span> <span class="token comment"># Dividing using floor division</span> res <span class="token operator">=</span> a <span class="token operator">//</span> b <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Result of floor division = "</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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 ?
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># A negative number with a positive number</span> a <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">37</span> b <span class="token operator">=</span> <span class="token number">11</span> <span class="token comment"># 1st Number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The 1st Number = "</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span> <span class="token comment"># 2nd Number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The end Number = "</span><span class="token punctuation">,</span>b<span class="token punctuation">)</span> <span class="token comment"># Dividing using floor division</span> res <span class="token operator">=</span> a <span class="token operator">//</span> b <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Result of floor division = "</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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 ?
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">a <span class="token operator">=</span> <span class="token number">37</span> b <span class="token operator">=</span> <span class="token number">11</span> <span class="token comment"># 1st Number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The 1st Number = "</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span> <span class="token comment"># 2nd Number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The end Number = "</span><span class="token punctuation">,</span>b<span class="token punctuation">)</span> <span class="token comment"># Dividing using the / operator</span> res <span class="token operator">=</span> a <span class="token operator">/</span> b <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Result of division = "</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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:
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;">a <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">37</span> b <span class="token operator">=</span> <span class="token number">11</span> <span class="token comment"># 1st Number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The 1st Number = "</span><span class="token punctuation">,</span>a<span class="token punctuation">)</span> <span class="token comment"># 2nd Number</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"The end Number = "</span><span class="token punctuation">,</span>b<span class="token punctuation">)</span> <span class="token comment"># Dividing using the / operator</span> res <span class="token operator">=</span> a <span class="token operator">/</span> b <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Result of division = "</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
The 1st Number = -37 The end Number = 11 Result of division = -3.3636363636363638
