Introduction To Range Collection Data Type

Oindrila Chakraborty
4 min readAug 10, 2023

--

What is a Range in Python?

A Range is a Collection, rather than a container.

A Range is a type of Sequence that is used to represent an Arithmetic Progression of integers.

How Ranges are Created?

Ranges are created by calling the range () constructor.

There is no literal form to create a Range.

Most typically, when creating a Range by calling the range () constructor, only the Stop value of the Range to create is provided, and, Python assumes the Starting value to be 0, by default.

Create a Range by Providing Only the Stop Value to the range () Constructor

It is possible to supply a Starting value as well, by passing the two arguments to the range () constructor.

Create a Range by Providing Both the Starting Value and Stop Value to the range () Constructor

What is Half-Open Range Convention?

Ranges are sometimes used to create the consecutive integers to be used as loop counters.

In this case, the Stop value, which is supplied to the range () constructor, is one past the end of the Sequence, and, the Stop value of the Range is not displayed using the loop.

The situation, where the Stop value is not included in the Sequence of a Range, is called as the Half-Open Range Convention.

Display the Consecutive Integers of a Range, Defined with Only Stop Value, Using Loop
Display the Consecutive Integers of a Range, Defined with Both Starting Value and Stop Value, Using Loop

Insight into the Half-Open Range Convention Situation

The Half-Open Range Convention situation, i.e., the Stop value not being included in the Sequence of a Range may seem strange at first, but it actually makes a lot of sense when dealing with consecutive Ranges, because, the Stop value, specified by one Range is the Starting value of the next Range.

Wrapping the call to the range () constructor inside another call to the list () constructor is a handy way to force production of each item.

Call range () Constructor from Within list () Constructor

How to Create Range with Step Argument?

The range () constructor also supports a Step argument.

In order to use the Step argument in the range () constructor, it is mandatory to supply all three arguments to the range () constructor.

  • First argument is the Starting value
  • Second argument is the Stop value
  • Third argument is the Step value
Create a Range, which is Starting from 0, and, Ending with 10 with Step Argument as 2

Insight into the range () Constructor

Range is curious in that it determines what its arguments mean by counting the number of arguments -
1. Providing only one argument means that the argument is the Stop value.
2. Providing two arguments mean that the arguments are the Starting value, and, the Stop value respectively.
3. Providing three arguments mean that the arguments are the Starting value, the Stop value, and, the Step value respectively.

Python Range works in such a way that the first argument, i.e., the Starting value is made optional.

Python Range Does not support the Keyword Arguments.

Poorly Styled Written Code with Python Range

Following type of code should be avoided -

Example - A poor way of coding to print the elements in a List is by creating a Range over the length of the List, and, then Indexing into the List on each iteration.

Poor Way of Coding to Print the Elements in a List
  • Although the above style of code works, but it should not be used.
  • Instead, it is always preferable to use iteration over the objects.
Correct Way of Coding to Print the Elements in a List

What is Enumerate in Python?

If a counter is needed on another Iterable object, then the Python built-in function, i.e., enumerate can be used.

The enumerate function returns an Iterable Series of Pairs, where each Pair is a Tuple.

The first element of the Pair is the Index of the current Item.

The second element of the Pair is the Item itself.

Display All the Items of List Along with the Corresponding Index Positions in the List Using enumerator () Function
  • It is possible to use the Tuple Unpacking to avoid dealing with the Tuples directly.
Display All the Items of List Along with the Corresponding Index Positions in the List Using enumerator () Function with Tuple Unpacking

--

--

Oindrila Chakraborty
Oindrila Chakraborty

Written by Oindrila Chakraborty

I have 12+ experience in IT industry. I love to learn about the data and work with data. I am happy to share my knowledge with all. Hope this will be of help.