{"id":15949,"date":"2020-05-14T00:04:43","date_gmt":"2020-05-14T07:04:43","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=15949"},"modified":"2023-12-01T02:45:31","modified_gmt":"2023-12-01T10:45:31","slug":"python-range","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-range\/","title":{"rendered":"How to Use Python range()"},"content":{"rendered":"\n<p>When you\u2019re coding in Python, you may want to generate a sequence of numbers within a given range. This is a common need when you are creating a for loop, as the number of values in the specified sequence determines how many times the loop is executed.<br><\/p>\n\n\n\n<p>That\u2019s where the Python <code>range()<\/code> method comes in. The <code>range()<\/code> method allows you to generate a sequence of numbers between a start and end number.<br><\/p>\n\n\n\n<p>This tutorial will discuss, with examples, the basics of the Python <code>range()<\/code> function and how you can use it in your code. By the end of reading this tutorial, you\u2019ll be an expert at using the Python <code>range()<\/code> function.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Range<\/h2>\n\n\n\n<p>The <code>range()<\/code> built-in function returns an immutable sequence of numbers between a certain range.<br><\/p>\n\n\n\n<p>The <code>range()<\/code> function can be used in one of two ways.<br><\/p>\n\n\n\n<p>First, the <code>range()<\/code> function can accept one argument, which is the number at which the sequence of integers should stop generating. By default, the <code>range()<\/code> function will start at 0. Here\u2019s the syntax for this usage of <code>range()<\/code>:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>range(stop)<\/pre><\/div>\n\n\n\n<p>Second, <code>range()<\/code> can accept two arguments (with an optional third argument), which specify the start of your range, the end of your range, and the increment between each number in the sequence. The syntax for this usage of <code>range()<\/code> is:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>range(start, stop, [step])<\/pre><\/div>\n\n\n\n<p>The parameters accepted by this syntax are:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>start<\/code>, which refers to the starting value of the range.<\/li>\n\n\n\n<li><code>stop<\/code>, which refers to the final value in the range.<\/li>\n\n\n\n<li><code>step<\/code> (optional), which refers to the increment between each number.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Python Range Examples<\/h2>\n\n\n\n<p>Let\u2019s walk through a few examples to illustrate how the <code>range()<\/code> method works.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">range() with One Parameter<\/h3>\n\n\n\n<p>Suppose we want to generate a sequence of numbers between 0 and 10. We could do so using the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>numbers = range(10)\nprint(list(numbers))<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<\/pre><\/div>\n\n\n\n<p>Let\u2019s break down our code. On the first line, we use range(10) to generate a sequence of numbers between 0 and 10, and we assign our sequence of numbers to the variable <code>numbers<\/code>.<br><\/p>\n\n\n\n<p>Next, we use <code>list()<\/code> which returns a list of our numbers. By default, <code>range()<\/code> returns a range object, so if we want to see our numbers as a list, we need to convert it into a Python list first.<br><\/p>\n\n\n\n<p>Then, we print our newly-generated list to the console. As you can see, our list includes all numbers in the range of 0 and 10.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">range() with Two Parameters<\/h3>\n\n\n\n<p>Suppose we want to generate a sequence of numbers between 5 and 9, and print out each item in the sequence to the console. We could do so using a loop with range, like so:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>numbers = range(5, 9)\nfor n in numbers:\n\tprint(n)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>5\n6\n7\n8<\/pre><\/div>\n\n\n\n<p>Let\u2019s break down our code. On the first line, we use the <code>range()<\/code> Python method to generate a sequence of numbers between 5 and 9.<br><\/p>\n\n\n\n<p>Then, we start a for loop that iterates through every item in the sequence. Because the <code>range()<\/code> method returns a sequence, we can iterate through it in a for loop. Each time the for loop executes, a number from our range is printed to the console.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">range() with Three Parameters<\/h3>\n\n\n\n<p>Now, let\u2019s say we want to create a sequence of numbers between 10 and 20. But, we want each number in our sequence to be 2 values higher than the last one (instead of 1 value higher). After we generate the sequence, we want to print each value to the console.<br><\/p>\n\n\n\n<p>We could achieve this goal by specifying three parameters with the <code>range()<\/code> function. Here\u2019s the code we would use:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>numbers = range(10, 20, 2)\nfor n in numbers:\n\tprint(n)<\/pre><\/div>\n\n\n\n<p>Our code returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>10\n12\n14\n16\n18<\/pre><\/div>\n\n\n\n<p>Our code works in almost the same way as our last example, but with one difference: we have specified an increment parameter. In this case, our increment parameter is equal to 2, which means that every new number generated in our range is 2 values greater than the last.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>The <code>range()<\/code> function allows you to generate a sequence of numbers in Python.<br><\/p>\n\n\n\n<p>By default, <code>range()<\/code> starts counting from 0 and stops at the specified number. But, you can also specify a start value and an increment value to customize the number at which your sequence should start, and the gap between values in your sequence, respectively.<br><\/p>\n\n\n\n<p>This tutorial discussed the basics of the <code>range()<\/code> function and how you can use it in your code. Now you have the knowledge you need to start using the <code>range()<\/code> function like a Python expert!<br><\/p>\n","protected":false},"excerpt":{"rendered":"When you\u2019re coding in Python, you may want to generate a sequence of numbers within a given range. This is a common need when you are creating a for loop, as the number of values in the specified sequence determines how many times the loop is executed. That\u2019s where the Python range() method comes in.&hellip;","protected":false},"author":240,"featured_media":15950,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-15949","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Use Python range(): A Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"The Python range() method allows you to generate a sequence of numbers within a defined range. On Career Karma, learn how to use Python 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:\/\/careerkarma.com\/blog\/python-range\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use Python range()\" \/>\n<meta property=\"og:description\" content=\"The Python range() method allows you to generate a sequence of numbers within a defined range. On Career Karma, learn how to use Python range().\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-range\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-05-14T07:04:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:45:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"How to Use Python range()\",\"datePublished\":\"2020-05-14T07:04:43+00:00\",\"dateModified\":\"2023-12-01T10:45:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/\"},\"wordCount\":697,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-range\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-range\/\",\"name\":\"How to Use Python range(): A Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg\",\"datePublished\":\"2020-05-14T07:04:43+00:00\",\"dateModified\":\"2023-12-01T10:45:31+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"The Python range() method allows you to generate a sequence of numbers within a defined range. On Career Karma, learn how to use Python range().\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-range\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-range\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Use Python range()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Use Python range(): A Complete Guide | Career Karma","description":"The Python range() method allows you to generate a sequence of numbers within a defined range. On Career Karma, learn how to use Python 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:\/\/careerkarma.com\/blog\/python-range\/","og_locale":"en_US","og_type":"article","og_title":"How to Use Python range()","og_description":"The Python range() method allows you to generate a sequence of numbers within a defined range. On Career Karma, learn how to use Python range().","og_url":"https:\/\/careerkarma.com\/blog\/python-range\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-05-14T07:04:43+00:00","article_modified_time":"2023-12-01T10:45:31+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-range\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-range\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"How to Use Python range()","datePublished":"2020-05-14T07:04:43+00:00","dateModified":"2023-12-01T10:45:31+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-range\/"},"wordCount":697,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-range\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-range\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-range\/","url":"https:\/\/careerkarma.com\/blog\/python-range\/","name":"How to Use Python range(): A Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-range\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-range\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg","datePublished":"2020-05-14T07:04:43+00:00","dateModified":"2023-12-01T10:45:31+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"The Python range() method allows you to generate a sequence of numbers within a defined range. On Career Karma, learn how to use Python range().","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-range\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-range\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-range\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/photo-of-woman-using-laptop-3194518.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-range\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"How to Use Python range()"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=15949"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15949\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/15950"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=15949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=15949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=15949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}