

{"id":5932,"date":"2018-01-15T07:39:47","date_gmt":"2018-01-15T02:09:47","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=5932"},"modified":"2026-04-13T18:04:27","modified_gmt":"2026-04-13T12:34:27","slug":"python-range-function","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-range-function\/","title":{"rendered":"Python Range &#8211; Range() Function in Python"},"content":{"rendered":"<p>Something you\u2019ve always seen with a for loop, the Python range() function is a handy feature in Python. As we saw in our tutorial on Python Loops, the range function in Python provides us with a list of numbers to iterate over.<\/p>\n<p>Actually, it returns a range object, which we then convert to a list to iterate on.<\/p>\n<p>So, let\u2019s begin the Python Range Function Tutorial.<\/p>\n<h3>What is Range in Python?<\/h3>\n<p>range() makes a series of numbers that you can step through in a loop. By default, range(5) starts at 0 and stops before 5, giving 0, 1, 2, 3, 4. This keeps count for tasks like printing table rows or adding array indexes.<\/p>\n<p><strong>Key characteristics of Range Function in Python:<\/strong><\/p>\n<ul>\n<li><strong>Uses less memory:<\/strong> Range does not store all the memory at once, if create a number if required which saves memory.<\/li>\n<li><strong>Cannot be changed:<\/strong> Once a range is created, its value stays the same.<\/li>\n<li><strong>Only for whole numbers:<\/strong> It works with integers. If a decimal number is used, Python gives you a type error.<\/li>\n<\/ul>\n<p>First, let\u2019s see what it looks like.<\/p>\n<h4>Syntax of Python range() function<\/h4>\n<p>The Range function in Python takes from one to three arguments. Let\u2019s see how.<\/p>\n<h4>1. One Parameter<\/h4>\n<p>For an argument n, the function returns integer values from 0 to n-1.<\/p>\n<p>range(stop)<\/p>\n<p>Let\u2019s take an example to be clearer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(3))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[0, 1, 2]<\/div>\n<p>What happens if you provide a negative value?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(-3))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[]<\/div>\n<p>The fact that it returned an empty list tells us that range() is internally coded to traverse to the right.<\/p>\n<p>And there are no integers from 0 to the right -3, because -3 falls to the left of 0.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(0))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[]<\/div>\n<h4>2. Two Parameters<\/h4>\n<p>Now, we\u2019ll try different combinations for two parameters.<\/p>\n<p>range(start,stop)<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(1,7))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 4, 5, 6]<\/div>\n<p>Here, both arguments are positive. Also, 7 falls to the right of 1. So, it prints integers from 1 to 6 (7-1).<\/p>\n<p>Hence, we conclude that it prints integers from the first number to one from the second.<\/p>\n<p>Now we try passing arguments where the first is greater than the second.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(7,1))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[]<\/div>\n<p>To back what we just said, it returns an empty list because 7 falls to the right of 1, and it traverses to the right from 7. Therefore, it never reaches 1.<\/p>\n<p>In section a, we saw that we can\u2019t pass a negative argument to range(). But it is indeed possible to pass a negative value or two when you pass two or more arguments.<\/p>\n<p>Let\u2019s see how.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(-7,3))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[-7, -6, -5, -4, -3, -2, -1, 0, 1, 2]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(-7,-3))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[-7, -6, -5, -4]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(-3,-7))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[]<\/div>\n<p>This, again, returns an empty list, for -3 lies to the right of -7.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(3,3))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[]<\/div>\n<p>There are no integers between 3 and less than 3.<\/p>\n<h4>3. Three Parameters<\/h4>\n<p>Finally, the range() function can also take a third parameter. This is for the interval.<br \/>\nrange(start,stop,interval)<\/p>\n<p>We\u2019ll see this one by example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(7,1,-1))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[7, 6, 5, 4, 3, 2]<\/div>\n<p>After bragging about how you can\u2019t have the second argument smaller than the first one, now we tell you that you can. But on one condition- you must specify a negative interval.<\/p>\n<p>Here, we used -1 as an interval. We could\u2019ve used -2 as well.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(7,1,-2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[7, 5, 3]<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(7,1,-8))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[7]<\/div>\n<p>Here, note that the first integer, 7, is always returned, even though the interval -8 sends it beyond 1.<\/p>\n<p>Let\u2019s now take a look at more examples for three parameters.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; list(range(1,7,1.5))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#17&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>list(range(1,7,1.5))<\/p>\n<p>TypeError: &#8216;float&#8217; object cannot be interpreted as an integer<\/p>\n<\/div>\n<p>Note that all three arguments must be integers only.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(1,7,2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 3, 5]<\/div>\n<p>Let\u2019s take another example.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; list(range(1,7,0))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#26&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>list(range(1,7,0))<\/p>\n<p>ValueError: range() arg 3 must not be zero<\/p>\n<\/div>\n<p>It raised a value error, because the interval cannot be zero if you need to go from one number to another.<\/p>\n<p>Actually, when we provide one or two arguments, the interval is assumed to be +1. This is why we were unable to have it traverse to the left.<\/p>\n<p>Now that you know what\u2019s going on, it is easier to toy with range().<\/p>\n<p>Let\u2019s take one last example to make sure we\u2019re on the right track of thoughts.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; list(range(12,2,2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>[]<\/p>\n<\/div>\n<p>Now this returns an empty list in python because in a positive interval of 2 means traversing to the right, but 2 falls to the left of 12.<\/p>\n<h3>Python Iterate Function<\/h3>\n<p>Now that we know how to use the Python range() function, don\u2019t you want to know where to use it? Well, one application is the for loop.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for i in range(6):\r\n         print(i*2)\u00a0\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0<br \/>\n2<br \/>\n4<br \/>\n6<br \/>\n8<br \/>\n10<\/div>\n<p>So, this was all about the Python Range Function. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Range() Function<\/h3>\n<p>1. What is Python range() function?<\/p>\n<p>2. What does range() do in Python?<\/p>\n<p>3. How do you find the range in Python?<\/p>\n<p>4. How does range() work in Python?<\/p>\n<p>5. What is the syntax\u00a0 for range() function in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>To sum this tutorial up on Python Range, range function in python is an in-built function, in Python, that lends us a sequence of integers.<\/p>\n<p>We can use this to iterate on using a for loop. Now you know that it\u2019s possible to call range() in python with one, two, or three arguments.<\/p>\n<p>We would like you to come up with a creative use of the range() function; tell us in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Something you\u2019ve always seen with a for loop, the Python range() function is a handy feature in Python. As we saw in our tutorial on Python Loops, the range function in Python provides us&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":35865,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[36646,36644,10792,10793,11312,11313,36645],"class_list":["post-5932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-different-arguments-in-range-function","tag-how-to-iterate-through-range-function","tag-python-range-function","tag-python-range","tag-range-function-in-python","tag-range-in-python","tag-syntax-of-python-range-function"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Range - Range() Function in Python - DataFlair<\/title>\n<meta name=\"description\" content=\"Master Python Range function - what is the range() function in Python with syntax and examples, how to iterate through range().\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-range-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Range - Range() Function in Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Master Python Range function - what is the range() function in Python with syntax and examples, how to iterate through range().\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-range-function\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-15T02:09:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-13T12:34:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/range-in-Python-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Range - Range() Function in Python - DataFlair","description":"Master Python Range function - what is the range() function in Python with syntax and examples, how to iterate through range().","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-range-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Range - Range() Function in Python - DataFlair","og_description":"Master Python Range function - what is the range() function in Python with syntax and examples, how to iterate through range().","og_url":"https:\/\/data-flair.training\/blogs\/python-range-function\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-15T02:09:47+00:00","article_modified_time":"2026-04-13T12:34:27+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/range-in-Python-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Range &#8211; Range() Function in Python","datePublished":"2018-01-15T02:09:47+00:00","dateModified":"2026-04-13T12:34:27+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/"},"wordCount":882,"commentCount":4,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/range-in-Python-1.jpg","keywords":["different arguments in range function","how to iterate through range function","python range function","python range()","range function in python","range() in python","syntax of python range function"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-range-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/","url":"https:\/\/data-flair.training\/blogs\/python-range-function\/","name":"Python Range - Range() Function in Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/range-in-Python-1.jpg","datePublished":"2018-01-15T02:09:47+00:00","dateModified":"2026-04-13T12:34:27+00:00","description":"Master Python Range function - what is the range() function in Python with syntax and examples, how to iterate through range().","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-range-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/range-in-Python-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/range-in-Python-1.jpg","width":1200,"height":628,"caption":"Python Range Function Tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-range-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Range &#8211; Range() Function in Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=5932"}],"version-history":[{"count":15,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5932\/revisions"}],"predecessor-version":[{"id":147594,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5932\/revisions\/147594"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/35865"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=5932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=5932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=5932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}