VOOZH about

URL: https://thenewstack.io/python-for-beginners-the-range-function/

⇱ Python for Beginners: The Range() Function | The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-02-15 08:00:59
Python for Beginners: The Range() Function
tutorial,
Data / Software Development

Python for Beginners: The Range() Function

Python's range function helps you to quickly generate a count of numbers within a range. Let's dive into the features of the range() function.
Feb 15th, 2022 8:00am by Jack Wallen
👁 Featued image for: Python for Beginners: The Range() Function

Welcome back to Python for Beginners! In this series, we’re breaking down Python into very small, digestible chunks so anyone (of any skill level) can learn how to program with this user-friendly, versatile language. So far we’ve covered functions, lists, if/else statements, saving input to a file, and and/or operators. We’ve put these pieces together to help you see how applications are created and how to build functioning tools such as calculators.

This time around, we’re going to talk about the range() function. Although you might not see the value of this function at first, eventually you’ll find it to be a very helpful tool.

What Is the range() function?

Simply put, the range() function makes it possible to generate a sequence of numbers between a given range. For example, you might want to print out (or use) a range of numbers from 0-9. Depending on how arguments are passed through the function, a user can determine where the series of numbers begins and ends.

The range() function accepts three arguments:

  • start – integer where the range starts.
  • stop – integer before the sequence is to end.
  • step – how to increment between each integer in the sequence.

We’ll obviously start off with something simple, so you can easily understand the concept of the range() function.

The Basics of range()

Let’s first use the Python console to demonstrate how range works. Log in to the console with the command:

python3

Type the following command:

list(range(1, 10))

You should see the following output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Wait, wasn’t our range from 1 to 10? Where’s the 10? That’s where it gets a bit tricky. You see 1 is our start but the very definition of stop is the integer before the sequence is to end. So if you wanted to print out a range from 1-10 (including the 10), the command would be:

list(range(1, 11))

The above command would output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Let’s add a for loop into the mix to print a range from start to end. To do this, we’re going to add the end= parameter that will instruct Python what to do after each step in the range. This will make sense when you see the output. Still in the console, type:

for i in range(20):

Hit enter on your keyboard and then type (include the spaces before print):

    print(i, end= " ")

Hit enter twice and you’ll see the output:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

By using end= ” ” we tell Python to put a space between each number. If, however, our end statement was:

print(i, end= "\n")

The output would be:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Why is that? Because we added a newline sequence (\n) as the end parameter.

Let’s make this a bit more useful.

How to Use range() within Code

What we’re going to do first is allow the user to input our range. To do that we have to approach the inputting variables a bit differently than we have before. What we have to do is inform Python our input will be an integer. We’re also going to combine the text asking the user to input a number with the variable declaration.

Create the new file with:

nano range.py

The first thing we’ll do is ask the user to enter a number to serve as the start of our range. That line is:

x = int(input("Type a number to start the range:"))

The line which asks the user to input a number to end the range is:

y = int(input("Type a number to end the range:"))

Next, we’ll use a for loop to print the range from the user’s input. That statement looks like this:

for i in range(x, y):

print(i)

So our entire program looks like:

#Request the start of the range
x = int(input("Type a number to start the range:"))

#Request the end of the range
y = int(input("Type a number to end the range:"))

#Print the entire range
for i in range(x, y):
    print(i)

Save and close the file. Run the program with:

python3 range.py

When prompted, enter the start and end points and the program will print out the range.

Simple.

How to Use the Step Option

Let’s get a bit more creative and make use of the step option. What this does is allow you to define the increments, by step number, for a range. So instead of printing every number in the range from 1-20, we can tell Python to only print every other number.

Let’s do just that. We’ll go back to using the console. Log in to the Python console with:

python3

Remember our first command:

list(range(1, 10))

If we add a third number to that, we instruct Python on how to iterate the range. So if we issue the command:

list(range(1, 10, 2))

our output would be:

[1, 3, 5, 7, 9]

If we wanted even numbers, we’d have to start our range with 0, like this:

list(range(0, 10, 2))

Pretty simple.

You can also use a range that prints in reverse order. For example, type:

list(range(10, -10, -1))

The output of the above command would be:

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9]

Or we could step the iteration by -2 with:

list(range(10, -10, -2))

The output of the above command would be:

[10, 8, 6, 4, 2, 0, -2, -4, -6, -8]

Remember, to exit out of the Python console, type:

exit()

Stepping with Decimal Points

Let’s get creative and make use of the NumPy library. With the help of NumPy and the arrange() function, we can use numbers with decimal points as both range and step. Create a new file with:

nano range1.py

The first line in our file will import the NumPy library as np:

import numpy as np

Next, we’ll create a for loop to print our range with:

for i in np.arrange(0, 20.5, 0.5):
    print(i, end=', l)

So our full application looks like this:

import numpy as np

for i in np.arrange(0, 20.5, 0.5):
    print(i, end=', l)

Save and close the file and run it with:

python3 range2.py

The output for this program will be:

0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0, 11.5, 12.0, 12.5, 13.0, 13.5, 14.0, 14.5, 15.0, 15.5, 16.0, 16.5, 17.0, 17.5, 18.0, 18.5, 19.0, 19.5, 20.0

As you can see, we’ve stepped the range by .5 from start to end.

And there you go. You now know how to use the range() function in various ways with Python. Your range of skills with Python will continue to grow.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Simply.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.