{"id":75368,"date":"2020-01-22T09:46:15","date_gmt":"2020-01-22T04:16:15","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75368"},"modified":"2020-01-22T09:46:15","modified_gmt":"2020-01-22T04:16:15","slug":"python-range-function","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/","title":{"rendered":"Python Range() Function &#8211; Learn to iterate numbers using loops"},"content":{"rendered":"<p>We have already used the <strong>range()<\/strong> <strong>function<\/strong> in our <strong>Python loops<\/strong> tutorial but in this article, we are going to understand the range() function in more detail.<\/p>\n<p>So, let\u2019s start with the Python range() function tutorial.<\/p>\n<h3>Python Range Function<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-of-python-range-function.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-75689 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/arguments-of-python-range-function.jpg\" alt=\"python range function arguments\" width=\"766\" height=\"336\" \/><\/a><\/h3>\n<p>The range() function in Python is an <strong>inbuilt<\/strong> method which is used to <strong>generate<\/strong> a <strong>list<\/strong> of <strong>numbers<\/strong> which we can iterate on using <strong>loops<\/strong>.<\/p>\n<p>The range() function is a renamed version in<strong> Python(3.x)<\/strong> of a function named<strong> xrange()<\/strong> in <strong>Python(2.x)<\/strong>.<\/p>\n<p>Python range function has basically <strong>three arguments<\/strong>.<\/p>\n<ul>\n<li><strong>Start:<\/strong> Integer representing the <strong>start<\/strong> of the range object.<\/li>\n<li><strong>Stop:<\/strong> Integer representing the <strong>end<\/strong> of the range object.<\/li>\n<li><strong>Step(optional):<\/strong> Integer representing the <strong>increment<\/strong> after each value.<\/li>\n<\/ul>\n<p>The <strong>range() function<\/strong> can be called in <strong>three<\/strong> ways &#8211;<\/p>\n<ul>\n<li><strong>range(stop)<\/strong><\/li>\n<li><strong>range(start, stop)<\/strong><\/li>\n<li><strong>range(start, stop, step)<\/strong><\/li>\n<\/ul>\n<p>Let&#8217;s discuss the above ways of Python range functions in detail.<\/p>\n<h3>1. range(stop) &#8211; One argument<\/h3>\n<p>The <strong>range() function<\/strong> with one parameter takes the <strong>stop<\/strong> <strong>argument<\/strong> and it then returns us a <strong>range<\/strong> <strong>object<\/strong> from values between <strong>0<\/strong> to stop<strong> &#8211; 1<\/strong>.<\/p>\n<p>We can <strong>iterate<\/strong> over the <strong>range object<\/strong> using a <strong>loop<\/strong> or we can also <strong>convert<\/strong> the <strong>range<\/strong> into a <strong>list<\/strong>.<\/p>\n<p>Let see the example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( range(5))\nprint( list(range(5)))<\/pre>\n<p><strong>Output: <\/strong><\/p>\n<div class=\"code-output\">range(0,5)<br \/>\n[0, 1, 2, 3, 4, 5]<\/div>\n<p>Here, we called the <strong>function range(5)<\/strong> and it <strong>generated<\/strong> numbers between <strong>0<\/strong> to<strong> (5-1)<\/strong> i.e. <strong>0<\/strong> to <strong>4<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( list(range(10) ))\nprint( list(range(-4) ))<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br \/>\n[]<\/div>\n<h3>2. range( start, stop) &#8211; Two arguments<\/h3>\n<p>The range function with <strong>two argument<\/strong> takes the <strong>start<\/strong> and <strong>stop<\/strong> arguments. This way we can <strong>generate<\/strong> the <strong>range<\/strong> object <strong>between<\/strong> any <strong>two integer values<\/strong>.<\/p>\n<p>The integer values can be from <strong>positive<\/strong> to <strong>negative<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( list(range(5, 10)) )\nprint( list(range( -2, 4)) )\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[5, 6, 7, 8, 9]<br \/>\n[-2, -1, 0, 1, 2, 3]<\/div>\n<p>Here, we see that the <strong>range(start, stop)<\/strong> function will <strong>generate<\/strong> numbers from <strong>start<\/strong> to <strong>stop<\/strong> <strong>&#8211;<\/strong> <strong>1<\/strong>.<\/p>\n<p>We can also generate between <strong>negative integers<\/strong> but if the <strong>start value<\/strong> is <strong>greater<\/strong> or <strong>equal<\/strong> to <strong>stop value<\/strong>, then we will get an <strong>empty list<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( list(range(4, 0)) )\nprint( list(range(-2, -10)) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[]<br \/>\n[]<\/div>\n<h3>3. range( start, stop, step) &#8211; Three arguments<\/h3>\n<p>The<strong> range()<\/strong> function with <strong>three arguments<\/strong> take the <strong>start<\/strong>, <strong>stop<\/strong> and a <strong>step<\/strong> as an argument. With the step argument, we get more <strong>flexibility<\/strong> in generating <strong>numbers<\/strong> <strong>within<\/strong> a <strong>range<\/strong>. This is useful in <strong>skipping<\/strong> numbers <strong>between<\/strong> the <strong>range<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( list(range(0,10,2)) )\nprint( list(range(-5,5,3)) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[0, 2, 4, 6, 8]<br \/>\n[-5, -2, 1, 4]<\/div>\n<p>Here in the first line, we generated numbers from <strong>0<\/strong> to <strong>10-1<\/strong> by <strong>incrementing<\/strong> with <strong>2<\/strong>. In the second-line, we also saw the same with <strong>negative numbers<\/strong>.<\/p>\n<p>Recently we told you that the <strong>start<\/strong> should be <strong>smaller<\/strong> than <strong>stop<\/strong> otherwise we will get an <strong>empty list<\/strong>. There\u2019s a way around by using a <strong>negative integer<\/strong> in step. It then <strong>decrements<\/strong> the numbers.<\/p>\n<p>Let\u2019s see this with an example.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">print( list(range(10,0,-1)) )\nprint( list(range(5,-5,-2)) )<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]<br \/>\n[5, 3, 1, -1, -3]<\/div>\n<p><strong>Note:<\/strong> The <strong>range()<\/strong> function only takes <strong>integer values<\/strong> in its argument and the <strong>third<\/strong> argument <strong>cannot<\/strong> be <strong>zero<\/strong>.<\/p>\n<h3>Iterating on range object<\/h3>\n<p>We saw the <strong>working<\/strong> of the <strong>range()<\/strong> function and now let\u2019s see the <strong>application<\/strong>. This function is mostly used in <strong>loops<\/strong> to <strong>iterate<\/strong> over a <strong>range of numbers<\/strong>.<\/p>\n<p>The <strong>advantage<\/strong> of this method is that it <strong>does not<\/strong> <strong>generate<\/strong> all the <strong>numbers range<\/strong> at <strong>once<\/strong>. It <strong>yields<\/strong> the <strong>sequence<\/strong> <strong>one by one<\/strong> so it is <strong>memory efficient<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for i in range(4):\n  print(i)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">0<br \/>\n1<br \/>\n2<br \/>\n3<\/div>\n<p><strong>Note:<\/strong> The range() function only <strong>supports integers<\/strong> in the argument.<\/p>\n<p>Using a <strong>float value<\/strong> in <strong>range()<\/strong> will <strong>raise<\/strong> an <strong>error<\/strong>.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for i in range(3.4):\n  print(i)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">Traceback (most recent call last):<br \/>\n<span style=\"font-weight: 400\">\u00a0\u00a0<\/span>File &#8220;&lt;stdin&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\nTypeError: &#8216;float&#8217; object cannot be interpreted as an integer<\/div>\n<h3>Summary<\/h3>\n<p>In this article, we talked everything about the <strong>range() function<\/strong> which is the<strong> built-in function<\/strong> of <strong>Python<\/strong>. We saw <strong>three<\/strong> different <strong>ways<\/strong> to <strong>call<\/strong> the <strong>range() function<\/strong> based on the <strong>number of arguments passed<\/strong>. Moreover, we saw the <strong>application<\/strong> of <strong>range()<\/strong> function in <strong>loops<\/strong>.<\/p>\n<p>Now you can easily use Python range() function in your programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have already used the range() function in our Python loops tutorial but in this article, we are going to understand the range() function in more detail. So, let\u2019s start with the Python range()&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75588,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[1345,1346,1347,1348,1349,1350,1351],"class_list":["post-75368","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-iterating-on-range-object","tag-python-range-argument-start","tag-python-range-argument-step","tag-python-range-argument-stop","tag-python-range-function","tag-python-range-function-arguments","tag-range-function-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Range() Function - Learn to iterate numbers using loops - TechVidvan<\/title>\n<meta name=\"description\" content=\"Python range function - Explore the inbuilt method of Python which is range function in Python. Also, get to know about its three arguments and its ways.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/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() Function - Learn to iterate numbers using loops - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Python range function - Explore the inbuilt method of Python which is range function in Python. Also, get to know about its three arguments and its ways.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-range-function\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-22T04:16:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-range-function.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Range() Function - Learn to iterate numbers using loops - TechVidvan","description":"Python range function - Explore the inbuilt method of Python which is range function in Python. Also, get to know about its three arguments and its ways.","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:\/\/techvidvan.com\/tutorials\/python-range-function\/","og_locale":"en_US","og_type":"article","og_title":"Python Range() Function - Learn to iterate numbers using loops - TechVidvan","og_description":"Python range function - Explore the inbuilt method of Python which is range function in Python. Also, get to know about its three arguments and its ways.","og_url":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-22T04:16:15+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-range-function.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Range() Function &#8211; Learn to iterate numbers using loops","datePublished":"2020-01-22T04:16:15+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/"},"wordCount":626,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-range-function.jpg","keywords":["Iterating on range object","python range argument start","python range argument step","python range argument stop","python range function","Python range function arguments","range function in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-range-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/","url":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/","name":"Python Range() Function - Learn to iterate numbers using loops - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-range-function.jpg","datePublished":"2020-01-22T04:16:15+00:00","description":"Python range function - Explore the inbuilt method of Python which is range function in Python. Also, get to know about its three arguments and its ways.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-range-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-range-function.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/python-range-function.jpg","width":802,"height":420,"caption":"range function in python"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-range-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Range() Function &#8211; Learn to iterate numbers using loops"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75368"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75368\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75588"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}