Python is one of few programming languages which escaped the normal trend of the programming language syntax and completely revolutionized the syntax pattern in programming languages. One of the most interesting leaps is the change in the for-loop syntax.
The for
loop in Python uses the in
keyword and the built-in function range to iterate through a range of numbers. The range function is independent of the for loop, and so is the in
keyword. They can be used in different situations as well.
This new leap in the syntax pattern that Python took from other programming languages is for sure a lot more convenient, but it also creates a lot of confusion among the junior programmers that switched to Python from other low-level programming languages like C or C++.
In this article, we’re going to learn about the range
function and see how can we use it in different situations to iterate through a particular range of numbers.
What is a "for"
loop?
A for loop is a way to iterate over a sequence of items. Now these items can be anything. It can be an array of numbers, a set of characters, a dictionary, or even a range of numbers. It can be used to run a piece of code n
number of times. The syntax for ‘for loop’ is:
for <iterator> in <item-sequence>:
#your code goes here
Related: Learn more about the for-loops.
How to iterate over a range of number?
We can iterate over a range of numbers using the range
function in a for loop.
“Range
” function
The range function allows you to create a sequence of numbers with a particular step size. The step size is set to 1 by default. The starting value is set to 0 by default, and we just need to pass the ending value. The ending value passed in the range
function is not inclusive. This may be a little confusing, so let’s take an example to understand it better.
So let’s say we want to print a range of numbers from 0-9 in order. The range
function takes 3 arguments – starting value, ending value, and the step size. We know that the starting value is 0 and the step size is 1 by default. So we would just need to pass the ending value here. The last number of our sequence is 9. The ending value passed in the range function would be excluded. So we will have to pass 10 as the only argument in the range function to print this sequence.
for i in range(10):
print(i)

So we just used the for loop to iterate over this range of numbers.
range
function with two arguments – starting and ending value
Now if you want it to print all these numbers except 0 you’ll have to specify to ending value. You can simply pass your desired starting value in the range function.
Let’s see how to print a sequence of numbers from 0 to 9.
for i in range(10):
print(i)

enumerate()
function
The enumerate function is another function that is somewhat similar to the range function. The enumerate function takes a sequence as input and converts it into an enumerate object which is a sequence of tuples of index, value pairs of the sequence we’re enumerating.
We can create a range of numbers and enumerate the list to get the key-value pair of each element. Let’s see it in code.
result = list(enumerate(range(10)))
print(result)

Now, the enumerate function has a second argument where you can specify the starting value. So if you’re traversing through a sequence, you can manipulate the starting value of the index to change the indexing.
Let’s try to print the the sequence with the starting index as 1.
result = list(enumerate(range(10), start = 1))
print(result)

List comprehension
List comprehension is a way to create lists in short syntax. They use for loop in pair with range function to create a list in a single line. So instead of printing the range of numbers, using list comprehension you can store the range of numbers in a list.
Let’s again try to understand it with the code itself.
Related: Learn list comprehensions in depth.
my_list = [x for x in range(10)]
print(my_list)

List comprehension for range of numbers from 1 to n
To create a list through list comprehension we’ll simply input the starting value argument in the range function like before.
my_list = [x for x in range(1,10)]
print(my_list)

Combining list comprehension and enumerate function
Now that we’ve learned the enumerate function and the list comprehension let’s combine both tools and create a list of tuples with index-value pairs of all the numbers from 1-9 with index 1-9.
indexed_list = [(index, value) for index, value in enumerate(range(1,10), start=1)]
print(indexed_list)

In the above block of code, we just combined everything that we learned just now and created a list of tuples from range 1-9 with index 1-9. We used the range function to create a sequence of numbers 1-9. Then we used the enumerate function to create the tuples of index-value pairs with starting index of 1. Then we used the list comprehension to create a list of these tuples.
Applications and uses of specifying the starting value
All this that we learnt in this article is really useful in a lot of fields of programming. It has applications in data analysis, web development, machine learning and a lot more. They are used to generate Fibonacci series, index one-based arrays, print ordinal numbers, and many more.
Conclusion
We learned about for loops, range function, enumerate function, and the most important thing – list comprehension in this article. All these topics are important to lay down the basics of modern programming. You can’t do efficient programming without them. They have applications in almost the entirety of programming. Make sure you get a good grip on all of them. Keep practicing as much as you can as these are the topics that will trouble you later if left unchecked.
References
Official Python Documentation.
Stack Overflow answer for the same question.