{"id":21892,"date":"2023-09-29T10:54:08","date_gmt":"2023-09-29T05:24:08","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=21892"},"modified":"2023-09-29T10:54:09","modified_gmt":"2023-09-29T05:24:09","slug":"numpy-zeros-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-zeros-in-python\/","title":{"rendered":"numpy.zeros() in Python: Introduction, Syntax &amp; Examples"},"content":{"rendered":"\n<p>The <strong>numpy.zeros()<\/strong> function in Python&#8217;s <a href=\"https:\/\/codeforgeek.com\/python-top-libraries-for-machine-learning-and-data-science\/\">NumPy<\/a> library creates an array filled with zeros. This function is particularly useful when we need to initialize an array with zeros before populating it with actual data. It&#8217;s commonly used in various numerical and scientific computing tasks. In this article, we will understand <a href=\"https:\/\/codeforgeek.com\/python\/\">Python<\/a> <strong>numpy.zeros()<\/strong> function, its syntax, and learn how to use it with the help of five unique examples. Let\u2019s get started.<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/codeforgeek.com\/numpy-cbrt-python\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-cbrt-python\/\">numpy.cbrt() in Python &#8211; Calculating Cube Roots in NumPy<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of numpy.zeros() Function<\/h2>\n\n\n\n<p>Below is the syntax for using <strong>numpy.zeros()<\/strong> function in Python.<\/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=\"\">\nnumpy.zeros(shape, dtype=float, order=&#039;C&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>shape:<\/strong> A tuple specifying the dimensions (shape) of the output array. For example, for a 2D array, we would specify the shape as (rows, or columns).<\/li>\n\n\n\n<li><strong>dtype (optional): <\/strong>The data type of the elements in the array. It&#8217;s typically floating, but we can specify other data types as well.<\/li>\n\n\n\n<li><strong>order (optional): <\/strong>Specifies the memory layout of the array, either &#8216;C&#8217; (row-major) or &#8216;F&#8217; (column-major). The default is &#8216;C&#8217;.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return<\/strong>:&nbsp;An array of zeros with the given shape, data type, and order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of numpy.zeros() Function<\/h2>\n\n\n\n<p>Let us now look at some examples to demonstrate the use of <strong>numpy.zeros()<\/strong> function in Python. In each example, we have passed different parameter combinations to understand how this function works in different scenarios.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Creating a 1D array by specifying the shape<\/h3>\n\n\n\n<p>Here we will only provide rows in the<strong> numpy.zeros()<\/strong> function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nnp.zeros(5)\n<\/pre><\/div>\n\n\n<p>In the code above to use the <strong>zeros()<\/strong> function we first imported the <strong>Numpy<\/strong> library as <strong>np<\/strong> then we provided parameter <strong>shape<\/strong> as 5 in the <strong>np.zeros()<\/strong> function means we provided only row as the dimension of the output array.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"710\" height=\"54\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1782.png\" alt=\"Example 1: By specifying shape in numpy.zeros() function\" class=\"wp-image-22045\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1782.png 710w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1782-300x23.png 300w\" sizes=\"(max-width: 710px) 100vw, 710px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 1D array filled with five zeros of data type float as the default data type is float.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Creating a 2D array by specifying the shape<\/h3>\n\n\n\n<p>Here we will provide both rows and columns in the<strong> numpy.zeros()<\/strong> function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.zeros((2,3))\n<\/pre><\/div>\n\n\n<p>In the code above we provided parameter <strong>shape<\/strong> as (2,3) in the <strong>np.zeros()<\/strong> function, which means we provided a tuple of integers that helps to create a 2D array of 2 rows and 3 columns.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"77\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1783-1024x77.png\" alt=\"Example 2: By specifying shape in numpy.zeros() function\" class=\"wp-image-22046\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1783-1024x77.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1783-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1783-768x58.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1783.png 1196w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 2D array filled with zeros of data type float as the default data type is float.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Creating a 1D array by specifying shape &amp; dtype<\/h3>\n\n\n\n<p>Here we will provide <strong>shape<\/strong> and <strong>dtype<\/strong> in the<strong> numpy.zeros()<\/strong> function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.zeros(3,dtype=&quot;int&quot;)\n<\/pre><\/div>\n\n\n<p>In the code above within the<strong> np.zeros()<\/strong> function we have provided parameter <strong>shape<\/strong> as 3 and we have also provided <strong>dtype<\/strong> as <strong>int<\/strong> which means we have changed the data type of the element from float to<strong> int<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"823\" height=\"62\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1787.png\" alt=\"Example 3: By specifying shape with the dtype in numpy.zeros() function\" class=\"wp-image-22050\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1787.png 823w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1787-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1787-768x58.png 768w\" sizes=\"(max-width: 823px) 100vw, 823px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 1D array filled with zeros of data type Integer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: Creating a 2D array by specifying shape &amp; dtype<\/h3>\n\n\n\n<p>Here we will do the same thing that we did in the last example but the only difference is we will specify tuple as a parameter for creating a 2D array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.zeros((4,5), dtype=&quot;int&quot;)\n<\/pre><\/div>\n\n\n<p>In the code above we provided parameter <strong>shape<\/strong> as (4,5) in the <strong>np.zeros()<\/strong> function to create a 2D array of 4 rows and 5 columns and we also have provided <strong>dtype<\/strong> as <strong>int<\/strong>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"88\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1785-1024x88.png\" alt=\"Example 4: By specifying shape with the dtype in numpy.zeros() function\" class=\"wp-image-22048\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1785-1024x88.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1785-300x26.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1785-768x66.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1785.png 1458w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 2D array filled with zeros of data type Integer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5: Creating a 2D array by specifying shape &amp; order<\/h3>\n\n\n\n<p>Here in the <strong>numpy.zeros()<\/strong> function we will provide <strong>shape<\/strong> which is the dimension of the output array and <strong>order<\/strong> which specifies the memory layout of the array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.zeros((3,4),order=&quot;F&quot;)\n<\/pre><\/div>\n\n\n<p>In the code above we provided <strong>shape<\/strong> as a tuple which is (3,4) which creates a 2D array filled with zeros of 3 rows and 4 columns and we also provided <strong>order<\/strong> as <strong>F<\/strong> to store data in column-major order in the memory which is by default <strong>C<\/strong> means row-major.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"78\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1786-1024x78.png\" alt=\"Example: By specifying shape with the order in numpy.zeros() function\" class=\"wp-image-22049\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1786-1024x78.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1786-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1786-768x59.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1786.png 1476w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this tutorial, we have discussed<strong> numpy.zeros()<\/strong> function provided by Python\u2019s NumPy library and also explored five examples to create an array filled with zeros using <strong>numpy.zeros()<\/strong> function with examples. After reading this tutorial, we hope you can easily create an array filled with zeros in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/69847064\/python-for-structure-based-array\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/69847064\/python-for-structure-based-array<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The numpy.zeros() function in Python&#8217;s NumPy library creates an array filled with zeros. This function is particularly useful when we need to initialize an array with zeros before populating it with actual data. It&#8217;s commonly used in various numerical and scientific computing tasks. In this article, we will understand Python numpy.zeros() function, its syntax, and [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":22038,"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-21892","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\/2023\/09\/numpy.zeros-in-Python-Creating-an-array-filled-with-zeros.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.zeros-in-Python-Creating-an-array-filled-with-zeros-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.zeros-in-Python-Creating-an-array-filled-with-zeros-300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.zeros-in-Python-Creating-an-array-filled-with-zeros-768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.zeros-in-Python-Creating-an-array-filled-with-zeros-1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.zeros-in-Python-Creating-an-array-filled-with-zeros.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.zeros-in-Python-Creating-an-array-filled-with-zeros.png",1200,800,false]},"uagb_author_info":{"display_name":"Priyanshu Singh","author_link":"https:\/\/codeforgeek.com\/author\/priyanshu\/"},"uagb_comment_info":0,"uagb_excerpt":"The numpy.zeros() function in Python&#8217;s NumPy library creates an array filled with zeros. This function is particularly useful when we need to initialize an array with zeros before populating it with actual data. It&#8217;s commonly used in various numerical and scientific computing tasks. In this article, we will understand Python numpy.zeros() function, its syntax, and&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21892","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\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=21892"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21892\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/22038"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=21892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=21892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=21892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}