{"id":30138,"date":"2024-05-27T13:46:47","date_gmt":"2024-05-27T08:16:47","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=30138"},"modified":"2024-05-27T13:51:25","modified_gmt":"2024-05-27T08:21:25","slug":"numpy-exp2-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-exp2-in-python\/","title":{"rendered":"numpy.exp2: Computing Powers of Two in Python"},"content":{"rendered":"\n<p>Welcome, coders! In our NumPy series, we have brought a useful function for you: <strong>numpy.exp2<\/strong>, which helps us compute the powers of two in Python. We will discuss it in detail, so keep following along.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Compute Powers of Two?<\/h2>\n\n\n\n<p>&#8220;<strong>Computing Powers of Two<\/strong>&#8221; is like figuring out how many times you can double a number. You start with 1, then double it to get 2, double that to get 4, and so on.<\/p>\n\n\n\n<p>This is important in computers because they work with <strong>0s<\/strong> and <strong>1s<\/strong>, which we call <strong>binary<\/strong>. Let&#8217;s say you have a computer with <strong>4 slots<\/strong> for <strong>0s<\/strong> and <strong>1s<\/strong>, called <strong>bits<\/strong>. Each bit can either be 0 or 1, just like flipping a coin.<\/p>\n\n\n\n<p>Now, if you have 4 of these bits, you can make different combinations of 0s and 1s. How many? Well, each bit gives you two choices (0 or 1), and you multiply those choices together for each bit. So, it&#8217;s like 2 multiplied by itself 4 times:<strong> 2^4=16<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"720\" height=\"578\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-37.png\" alt=\"Powers of Two\" class=\"wp-image-30140\" style=\"width:410px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-37.png 720w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-37-300x241.png 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/figure>\n\n\n\n<p>Knowing the powers of two helps us understand how much information computers can handle and how they store data efficiently. It is the foundation of computer memory and storage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">numpy.exp2() in Python<\/h2>\n\n\n\n<p>The NumPy library offers us a <strong>numpy.exp2()<\/strong> function, which allows us to easily compute powers of two. Let&#8217;s discuss its syntax and parameters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax:<\/h3>\n\n\n\n<p>The basic syntax of the given function is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.exp2(x, \/, out=None, *, where=True, casting=&#039;same_kind&#039;, order=&#039;K&#039;, dtype=None, subok=True&#x5B;, signature, extobj])\n<\/pre><\/div>\n\n\n<p>Here is what these parameters mean:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>x<\/strong>: It&#8217;s the input values. It can be both an array or a single value.<\/li>\n\n\n\n<li><strong>out<\/strong>: This is where the result is stored. If you provide it, it must match the inputs. If not provided, a new array is created.<\/li>\n\n\n\n<li><strong>where<\/strong>: It&#8217;s a condition applied to the input. Where the condition is true, the result will be set. Otherwise, the original value is kept.<\/li>\n\n\n\n<li><strong>dtype<\/strong>: This specifies the type of the output array. If you don&#8217;t give it, the type is guessed from the input.<\/li>\n\n\n\n<li><strong>casting<\/strong>: It controls what kind of data casting is allowed. The default is to keep it the same.<\/li>\n\n\n\n<li><strong>order<\/strong>: This determines how the array is laid out in memory. &#8216;C&#8217; means row-major (last index varies fastest), &#8216;F&#8217; means column-major (first index varies fastest), and &#8216;A&#8217; (default) means any order.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Working:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>numpy.exp2()<\/strong> function returns 2 raised to the power of the elements of the input array.<\/li>\n\n\n\n<li>If the input is scalar, the result will be scalar too.<\/li>\n\n\n\n<li>The function supports broadcasting, meaning it can operate on arrays of different shapes and sizes.<\/li>\n\n\n\n<li>You can control the data type of the output array using the <strong>dtype<\/strong> parameter.<\/li>\n\n\n\n<li>The <strong>where<\/strong> parameter allows you to conditionally operate.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Using numpy.exp2() in Python<\/h2>\n\n\n\n<p>It will never be so clear until we see some real coding examples. Let&#8217;s see how we can use this function in Python with different inputs like scalar, 1D array, 2D array, where parameter, etc.<\/p>\n\n\n\n<p><strong>Example 1<\/strong>: Here we will compute the power of two for a scalar value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nresult = np.exp2(4)\nprint(&quot;2 raised to the power of 4:&quot;, result)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"498\" height=\"117\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-38.png\" alt=\"Example 1 Output\" class=\"wp-image-30142\" style=\"width:319px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-38.png 498w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-38-300x70.png 300w\" sizes=\"(max-width: 498px) 100vw, 498px\" \/><\/figure>\n\n\n\n<p><strong>Example 2:<\/strong> When we work with arrays, the function returns powers of two for each element in an array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ninput_array = np.array(&#x5B;0, 3, 5, 7])\nresult = np.exp2(input_array)\nprint(&quot;Powers of two:&quot;, result)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"496\" height=\"132\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-39.png\" alt=\"Example 2 Output\" class=\"wp-image-30143\" style=\"width:333px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-39.png 496w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-39-300x80.png 300w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/figure>\n\n\n\n<p><strong>Example 3:<\/strong> We can apply conditions to computations using the &#8216;<strong>where<\/strong>&#8216; parameter.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\ninput_array = np.array(&#x5B;0, 1, 2, 3])\ncondition = input_array &lt; 2\nresult_array = np.exp2(input_array, where=condition)\nprint(&quot;Powers of two with condition:&quot;, result_array)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"505\" height=\"131\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-40.png\" alt=\"Example 3 Output\" class=\"wp-image-30144\" style=\"width:339px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-40.png 505w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-40-300x78.png 300w\" sizes=\"(max-width: 505px) 100vw, 505px\" \/><\/figure>\n\n\n\n<p><strong>Example 4:<\/strong> Let&#8217;s see how we can use <strong>broadcasting<\/strong> with this function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\nscalar_value = 3\narray_1d = np.array(&#x5B;0, 2, 4, 6])\narray_2d = np.array(&#x5B;&#x5B;1, 3], &#x5B;5, 7]])\nresult_1d = np.exp2(scalar_value + array_1d)\nprint(&quot;Broadcasting scalar with 1D array:&quot;)\nprint(result_1d)\nresult_2d = np.exp2(scalar_value + array_2d)\nprint(&quot;\\nBroadcasting scalar with 2D array:&quot;)\nprint(result_2d)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"493\" height=\"281\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-41.png\" alt=\"Example 4 Output\" class=\"wp-image-30145\" style=\"width:323px;height:auto\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-41.png 493w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/image-41-300x171.png 300w\" sizes=\"(max-width: 493px) 100vw, 493px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>And that&#8217;s it for this one! We have learned about new things such as the importance of powers of two in computer memory organization. We have also seen the method in NumPy to calculate powers of two and discussed its syntax, parameters, and use cases.<\/p>\n\n\n\n<p><em><strong>If you enjoyed this one, don&#8217;t forget to check out:<\/strong><\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/codeforgeek.com\/round-numbers-in-python\/\"><em>How to Round Numbers in Python (5 Ways)<\/em><\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/codeforgeek.com\/convert-degree-to-radian-in-python\/\"><em>Convert Degree to Radian in Python (3 Ways)<\/em><\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.exp2.html\" target=\"_blank\" rel=\"noopener\">https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.exp2.html<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome, coders! In our NumPy series, we have brought a useful function for you: numpy.exp2, which helps us compute the powers of two in Python. We will discuss it in detail, so keep following along. Why Compute Powers of Two? &#8220;Computing Powers of Two&#8221; is like figuring out how many times you can double a [&hellip;]<\/p>\n","protected":false},"author":104,"featured_media":30148,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-30138","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Computing-Powers-of-Two-in-Python.png",1200,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Computing-Powers-of-Two-in-Python-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Computing-Powers-of-Two-in-Python-300x150.png",300,150,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Computing-Powers-of-Two-in-Python-768x384.png",768,384,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Computing-Powers-of-Two-in-Python-1024x512.png",1024,512,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Computing-Powers-of-Two-in-Python.png",1200,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2024\/05\/Computing-Powers-of-Two-in-Python.png",1200,600,false]},"uagb_author_info":{"display_name":"Snigdha Keshariya","author_link":"https:\/\/codeforgeek.com\/author\/snigdha\/"},"uagb_comment_info":0,"uagb_excerpt":"Welcome, coders! In our NumPy series, we have brought a useful function for you: numpy.exp2, which helps us compute the powers of two in Python. We will discuss it in detail, so keep following along. Why Compute Powers of Two? &#8220;Computing Powers of Two&#8221; is like figuring out how many times you can double a&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/30138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=30138"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/30138\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/30148"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=30138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=30138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=30138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}