When you’re 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’s where the Python
range() method comes in. The
range() method allows you to generate a sequence of numbers between a start and end number.
This tutorial will discuss, with examples, the basics of the Python
range() function and how you can use it in your code. By the end of reading this tutorial, you’ll be an expert at using the Python
range() built-in function returns an immutable sequence of numbers between a certain range.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
range() function can be used in one of two ways.
range() function can accept one argument, which is the number at which the sequence of integers should stop generating. By default, the
range() function will start at 0. Here’s the syntax for this usage of
range() 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
range(start, stop, [step])
The parameters accepted by this syntax are:
start, which refers to the starting value of the range.
stop, which refers to the final value in the range.
step(optional), which refers to the increment between each number.
Python Range Examples
Let’s walk through a few examples to illustrate how the
range() method works.
range() with One Parameter
Suppose we want to generate a sequence of numbers between 0 and 10. We could do so using the following code:
numbers = range(10) print(list(numbers))
Our code returns:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Let’s 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
Next, we use
list() which returns a list of our numbers. By default,
range() 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.
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.
range() with Two Parameters
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:
numbers = range(5, 9) for n in numbers: print(n)
Our code returns:
5 6 7 8
Let’s break down our code. On the first line, we use the
range() Python method to generate a sequence of numbers between 5 and 9.
Then, we start a for loop that iterates through every item in the sequence. Because the
range() 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.
range() with Three Parameters
Now, let’s 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.
We could achieve this goal by specifying three parameters with the
range() function. Here’s the code we would use:
numbers = range(10, 20, 2) for n in numbers: print(n)
Our code returns:
10 12 14 16 18
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.
range() function allows you to generate a sequence of numbers in Python.
range() 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.
This tutorial discussed the basics of the
range() function and how you can use it in your code. Now you have the knowledge you need to start using the
range() function like a Python expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.