{"id":5400,"date":"2020-04-29T14:37:49","date_gmt":"2020-04-29T14:37:49","guid":{"rendered":"https:\/\/www.askpython.com\/?p=5400"},"modified":"2022-08-06T13:13:49","modified_gmt":"2022-08-06T13:13:49","slug":"python-division-operation","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/python-division-operation","title":{"rendered":"How to Perform the Python Division Operation?"},"content":{"rendered":"\n<p>Hey, folks! In this article, we will be focusing on an arithmetic operation &#8211; Python <strong>Division operation<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started with Python division operation<\/h2>\n\n\n\n<p>Python has got various in-built operators and functions to perform arithmetic manipulations.<\/p>\n\n\n\n<p>The <code>'\/' operator<\/code> is used to perform division operation on data values of both the data types i.e. &#8216;<strong>float<\/strong>&#8216; and &#8216;<strong>int<\/strong>&#8216;.<\/p>\n\n\n\n<p>The beauty of Python &#8216;\/&#8217; operator is that this operator can handle decimal as well as negative values, respectively.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumber1 \/ number2\n<\/pre><\/div>\n\n\n<p>The operator operates on numeric values and returns a floating point value as a result. The result of the division operation is the <strong>Quotient <\/strong>of the operation performed, being represented as a <strong>floating point value<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = input(&quot;Enter the value for a:&quot;)\nb = input(&quot;Enter the value of b:&quot;)\nres = int(a)\/int(b)\nprint(res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nEnter the value for a:10\nEnter the value of b:2\n5.0\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = -10\nb = 20\nres = a\/b\nprint(res)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n-0.5\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python division operation on Tuple<\/h2>\n\n\n\n<p>Python <code>floordiv() method<\/code> along with <code>map() function<\/code> can be used to perform division operation on various data values stored in a <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" class=\"rank-math-link\">Tuple <\/a>data structure.<\/p>\n\n\n\n<p>Python <code>floordiv() method<\/code> is used to perform division operation on all the elements present in the data structure i.e. it performs <strong>element wise division<\/strong> operation. Further, <code>Python map() function<\/code> applies any passed\/given function or operation on a set of iterables such as tuples, list, etc.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntuple(map(floordiv, tuple1, tuple2))\n<\/pre><\/div>\n\n\n<p>The <code>floordiv() method<\/code> performs integer division i.e. divides the elements and returns only the integer portion of the quotient and skips the decimal portion.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom operator import floordiv \n\n \ninp_tup1 = (10,16,9,-4) \ninp_tup2 = (2,-8,4,4) \n\ntup_div = tuple(map(floordiv, inp_tup1, inp_tup2)) \n\n\nprint(&quot;Resultant tuple after performing division operation : &quot; + str(tup_div)) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nResultant tuple after performing division operation : (5, -2, 2, -1)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python division operation on Dict<\/h2>\n\n\n\n<p>Python division operation can be performed on the elements present in the <a href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\" class=\"rank-math-link\">dictionary <\/a>using Counter() function along with &#8216;\/\/&#8217; operator.<\/p>\n\n\n\n<p>The <code>Counter() function<\/code> stores the <strong>dictionary key-value data as dict keys<\/strong> and stores the <strong>count of the dict elements as the associated values<\/strong>.<\/p>\n\n\n\n<p>The &#8216;\/\/&#8217; operator performs integer level division on the data elements.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nCounter({key : dict1&#x5B;key] \/\/ dict2&#x5B;key] for key in dict1})\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom collections import Counter\n\ninp_dict1 = {&#039;Python&#039;: 100, &#039;Java&#039;: 40, &#039;C&#039;: 36}\ninp_dict2 = {&#039;Python&#039;: 20, &#039;Java&#039;: -10, &#039;C&#039;: 8}\n\ninp_dict1 = Counter(inp_dict1) \ninp_dict2 = Counter(inp_dict2) \ndict_div = Counter({key : inp_dict1&#x5B;key] \/\/ inp_dict2&#x5B;key] for key in inp_dict1}) \n\nprint(&quot;Resultant dict after performing division operation : &quot; + str(dict(dict_div))) \n<\/pre><\/div>\n\n\n<p>In the above example, we have stored the key-value pairs of the input dict in such a way using Counter() function that the input dict now contains the key as the dict elements and the value as the count of elements present in the dict.<\/p>\n\n\n\n<p>Further, we have passed the keys to the &#8216;\/\/&#8217; operator to perform the division operation.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nResultant dict after performing division operation : {&#039;Python&#039;: 5, &#039;Java&#039;: -4, &#039;C&#039;: 4}\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Difference between Python &#8216;\/&#8217; and Python &#8216;\/\/&#8217; division operators<\/h2>\n\n\n\n<p>The basic and probably the only difference between the &#8216;\/&#8217; and &#8216;\/\/&#8217; division operators is that the<code> '\/' operator<\/code> returns float values as the result of division i.e. it returns the entire quotient( the integer as well as the decimal part). <\/p>\n\n\n\n<p>On the other hand,<code> '\/\/' division operator<\/code> returns integer value as a result of division i.e. returns only the integer portion of the quotient value.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nprint(10\/3)\nprint(10\/\/3)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n3.3333333333333335\n3\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Thus, in this article, we have understood the ways to perform division operation in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python division operation<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey, folks! In this article, we will be focusing on an arithmetic operation &#8211; Python Division operation. Getting started with Python division operation Python has got various in-built operators and functions to perform arithmetic manipulations. The &#8216;\/&#8217; operator is used to perform division operation on data values of both the data types i.e. &#8216;float&#8216; and [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":5485,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-5400","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=5400"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5400\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/5485"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=5400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=5400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=5400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}