Introduction To “Range”

Oindrila Chakraborty
4 min readAug 10

--

What is a “Range”?

  • 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

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 “1 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”

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 “Constructing” 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”

“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

I have 11+ 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.