{"id":21898,"date":"2023-09-27T15:18:18","date_gmt":"2023-09-27T09:48:18","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=21898"},"modified":"2023-09-27T15:18:19","modified_gmt":"2023-09-27T09:48:19","slug":"numpy-arange-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-arange-in-python\/","title":{"rendered":"numpy.arange() in Python: Introduction, Syntax &#038; Examples"},"content":{"rendered":"\n<p>The <strong>numpy.arange()<\/strong> function in <a href=\"https:\/\/codeforgeek.com\/python\/\">Python&#8217;s<\/a> NumPy library is used to generate arrays of evenly spaced values within a specified range. It&#8217;s similar to Python&#8217;s built-in <strong>range()<\/strong> function but produces a <a href=\"https:\/\/codeforgeek.com\/python-top-libraries-for-machine-learning-and-data-science\/\">NumPy<\/a> array as output.<\/p>\n\n\n\n<p>In this article, we will understand Python <strong>numpy.arange()<\/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<h2 class=\"wp-block-heading\">Syntax of numpy.arange() Function<\/h2>\n\n\n\n<p>Below is the syntax for using the <strong>numpy.arange()<\/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.arange(start, stop, step, dtype=None)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>start (optional):<\/strong> The start of the range (inclusive). If not provided, it defaults to 0.<\/li>\n\n\n\n<li><strong>stop:<\/strong> The end of the range (exclusive). This value is required.<\/li>\n\n\n\n<li><strong>step (optional):<\/strong> The step size between values in the range. If not provided, it defaults to 1.<\/li>\n\n\n\n<li><strong>dtype (optional): <\/strong>The data type of the output array. It&#8217;s typically inferred based on the input arguments, but you can specify a different data type if needed.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return:<\/strong> <\/p>\n\n\n\n<p>This returns an array of evenly spaced values.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of numpy.arange() Function<\/h2>\n\n\n\n<p>Let us now look at some examples to demonstrate the use of <strong>numpy.arange()<\/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: By specifying only stop<\/h3>\n\n\n\n<p>Here we will only provide&nbsp;a <strong>stop&nbsp;<\/strong>in the<strong>&nbsp;numpy.arange()<\/strong>&nbsp;function which is the end of the range and is exclusive.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nnp.arange(10)\n<\/pre><\/div>\n\n\n<p>In the above code, we first imported the&nbsp;<strong>NumPy<\/strong>&nbsp;library as&nbsp;<strong>np<\/strong> then provided parameter <strong>stop<\/strong> as 10 in the <strong>np.arange()<\/strong> function means we only provided the end of the range or the endpoint which is exclusive.<\/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=\"779\" height=\"59\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1831.png\" alt=\"Example: By specifying the stop in numpy.arange() function\" class=\"wp-image-22146\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1831.png 779w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1831-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1831-768x58.png 768w\" sizes=\"(max-width: 779px) 100vw, 779px\" \/><\/figure>\n\n\n\n<p>In the above output, we got an array that starts from 0 and ends with 9.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: By specifying start &amp; stop<\/h3>\n\n\n\n<p>Here we will provide&nbsp;<strong>start<\/strong>&nbsp;and&nbsp;<strong>stop<\/strong>&nbsp;in the<strong>&nbsp;numpy.arange()<\/strong>&nbsp;function which is the start and end of the range respectively.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.arange(1,10)\n<\/pre><\/div>\n\n\n<p>In the above code, we provided parameters&nbsp;<strong>start<\/strong>&nbsp;as 1 which is inclusive and<strong> stop<\/strong> as 10 which is exclusive in the&nbsp;<strong>np.arange()<\/strong>&nbsp;function.<\/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=\"808\" height=\"61\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1832.png\" alt=\"Example: By specifying the start and stop in numpy.arange() function\" class=\"wp-image-22147\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1832.png 808w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1832-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1832-768x58.png 768w\" sizes=\"(max-width: 808px) 100vw, 808px\" \/><\/figure>\n\n\n\n<p>In the above output, we got an array that starts from 1 and ends with 9 and the step size value as 1 which is the default value for generating the array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: By specifying start, stop, &amp; step<\/h3>\n\n\n\n<p>Here in the&nbsp;<strong>numpy.arange()<\/strong>&nbsp;function we will provide&nbsp;a <strong>start&nbsp;<\/strong>and<strong> stop<\/strong> with the <strong>step<\/strong> which is the step size between values in the range and by default its value is 1.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.arange(1,10,2)\n<\/pre><\/div>\n\n\n<p>In the above code, we provided parameters&nbsp;<strong>start&nbsp;<\/strong>as 1, <strong>stop<\/strong> as 10, and <strong>step<\/strong> as 2 means the step size between the values is 2.<\/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=\"788\" height=\"60\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1833.png\" alt=\"Example: By specifying the start, stop, and step in numpy.arange() function\" class=\"wp-image-22148\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1833.png 788w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1833-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1833-768x58.png 768w\" sizes=\"(max-width: 788px) 100vw, 788px\" \/><\/figure>\n\n\n\n<p>In the above output, we got an array with the values from 1 to 10 and the difference between the values is 2 as the step size was 2.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: By specifying the stop &amp; dtype<\/h3>\n\n\n\n<p>Here in the&nbsp;<strong>numpy.arange()<\/strong>&nbsp;function we will provide&nbsp;<strong>stop&nbsp;<\/strong>and<strong> dtype<\/strong> which means the data type of the output array.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.arange(20,dtype=&quot;complex&quot;)\n<\/pre><\/div>\n\n\n<p>In the above code, we have provided parameter <strong>stop<\/strong> as 20 and <strong>dtype<\/strong> as complex means we have set the data type of values in the output array as complex.<\/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=\"80\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1834-1024x80.png\" alt=\"Example: By specifying the stop and dtype in numpy.arange() function\" class=\"wp-image-22149\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1834-1024x80.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1834-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1834-768x60.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1834.png 1406w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the above output, we got an array with values from 0 to 20 and the datatype of those values is complex.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5: By specifying the start, stop, step, &amp; dtype<\/h3>\n\n\n\n<p>Here we will provide all the parameters <strong>start<\/strong>, <strong>stop<\/strong>, <strong>step, <\/strong>and <strong>dtype<\/strong> together in the <strong>numpy.arange()<\/strong> function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.arange(1,10,2,dtype=&quot;float&quot;)\n<\/pre><\/div>\n\n\n<p>In the above code, we have provided the parameters <strong>start <\/strong>as 2, <strong>stop<\/strong> as 10, <strong>step <\/strong>as 2, and <strong>dtype<\/strong> as the float.<\/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=\"836\" height=\"63\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1835.png\" alt=\"Example: By specifying the start, stop, step, and dtype in numpy.arange() function\" class=\"wp-image-22150\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1835.png 836w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1835-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1835-768x58.png 768w\" sizes=\"(max-width: 836px) 100vw, 836px\" \/><\/figure>\n\n\n\n<p>In the above output, we got an array with values from 1 to 9 and the difference between the values is 2. Also, the datatype of those values is float.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>The <strong>numpy.arange()<\/strong> is a fundamental function for generating sequences of numbers, which makes it invaluable in a broad range of scientific and numerical tasks. In this tutorial, we have discussed numpy.arange()&nbsp;function provided by Python\u2019s NumPy library and also explored five examples to generate an array of evenly spaced values. After reading this tutorial, we hope you can easily generate an array of evenly-spaced values 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\/54111744\/numpy-arange-function-returns-inconsistant-array\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/54111744\/numpy-arange-function-returns-inconsistant-array<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The numpy.arange() function in Python&#8217;s NumPy library is used to generate arrays of evenly spaced values within a specified range. It&#8217;s similar to Python&#8217;s built-in range() function but produces a NumPy array as output. In this article, we will understand Python numpy.arange() function, its syntax, and learn how to use it with the help of [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":22232,"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-21898","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.arange-in-Python.jpg",1000,600,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.arange-in-Python-150x150.jpg",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.arange-in-Python-300x180.jpg",300,180,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.arange-in-Python-768x461.jpg",768,461,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.arange-in-Python.jpg",1000,600,false],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.arange-in-Python.jpg",1000,600,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.arange-in-Python.jpg",1000,600,false]},"uagb_author_info":{"display_name":"Priyanshu Singh","author_link":"https:\/\/codeforgeek.com\/author\/priyanshu\/"},"uagb_comment_info":0,"uagb_excerpt":"The numpy.arange() function in Python&#8217;s NumPy library is used to generate arrays of evenly spaced values within a specified range. It&#8217;s similar to Python&#8217;s built-in range() function but produces a NumPy array as output. In this article, we will understand Python numpy.arange() function, its syntax, and learn how to use it with the help of&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21898","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=21898"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21898\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/22232"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=21898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=21898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=21898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}