{"id":3514,"date":"2023-08-01T09:58:20","date_gmt":"2023-08-01T09:58:20","guid":{"rendered":"https:\/\/www.copahost.com\/blog\/?p=3514"},"modified":"2023-08-03T12:57:05","modified_gmt":"2023-08-03T12:57:05","slug":"python-range","status":"publish","type":"post","link":"https:\/\/www.copahost.com\/blog\/python-range\/","title":{"rendered":"Python range: the complete range function guide"},"content":{"rendered":"\n<p>The <strong>range function<\/strong> is a built-in function in Python that allows you to <strong>generate sequences of numbers<\/strong> in a simple and controlled way.&nbsp;Thus, the function is very useful in many cases, from a simple iteration over a list or dictionary to creating checklists and other more complex cases.<\/p>\n\n\n\n<p>In this article, we&#8217;ll explore the functionality of the range function in Python, including its <strong>basic and advanced syntax<\/strong>, how to use it in a loop, and compare it to other solutions available in Python.&nbsp;Let&#8217;s start by introducing the basic syntax of the range function, as well as ways to change its parameters to generate custom sequences.<\/p>\n\n\n\n<p>Next, we&#8217;ll show you how to use the range function in different types of loops, such as a for loop and a while loop, to perform operations on each number in the generated sequence.&nbsp;We will also show some practical examples of using the range function in&nbsp;<a href=\"https:\/\/www.copahost.com\/blog\/what-is-python\/\">Python<\/a>&nbsp;programs.<\/p>\n\n\n\n<p>Finally, let&#8217;s compare the range function with other solutions available in Python, such as the xrange function and the range() class.&nbsp;By the end of the article, we will have a deeper understanding of how to use the range function in Python and how to apply it to solve programming problems in different contexts.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Range_function_syntax_in_python\" title=\"Range function syntax in python\">Range function syntax in python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Range_function_with_map_function\" title=\"Range function with map function\">Range function with map function<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Generating_sequences_with_the_range_function\" title=\"Generating sequences with the range function\">Generating sequences with the range function<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Continuous_Sequences\" title=\"Continuous Sequences\">Continuous Sequences<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Partial_Sequences\" title=\"Partial Sequences\">Partial Sequences<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Using_the_range_function_in_a_loop_for\" title=\"Using the range function in a loop&nbsp;for&nbsp;\">Using the range function in a loop&nbsp;for&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Practical_examples_of_using_the_function_range_in_Python_programs\" title=\"Practical examples of using the function&nbsp;&nbsp;range&nbsp;in Python programs\">Practical examples of using the function&nbsp;&nbsp;range&nbsp;in Python programs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Comparison_of_range_function_in_python_with_other_available_solutions\" title=\"Comparison of range function in python with other available solutions\">Comparison of range function in python with other available solutions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.copahost.com\/blog\/python-range\/#Applications_of_the_range_function_in_python\" title=\"Applications of the range function in python\">Applications of the range function in python<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Range_function_syntax_in_python\"><\/span>Range function syntax in python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Basic syntax<\/h4>\n\n\n\n<p>The basic syntax of the range function in Python.&nbsp;Which allows you to create a sequence of values \u200b\u200bor integers, with optional increment or difference between consecutive values.<\/p>\n\n\n\n<p>Example to generate sequence of integers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(1, 10) # Generate the sequence &#091;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/code><\/pre>\n\n\n\n<p>Alternatively, you can also specify the sequence of values \u200b\u200bdirectly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(1, 9, 2) # Generate the sequence &#091;1, 3, 5, 7, 9]<\/code><\/pre>\n\n\n\n<p>And you can also use the operator &nbsp;&nbsp;<code>*<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(1, 9, 2)*2 # Generate the sequence &#091;1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Advanced syntax<\/h4>\n\n\n\n<p>In addition to the basic syntax, there are several advanced features that make the function more powerful and versatile.<\/p>\n\n\n\n<p>Consequently one of these features is the ability to use the argument&nbsp;<code>step<\/code>to specify a custom increment or difference between consecutive values \u200b\u200bin the string.&nbsp;Thus allowing to generate a sequence of data with a specific pattern, such as a sequence of prime numbers or a sequence of Fibonacci numbers.<\/p>\n\n\n\n<p>See an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(start, stop, step)<\/code><\/pre>\n\n\n\n<p>where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>start<\/code>&nbsp;<\/strong>is the first value in the sequence you want to generate<\/li>\n\n\n\n<li><strong><code>stop<\/code>&nbsp;<\/strong>is the last value in the sequence you want to generate<\/li>\n\n\n\n<li><strong><code>step<\/code>&nbsp;<\/strong>is the increment or difference between consecutive values \u200b\u200bin the sequence (optional)<\/li>\n<\/ul>\n\n\n\n<p>You can use the function&nbsp;&nbsp;<code>range()<\/code> to generate a sequence of values \u200b\u200band then use those values \u200b\u200bto create a list or set.&nbsp;See in the example, the function&nbsp;&nbsp;<code><em>generate_sequence<\/em><\/code> receives the three arguments:&nbsp;&nbsp;<code>start<\/code>,&nbsp;&nbsp;<code>stop<\/code>&nbsp;and&nbsp;&nbsp;<code>step<\/code>.&nbsp;O&nbsp;&nbsp;<code>start<\/code>&nbsp;is the start value of the sequence, o&nbsp;&nbsp;<code>stop<\/code>&nbsp;is the end value of the sequence, and o&nbsp;&nbsp;<code>step<\/code>&nbsp;is the step size between values \u200b\u200bin the sequence.&nbsp;The function returns the generated sequence of integers:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to generate a sequence of integers\ndef generate_sequence(start, stop, step):\n    # Creates the sequence of integers\n    sequence = range(start, stop, step)\n    return sequence\n\n# Example of using the function\nsequence = generate_sequence(1, 10, 1)\nprint(sequence) <\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;1, 2, 3, 4, 5, 6, 7, 8, 9]<\/code><\/pre>\n\n\n\n<p>Another way to use it is as a loop.&nbsp;In this example, the function&nbsp;&nbsp;<code><em>generate_sequence<\/em><\/code>&nbsp;is used inside a&nbsp;loop&nbsp;<code>while<\/code>&nbsp;&nbsp;to generate a sequence of integers from 1 to 10 and print each value in the sequence:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to generate a sequence of integers\ndef generate_sequence(start, stop, step):\n    # Creates the sequence of integers\n    sequence = range(start, stop, step)\n    return sequence\n\n# Example of using the function as a loop\ni = 1\nwhile i &lt;= 10:\n    print(i)\n    i += 1<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n2\n3\n4\n5\n6\n7\n8\n9\n10<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Range_function_with_map_function\"><\/span>Range function with map function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Another advanced feature of the range function is its ability to be used in conjunction with the&nbsp;&nbsp;<code>map()<\/code>.&nbsp;The function&nbsp;<code>map()<\/code> lets you apply a specific function or operation to each value in the sequence, and the function&nbsp;<code>range()<\/code> lets you specify the range of values \u200b\u200bover which the function should be applied.&nbsp;This allows you to create amazing tools.<\/p>\n\n\n\n<p>In this example, the function&nbsp;&nbsp;<code><em>add_one<\/em><\/code>&nbsp;is a function that adds 1 to each number passed to it.&nbsp;The function generates a sequence of integers from 1 to 10. Then the function applies the function to each element of the generated sequence and prints the resulting list.&nbsp;Look:<em>&nbsp;<code>generate_sequence<\/code><\/em><code> map() <\/code><em><code>add_<\/code>one<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to add  to each number in the sequence\ndef add_one(number):\n    return number + 1\n\n# Function to generate a sequence of integers\ndef generate_sequence(start, stop):\n    # Creates the sequence of integers\n    sequence = range(start, stop)\n    return sequence\n\n# Example of using the function\nsequence = generate_sequence(1, 10)\nmapped = map(add_one, sequence)\nprint(mapped)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> &#091;2, 3, 4, 5, 6, 7, 8, 9, 10]<\/code><\/pre>\n\n\n\n<p>Another example, the function&nbsp;&nbsp;<code><em>add_two<\/em><\/code>&nbsp;is a function that multiplies each number passed to it by 2. The function&nbsp;&nbsp;<em><code>generate_sequence<\/code>&nbsp;<\/em>is used to generate a sequence of integers from 1 to 10. Then the function&nbsp;&nbsp;<code>map()<\/code>&nbsp;is used to apply the function&nbsp;&nbsp;<code><em>add_two<\/em><\/code>&nbsp;to each element of the sequence generated and the resulting list is printed.&nbsp;Look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to add 2 to each number in the sequence\ndef add_two(number):\n    return number * 2\n\n# Function to generate a sequence of integers\ndef generate_sequence(start, stop):\n    # Creates the sequence of integers\n    sequence = range(start, stop)\n    return sequence\n\n# Example of using the function\nsequence = generate_sequence(1, 10)\nmapped = map(add_two, sequence)\nprint(mapped)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<p>[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]<\/p>\n\n\n\n<p>Overall, understanding its advanced features and various usages, you can use the function&nbsp;<code>range()<\/code> to generate valuable and useful data for your projects and to create more powerful and efficient data analysis and software development tools.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generating_sequences_with_the_range_function\"><\/span>Generating sequences with the range function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Continuous_Sequences\"><\/span>Continuous Sequences<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The Python function range() generates a sequence of continuous numbers.&nbsp;It takes three parameters: the first is the starting number of the sequence, the second is the last number of the sequence and the third is the step of the sequence.<\/p>\n\n\n\n<p>When using the function&nbsp;&nbsp;<code>range()<\/code>&nbsp;with a step of 1, you will get a continuous sequence of integers, starting with the starting number and ending with the last number.&nbsp;For example,&nbsp;&nbsp;<code>range(1, 6)<\/code>&nbsp;it will generate the sequence of integers from 1 to 5.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range1 = range(1, 6)\nprint(range1)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(1, 6)\n&#091;1, 2, 3, 4, 5]<\/code><\/pre>\n\n\n\n<p>If you want a partial sequence with a different step, you can specify the step as the third parameter of the function&nbsp;&nbsp;<code>range()<\/code>.&nbsp;For example,&nbsp;&nbsp;<code>range(1, 10, 2)<\/code>&nbsp;it will generate the partial sequence of integers from 1 to 8, with a step of 2.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range2 = range(1, 10, 2)\nprint(range2)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(1, 10, 2)\n&#091;1, 3, 5, 7, 9]<\/code><\/pre>\n\n\n\n<p>Note that if you specify the last number as the function&#8217;s second parameter&nbsp;&nbsp;<code>range()<\/code>, it will generate a continuous sequence of integers, starting with the starting number and ending with the last specified number.&nbsp;For example,&nbsp;&nbsp;<code>range(1, 11)<\/code>&nbsp;it will generate the continuous sequence of integers from 1 to 10.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range3 = range(1, 11)\nprint(range3)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(1, 11)\n&#091;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/code><\/pre>\n\n\n\n<p>However, if you need to generate continuous sequences with custom values, you can change the function&#8217;s parameters&nbsp;&nbsp;<code>range()<\/code>.&nbsp;For example,&nbsp;&nbsp;<code>range(10)<\/code>&nbsp;it will generate a continuous sequence of integers from 0 to 9. If you want to start the sequence at the number 100, you can do this by setting the second parameter to 100:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range4 = range(100)<br>print(range4)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(100)<br>&#091;100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30,29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]<\/code><\/pre>\n\n\n\n<p>This is one of the ways to generate continuous sequences with the&nbsp;<code>range<\/code>.&nbsp;You can also adjust the sequence step using the third parameter, as mentioned earlier.&nbsp;Remember that if the second parameter is less than the first, the function&nbsp;<code>range()<\/code> will generate an empty string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Partial_Sequences\"><\/span>Partial Sequences<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The Python function range() can generate partial sequences, that is, sequences that contain a limited number of elements.&nbsp;Therefore, you can specify a second parameter greater than the first, which will cause the function to return an empty string.<\/p>\n\n\n\n<p>For example,&nbsp;&nbsp;<code>range(5, 15)<\/code>&nbsp;it will generate the partial sequence of integers from 5 to 14.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range5 = range(5, 15)\nprint(range5)<\/code><\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range(5, 15)\n&#091;5, 6, 7, 8, 9, 10, 11, 12, 13, 14]<\/code><\/pre>\n\n\n\n<p>On the other hand, if you want a partial sequence with a different step, you can specify the step as the third parameter of the function&nbsp;&nbsp;<code>range()<\/code>.&nbsp;In the example, it will generate the partial sequence of integers.<\/p>\n\n\n\n<p>range(1, 10, 2)<\/p>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;1, 3, 5, 7, 9]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_the_range_function_in_a_loop_for\"><\/span>Using the range function in a loop&nbsp;<code>for<\/code>&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The function\u00a0\u00a0<code>range<\/code>\u00a0is a built-in function in Python that allows you to generate sequences of numbers.\u00a0We can use a <a href=\"https:\/\/www.copahost.com\/blog\/for-python\/\">for loop<\/a> to iterate over a sequence of numbers in Python and perform an action for each number in the sequence.<\/p>\n\n\n\n<p>That way, to use the function&nbsp;&nbsp;<code>range<\/code>&nbsp;in a&nbsp;for loop&nbsp;, just pass the argument&nbsp;&nbsp;<code>range<\/code>&nbsp;to the function&nbsp;&nbsp;<code>for<\/code>&nbsp;and specify the necessary parameters.&nbsp;For example, to iterate over a sequence of numbers from 0 to 4, the code would be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    print(i)<\/code><\/pre>\n\n\n\n<p>The result would be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2\n3\n4<\/code><\/pre>\n\n\n\n<p>Also, you can use the function&nbsp;&nbsp;<code>range<\/code>&nbsp;in a for loop to iterate over a sequence of numbers, with or without increments.&nbsp;For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(5):\n    print(i)<\/code><\/pre>\n\n\n\n<p>Likewise, the result would be the same as the previous example.<\/p>\n\n\n\n<p>You can also specify optional parameters&nbsp;&nbsp;<code>range<\/code>&nbsp;to generate a sequence of different numbers.&nbsp;For example, to iterate over a sequence of numbers from 1 to 10, excluding even numbers, the code would be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for i in range(1, 11, 2):\n    print(i)<\/code><\/pre>\n\n\n\n<p>The result would be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n3\n5\n7\n9<\/code><\/pre>\n\n\n\n<p>In short, the function&nbsp;&nbsp;<code>range<\/code>&nbsp;creates a sequence of integers, starting at the number specified in the first parameter and ending at the number specified in the second parameter.&nbsp;Furthermore, if you need a sequence of non-integer numbers or with a different range, you can use other functions such as&nbsp;&nbsp;<code>xrange<\/code>,&nbsp;&nbsp;<code>range(stop=None)<\/code>&nbsp;or&nbsp;&nbsp;<code>range(1, stop)<\/code>, respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Practical_examples_of_using_the_function_range_in_Python_programs\"><\/span>Practical examples of using the function&nbsp;&nbsp;<code>range<\/code>&nbsp;in Python programs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The function&nbsp;&nbsp;<code>range<\/code>&nbsp;is a built-in function in Python that allows you to generate sequences of numbers.&nbsp;Python programs can use the language in many practical situations.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Creating a list containing the first 1000000000 (10 billion) perfect numbers.<\/h4>\n\n\n\n<p>As a result, a practical example of using the function&nbsp;&nbsp;<code>range<\/code>&nbsp;would be to create a list containing the first 1000000000 (10 billion) perfect numbers.&nbsp;For this, it is possible to use a loop&nbsp;&nbsp;<code>for<\/code>&nbsp;and the function&nbsp;&nbsp;<code>range<\/code>&nbsp;as follows, in the code below it will create a called list&nbsp;&nbsp;<code><em>numbers<\/em><\/code>&nbsp;that contains the first 10 billion perfect numbers.&nbsp;We will print each number to the screen with each iteration of the loop.&nbsp;Look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>n = 1000000000\nnumbers = &#091;]\n\nfor i in range(n):\n    numbers.append(i)\n    print(i)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Creating a list containing numbers from 1 to 100, added alternately.<\/h4>\n\n\n\n<p>In this way, it is possible to use a loop&nbsp;&nbsp;<code>for<\/code>&nbsp;and the function&nbsp;&nbsp;<code>range<\/code>&nbsp;as follows, in the code below it will create a called list&nbsp;&nbsp;<em><code>numbers<\/code>&nbsp;<\/em>that contains the numbers from 1 to 100, added alternately.&nbsp;So we&#8217;ll add each number to the list if it&#8217;s even, or subtract 1 if it&#8217;s odd.&nbsp;Look:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#091;]\n\nfor i in range(1, 101):\n    if i % 2 == 0:\n        numbers.append(i)\n    else:\n        numbers.append(i - 1)\n\nprint(numbers)<\/code><\/pre>\n\n\n\n<p>The result will be a list with the numbers 1, 3, 5, 7, \u2026, 97, 99.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Creating a list containing numbers from 1 to 1000, divided into groups of 10.<\/h4>\n\n\n\n<p>Soon we can see that it is possible to use the function&nbsp;&nbsp;<code>range<\/code>&nbsp;in conjunction with a cycle&nbsp;&nbsp;<code>for<\/code>&nbsp;to divide a sequence of numbers into groups.&nbsp;To do this, just specify the number of elements you want to include in each group, adding the parameter&nbsp;&nbsp;<code>step<\/code>&nbsp;to the function&nbsp;&nbsp;<code>range<\/code>.<\/p>\n\n\n\n<p>For example, we can create a list that contains the numbers from 1 to 1000, divided into groups of 10, the code would be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>numbers = &#091;]\n\nfor i in range(1, 1001, 10):\n    numbers.append(i)\n\nprint(numbers)<\/code><\/pre>\n\n\n\n<p>So if you specify the parameter&nbsp;&nbsp;<code>step<\/code>&nbsp;as 1, the function&nbsp;&nbsp;<code>range<\/code>&nbsp;will generate a sequence of continuous numbers, starting at the specified number and ending at the specified number.&nbsp;However, if you specify a&nbsp;&nbsp;<code>step<\/code>&nbsp;negative, the sequence of numbers will be generated in reverse order.&nbsp;If you specify a&nbsp;&nbsp;<code>step<\/code>&nbsp;different than 1, the sequence of numbers will be generated with a different interval than normal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Comparison_of_range_function_in_python_with_other_available_solutions\"><\/span>Comparison of range function in python with other available solutions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The function&nbsp;&nbsp;<code>range<\/code>in python is a built-in function that returns a sequence of contiguous integers.&nbsp;This function is equivalent to the function&nbsp;&nbsp;<code>xrange()<\/code>&nbsp;in Python 2, which returns a sequence of contiguous integers, on the other hand, instead of returning a copy of the numbers, it returns an object&nbsp;&nbsp;<code>xrange<\/code>&nbsp;that is a reference to a table of numbers.<\/p>\n\n\n\n<p>In fact, class&nbsp;&nbsp;<code>range<\/code>in python is a class where derived from class&nbsp;&nbsp;<code>xrange()<\/code>, and has the same methods and attributes.&nbsp;However we have the main difference between the two is that the class&nbsp;&nbsp;<code>range()<\/code>&nbsp;is used to create an instance of an object&nbsp;&nbsp;<code>range<\/code>, while the function&nbsp;&nbsp;<code>xrange()<\/code>&nbsp;returns a reference to an object&nbsp;&nbsp;<code>xrange<\/code>.<\/p>\n\n\n\n<p>Although the class&nbsp;&nbsp;<code>range<\/code>&nbsp;is a more modern way of accessing the sequence of contiguous integers, the function&nbsp;&nbsp;<code>xrange()<\/code>&nbsp;is still widely used in older code and in some situations where you need to create a&nbsp;&nbsp;<code>xrange<\/code>.<\/p>\n\n\n\n<p>Still, in terms of performance, function&nbsp;&nbsp;<code>range<\/code>&nbsp;is more efficient than class&nbsp;&nbsp;<code>range()<\/code>because it is a built-in function and does not require creating a new object instance.&nbsp;Also, class&nbsp;&nbsp;<code>range()<\/code>&nbsp;is more complex than function&nbsp;&nbsp;<code>xrange()<\/code>, and may be less readable for developers.<\/p>\n\n\n\n<p>In general, the choice between using the function&nbsp;&nbsp;<code>range<\/code>in python, the function&nbsp;&nbsp;<code>xrange()<\/code>&nbsp;or the class&nbsp;&nbsp;<code>range()<\/code>&nbsp;depends on the specific needs of the project and the experience of the developer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Applications_of_the_range_function_in_python\"><\/span>Applications of the range function in python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>People widely apply Python&#8217;s range() function across a variety of applications, including:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Mathematical calculations:<\/strong>&nbsp;&nbsp;People often use the function to generate sequences of numbers in mathematical calculations, such as iterating over a range of numbers.<\/li>\n\n\n\n<li><strong>Testing Programs:<\/strong>&nbsp;&nbsp;People widely use Python to generate sequences of values \u200b\u200bin testing programs.<\/li>\n\n\n\n<li><strong>Generating random numbers:<\/strong>&nbsp;&nbsp;The function&nbsp;&nbsp;<code>range()<\/code>&nbsp;can be used to generate sequences of random numbers, such as in games or simulations.<\/li>\n\n\n\n<li><strong>Object-oriented programming:<\/strong>&nbsp;&nbsp;The function&nbsp;&nbsp;<code>range()<\/code>&nbsp;is often used in object-oriented programming to generate sequences of values \u200b\u200bfor testing programs.<\/li>\n\n\n\n<li><strong>Image processing:<\/strong>&nbsp;&nbsp;&nbsp;Developers can use Python to generate sequences of numbers and apply them in image processing, such as filtering algorithms, etc.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p>The&nbsp;&nbsp;<code>range<\/code>&nbsp;Python function is a very useful and versatile built-in function for generating sequences of contiguous integers.&nbsp;Thus people widely use the Python language in various applications, which include mathematical calculations, program testing, random number generation, object-oriented programming, and image processing.&nbsp;Although the class&nbsp;&nbsp;<code>range()<\/code>&nbsp;is a more modern way of accessing the sequence of contiguous integers, the function is still widely used and is a popular choice for many developers.&nbsp;Although the class&nbsp;&nbsp;<code>range<\/code>&nbsp;is more complex than the function&nbsp;&nbsp;<code>xrange()<\/code>, the function&nbsp;&nbsp;<code>range()<\/code>&nbsp;is more efficient and more readable for developers.&nbsp;In short, the function&nbsp;<code>range()<\/code>&nbsp;is a valuable tool for anyone working with Python and is widely used in many projects.&nbsp;Also learn about&nbsp;<a href=\"https:\/\/www.copahost.com\/blog\/scan-python\/\">Scan in python<\/a>&nbsp;!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The range function is a built-in function in Python that allows you to generate sequences of numbers in a simple and controlled way.&nbsp;Thus, the function is very useful in many cases, from a simple iteration over a list or dictionary to creating checklists and other more complex cases. In this article, we&#8217;ll explore the functionality [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":3515,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[174],"tags":[],"class_list":["post-3514","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python range: the complete range function guide - Copahost<\/title>\n<meta name=\"description\" content=\"Learn how to use the range function in Python, its syntax, practical examples and the comparison of the range function with other functions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.copahost.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=\"Python range: the complete range function guide - Copahost\" \/>\n<meta property=\"og:description\" content=\"Learn how to use the range function in Python, its syntax, practical examples and the comparison of the range function with other functions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.copahost.com\/blog\/python-range\/\" \/>\n<meta property=\"og:site_name\" content=\"Copahost\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-01T09:58:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-03T12:57:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1080\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Schenia T\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Schenia T\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/\"},\"author\":{\"name\":\"Schenia T\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f\"},\"headline\":\"Python range: the complete range function guide\",\"datePublished\":\"2023-08-01T09:58:20+00:00\",\"dateModified\":\"2023-08-03T12:57:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/\"},\"wordCount\":2355,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.copahost.com\/blog\/python-range\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/python-range\/\",\"name\":\"Python range: the complete range function guide - Copahost\",\"isPartOf\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png\",\"datePublished\":\"2023-08-01T09:58:20+00:00\",\"dateModified\":\"2023-08-03T12:57:05+00:00\",\"description\":\"Learn how to use the range function in Python, its syntax, practical examples and the comparison of the range function with other functions.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.copahost.com\/blog\/python-range\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/#primaryimage\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png\",\"width\":1080,\"height\":1080,\"caption\":\"python range\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.copahost.com\/blog\/python-range\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.copahost.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python range: the complete range function guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#website\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"name\":\"Copahost\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.copahost.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#organization\",\"name\":\"Copahost\",\"url\":\"https:\/\/www.copahost.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"contentUrl\":\"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png\",\"width\":223,\"height\":40,\"caption\":\"Copahost\"},\"image\":{\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f\",\"name\":\"Schenia T\",\"description\":\"Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!\",\"url\":\"https:\/\/www.copahost.com\/blog\/author\/schenia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python range: the complete range function guide - Copahost","description":"Learn how to use the range function in Python, its syntax, practical examples and the comparison of the range function with other functions.","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:\/\/www.copahost.com\/blog\/python-range\/","og_locale":"en_US","og_type":"article","og_title":"Python range: the complete range function guide - Copahost","og_description":"Learn how to use the range function in Python, its syntax, practical examples and the comparison of the range function with other functions.","og_url":"https:\/\/www.copahost.com\/blog\/python-range\/","og_site_name":"Copahost","article_published_time":"2023-08-01T09:58:20+00:00","article_modified_time":"2023-08-03T12:57:05+00:00","og_image":[{"width":1080,"height":1080,"url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png","type":"image\/png"}],"author":"Schenia T","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Schenia T","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.copahost.com\/blog\/python-range\/#article","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/python-range\/"},"author":{"name":"Schenia T","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f"},"headline":"Python range: the complete range function guide","datePublished":"2023-08-01T09:58:20+00:00","dateModified":"2023-08-03T12:57:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/python-range\/"},"wordCount":2355,"commentCount":0,"publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/python-range\/#primaryimage"},"thumbnailUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.copahost.com\/blog\/python-range\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.copahost.com\/blog\/python-range\/","url":"https:\/\/www.copahost.com\/blog\/python-range\/","name":"Python range: the complete range function guide - Copahost","isPartOf":{"@id":"https:\/\/www.copahost.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.copahost.com\/blog\/python-range\/#primaryimage"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/python-range\/#primaryimage"},"thumbnailUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png","datePublished":"2023-08-01T09:58:20+00:00","dateModified":"2023-08-03T12:57:05+00:00","description":"Learn how to use the range function in Python, its syntax, practical examples and the comparison of the range function with other functions.","breadcrumb":{"@id":"https:\/\/www.copahost.com\/blog\/python-range\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.copahost.com\/blog\/python-range\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/python-range\/#primaryimage","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2023\/07\/range.png","width":1080,"height":1080,"caption":"python range"},{"@type":"BreadcrumbList","@id":"https:\/\/www.copahost.com\/blog\/python-range\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.copahost.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python range: the complete range function guide"}]},{"@type":"WebSite","@id":"https:\/\/www.copahost.com\/blog\/#website","url":"https:\/\/www.copahost.com\/blog\/","name":"Copahost","description":"","publisher":{"@id":"https:\/\/www.copahost.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.copahost.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.copahost.com\/blog\/#organization","name":"Copahost","url":"https:\/\/www.copahost.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","contentUrl":"https:\/\/www.copahost.com\/blog\/wp-content\/uploads\/2016\/03\/copahostlogo.png","width":223,"height":40,"caption":"Copahost"},"image":{"@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.copahost.com\/blog\/#\/schema\/person\/2efb96f9dfaf6162f347abcd06b1429f","name":"Schenia T","description":"Data scientist, passionate about technology tools and games. Undergraduate student in Statistics at UFPB. Her hobby is binge-watching series, enjoying good music working or cooking, going to the movies and learning new things!","url":"https:\/\/www.copahost.com\/blog\/author\/schenia\/"}]}},"_links":{"self":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/comments?post=3514"}],"version-history":[{"count":8,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3514\/revisions"}],"predecessor-version":[{"id":3542,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/posts\/3514\/revisions\/3542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media\/3515"}],"wp:attachment":[{"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/media?parent=3514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/categories?post=3514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.copahost.com\/blog\/wp-json\/wp\/v2\/tags?post=3514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}