{"id":3276,"date":"2020-02-12T07:46:10","date_gmt":"2020-02-12T07:46:10","guid":{"rendered":"https:\/\/www.askpython.com\/?p=3276"},"modified":"2022-08-06T13:11:57","modified_gmt":"2022-08-06T13:11:57","slug":"python-pow","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/built-in-methods\/python-pow","title":{"rendered":"Python pow() method"},"content":{"rendered":"\n<p>The Python <code>pow()<\/code> function is one of the most commonly used <strong><a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\" class=\"rank-math-link\">built-in<\/a><\/strong> function in Python programming. It is extensively used to calculate the value of <strong>a<\/strong> to the power <strong>n<\/strong> or more specifically <strong>a<sup>n<\/sup><\/strong>. It is a very useful function when dealing with some complex mathematical calculations or sometimes for other operations. So, let us dig a bit deeper into the <code>pow()<\/code> function in <strong>Python<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Python pow() function<\/h2>\n\n\n\n<p>The <code>pow()<\/code> function can be passed with a total of <strong>three<\/strong> arguments. The syntax for <code>pow()<\/code> is given below,<\/p>\n\n\n\n<p><code>pow( a , n , b )<\/code><\/p>\n\n\n\n<p>Where,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>a<\/strong> is the number whose power we are calculating or the <strong>base number<\/strong>,<\/li><li><strong>n<\/strong> is how much to the power a is to be raised or the <strong>exponential part<\/strong>,<\/li><li><strong>b<\/strong> is the number with which the <strong>modulus<\/strong> of an is going to be calculated.<\/li><\/ul>\n\n\n\n<p>Note: <strong>b<\/strong> is an optional argument.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples<\/h2>\n\n\n\n<p>Look at the code below, we here try to calculate the value of, let us say, <strong>2<sup>5<\/sup><\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx=pow(2,5)     #2^5\ny=pow(4,-2)    #1\/(4^2)\nprint(x,y)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"441\" height=\"256\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/pow-in-python.png\" alt=\"Pow In Python\" class=\"wp-image-3283\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/pow-in-python.png 441w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/02\/pow-in-python-300x174.png 300w\" sizes=\"auto, (max-width: 441px) 100vw, 441px\" \/><figcaption>pow() In Python<\/figcaption><\/figure><\/div>\n\n\n\n<p>Lets again try passing the optional <strong>modulus<\/strong> argument now,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx=pow(2,5,5)        #(2^5) % 5 = 32 % 5\nprint(x)\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=\"\">\n2\n<\/pre><\/div>\n\n\n<p>So accordingly we get the output as <strong>2<\/strong>. Since <code>pow(2,5,5)<\/code> actually returns the value for <strong>(2^5) % 5<\/strong> Or, <strong>32 % 5 = 2<\/strong>.<\/p>\n\n\n\n<p><strong>Note:<\/strong> While using the modulo argument we must make sure that the 2nd argument(exponent part) is a <strong>positive integer. <\/strong>Or else an <strong>error<\/strong> is thrown as shown below,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ny=pow(4,-2,6)\nprint(y)\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=\"\">\nTraceback (most recent call last):\n  File &quot;C:\/Users\/sneha\/Desktop\/test.py&quot;, line 2, in &lt;module&gt;\n    y=pow(4,-2,6)\nValueError: pow() 2nd argument cannot be negative when 3rd argument specified\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">math.pow() vs. Built-in pow() in Python<\/h2>\n\n\n\n<p>Apart from the fact that the <code>math.pow()<\/code> doesn&#8217;t come with the integrated modulus operation, both the built-in <code>pow()<\/code> and the pre-defined <code>math.pow()<\/code> have some big differences.<\/p>\n\n\n\n<p>The <code>pow()<\/code> function is comparatively <strong>faster<\/strong> for large set of values. On the other hand for using <strong>math.pow()<\/strong> the user has to first import <strong>mat<\/strong>h module.<\/p>\n\n\n\n<p>Even <strong>math.pow()<\/strong> method throws an error while calculating the results for some complex numbers. Which the <strong>pow()<\/strong> method does not.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li> <a href=\"https:\/\/stackoverflow.com\/questions\/10282674\/difference-between-the-built-in-pow-and-math-pow-for-floats-in-python\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/10282674\/difference-between-the-built-in-pow-and-math-pow-for-floats-in-python<\/a> <\/li><li> https:\/\/www.journaldev.com\/23002\/python-pow <\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The Python pow() function is one of the most commonly used built-in function in Python programming. It is extensively used to calculate the value of a to the power n or more specifically an. It is a very useful function when dealing with some complex mathematical calculations or sometimes for other operations. So, let us [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":3289,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-3276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-built-in-methods"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3276","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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=3276"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3276\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/3289"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=3276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=3276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=3276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}