Python Range Function|Range in Python Explained with Examples 2020

Python Range Function|Range in Python Explained with Examples 2020

In this tutorial, I will teach you about what is Range in Python and How to use it using different Example.

So to start our journey, let us visit first some of the frequently asked questions about range() in python.

Watch the video tutorial here before you proceed.

Frequently Asked Questions about Range in Python

What Is Range In Python?

The range() built-in function in python is used to returns a series or sequence of numbers starting with an index of zero(0) by default and Incremented by one(1) by default and will ends at a specified value or number.

How to use Range in Python?

To use range in python, you need to use For loop so that you can iterate over the sequence of numbers produced by the range.

What is the difference between range() and xrange() functions?

The range is the renamed version of xrange in Python(2.x).

At this time, we will continue further with our discussion using different example on how to use range in python.

Syntax of range() function and it’s arguments

Startis the starting from which the sequence of integers is to be returned.
StopIs the upper limit which is the end position of a sequence
StepIs the integer number specifying the incremental value with a default value of zero(0)
range() function arguments

Since have already learned the basic ideas about range() in python. At this point, we will now proceed how to use range in python using the actual coding.

For this tutorial, I am using python 3.8.2 version. you can download it in website. link and for this demonstration the Pycharm IDE.

Range in Python

  • Step 1: Open Pycharm IDE.

    When the Pycharm is open, you need to create a Project and name it as “rangetest”.

  • Step 2: Create Python file

    Right Click Project, click “new” and Select “Python File“.create python project

  • Step 3: Create Python File

    On the new python file, type “pyrange” as the name of this python file and press Enter.range python project

  • Step 4: Start Coding

    To start the coding of range() in python. add the simple code.
    Python range code

  • Step 5: Run Project

    To run python project, you can press “Shift + F10” or simply click the Play button.

  • Step 6: Check the result

    As expected, when you successfully run the project, the result will looks like as shown below.
    python range result

3 Variant of using range in python

At this time, we will now explore more examples of using the three variance of range function in python.

Range with One Argument

For the first variant, we will use one Argument:

Sample code:


Code Explanation:

In the above code, when the program executes the range() function with one argument. It creates a sequence of numbers from 0 – 9, then it prints the result with each item in the sequence.

Take note:

The range with one argument always start with 0 with an incremental step of 1.(start = 0 and step = 1)

See the Diagram Below:


Range with Two Arguments

Sample code of using range() function with two arguments:


Code Explanation:

In the example using range with two arguments which are the (start and stop). The program doesn’t have to start with 0 all the time, just like in our example that the start value is 2. Then it prints the result with each item in the sequence.

It means the we can use this range() function in python to generate a series of numbers from X to Y using a range(X, Y).

See the Diagram Below:

range(start, stop)

Range with Three Arguments

Sample code of using range() function with three arguments:


Code Explanation:

In the example using range with three arguments which are the (start, stop, step). This time we can select not only where the series of numbers will start and stop, but we can also specify the difference between each number and in our case, the step value is 2.

See the Diagram Below:

python range (start, Stop, Step)
python range (start, Stop, Step)

Important Points of Range in Python to Remember

  • The range() function will only work with integers(Whole numbers)
  • Only Integer is acceptable for argument and not string or float numbers or any other data type.
  • The Three arguments can be a positive or negative integer.
  • For Step value, it should not be equal to zero.

Suggested reading from the author

Let’s our Ideas!

This time, I wanted to know from you what else I should include to this range in python tutorial, just let me know by leaving your comment below.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.