{"id":37961,"date":"2022-12-18T10:57:33","date_gmt":"2022-12-18T10:57:33","guid":{"rendered":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/?p=37961"},"modified":"2026-09-29T17:36:01","modified_gmt":"2026-09-29T17:36:01","slug":"numpy-add","status":"publish","type":"post","link":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/python-modules\/numpy\/numpy-add","title":{"rendered":"NumPy add &#8211; Explained in a Simple Way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this article, we will be exploring the NumPy add function. Have you ever thought about how we can add two numbers or two arrays of numbers using programming? Well, that&#8217;s where the Python NumPy library comes into play.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we will go through the syntax of the function and practice different types of examples as well. So, let&#8217;s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy add?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>numpy.add()<\/code><\/strong> is one of the mathematical functions of the NumPy library. It simply adds the values passed as input to the function. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, it is that simple by definition \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s look at the syntax and understand more about the function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Syntax of NumPy add<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.add(x1 , x2)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here, the inputs <strong><code>x1<\/code><\/strong> and <strong><code>x2<\/code><\/strong> are NumPy array of numbers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> In the syntax, the inputs can also be scalar values(simple numbers) and Python lists.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working with NumPy add<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We are ready to do programming and understand more about this function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy add with Scalar Values<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing the NumPy module\nimport numpy as np\n\na = 5\nb = 4\n\nprint(&quot;The sum of 5 and 4 is:&quot;,np.add(a , b))\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nThe sum of 5 and 4 is: 9\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We first import the NumPy library using the <strong><code>import<\/code><\/strong> statement in the above snippet. In the following lines, we assign values to the variables <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To compute the sum of the values of <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong> the function <strong><code>np.add(a , b)<\/code><\/strong> is called. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now let us use the function with the NumPy array and a scalar value and observe the output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy add with a NumPy array and a Scalar Value<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Creating the  2-D array\na = np.array(&#x5B;&#x5B;2 , 4 , 7] , &#x5B;5 , 10 , 15]])\n\nb = 5\n\nc = np.add(a , b)\n\nprint(&quot;Input Array:\\n&quot;,a)\nprint(&quot;After adding 5 to each value of the Input Array:\\n&quot;,c)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nInput Array:\n &#x5B;&#x5B; 2  4  7]\n &#x5B; 5 10 15]]\nAfter adding 5 to each value of the Input Array:\n &#x5B;&#x5B; 7  9 12]\n &#x5B;10 15 20]]\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In the above snippet, a two-dimensional array is created using the <strong><code>np.array()<\/code><\/strong>, which is stored in the variable <strong><code>a<\/code><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we created a variable <strong><code>b<\/code><\/strong> that holds an integer value of 5. Moving further, we used <strong><code>np.add()<\/code><\/strong> in which we passed <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong> as arguments to the function and the output of the function is stored in the variable <strong><code>c<\/code><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the output, we got a 2-D array. If you carefully observe the output array, you will see that the number 5 is added to each and every element in the input array.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>Note:<\/code><\/strong> The shape of the output array is the same as the shape of the input array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy add with two Same-Sized NumPy arrays <\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we will be adding two same-sized numpy arrays.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Creating 2x4 array\na = np.array(&#x5B;&#x5B;2 , 5 , 7 , 3] , &#x5B;1 , 4 , 5 , 12]])\n\nb = np.array(&#x5B;&#x5B;9 , 5 , 11 , 23] , &#x5B;21 , 34 , 1 , 9]])\n\n# Using the add function\nc = np.add(a , b)\n\n# Printing the values\nprint(&quot;Array 1:\\n&quot;,a)\nprint(&quot;Array 2:\\n&quot;,b)\n\nprint(&quot;Output array:\\n&quot;,c)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nArray 1:\n &#x5B;&#x5B; 2  5  7  3]\n &#x5B; 1  4  5 12]]\nArray 2:\n &#x5B;&#x5B; 9  5 11 23]\n &#x5B;21 34  1  9]]\nOutput array:\n &#x5B;&#x5B;11 10 18 26]\n &#x5B;22 38  6 21]]\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In the above example, we created two numpy arrays of dimensions 2&#215;4 i.e. an array having 2 rows and 4 columns. These arrays are stored in the variables <code>a<\/code> and <strong><code>b<\/code><\/strong>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the following lines, we used the function <strong><code>np.add(a , b)<\/code><\/strong> where a and b are passed as an argument to the function. The function <strong><code>np.add(a,b)<\/code><\/strong> adds the values at the same places in arrays <strong><code>a<\/code><\/strong> and <strong><code>b<\/code><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>shape<\/strong> of the output array is the same as the input arrays. Now, let us see the most interesting example of the article.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">NumPy add with two NumPy arrays of different shape<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we will see how can we add a 1-D NumPy array to a 2-D NumPy array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\n# Creating a 1-D array\na = np.array((10 , 56 , 21))\n\n# Creating a 2-D array\nb = np.array(&#x5B;&#x5B;1 , 4 , 6] , &#x5B;23 , 12 , 16]])\n\nc = np.add(a , b)\n\nprint(&quot;Array 1:\\n&quot;,a)\nprint(&quot;Array 2:\\n&quot;,b)\n\nprint(&quot;Output Array:\\n&quot;,c)\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nArray 1:\n &#x5B;10 56 21]\nArray 2:\n &#x5B;&#x5B; 1  4  6]\n &#x5B;23 12 16]]\nOutput Array:\n &#x5B;&#x5B;11 60 27]\n &#x5B;33 68 37]]\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Isn&#8217;t that interesting to add two NumPy arrays of different shapes? Well, we did that with ease using the NumPy library.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s try to understand the above snippet. We created a <strong>1-D<\/strong> NumPy array that is stored in the variable <strong><code>a<\/code><\/strong> and another 2-D NumPy array is created and stored in the variable <strong><code>b<\/code><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you carefully observe the arrays created in the above example you will notice that they have the same number of columns but a different number of rows. That&#8217;s where the term <strong>broadcasting<\/strong> comes into play.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the function <strong><code>np.add()<\/code><\/strong> is broadcasting the 1-D array across the rows of the 2-D array. In the above example, <strong><code>np.add()<\/code><\/strong> adds the values of the array <strong><code>a<\/code><\/strong> to row1 of the array <strong><code>b<\/code><\/strong>, element-wise. Similarly, the same computation happens for the next row of the array <strong>b<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note: <\/strong>The above is possible when the number of elements in the 1-D array is the same as the number of columns in the 2-D array <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, we are done with the examples \ud83d\ude42<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we learned how to use the NumPy add function for a variety of input types. It is a simple function to use as it just adds the inputs given to the function. Try using the NumPy add function with different functions of the NumPy array. Keep learning and happy coding. Find out more articles <a href=\"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be exploring the NumPy add function. Have you ever thought about how we can add two numbers or two arrays of numbers using programming? Well, that&#8217;s where the Python NumPy library comes into play. In this tutorial, we will go through the syntax of the function and practice different types [&hellip;]<\/p>\n","protected":false},"author":48,"featured_media":69831,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-37961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"_links":{"self":[{"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=37961"}],"version-history":[{"count":0,"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/posts\/37961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/media\/69831"}],"wp:attachment":[{"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=37961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=37961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fd.xuwubk.eu.org:443\/https\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=37961"}],"curies":[{"name":"wp","href":"https:\/\/fd.xuwubk.eu.org:443\/https\/api.w.org\/{rel}","templated":true}]}}