FOR Loops in Python: For Python Index, Python Range Function, and More

for python index

Python is great as a first language because it is concise and easy to read. The syntax rules of Python are easy to understand and allow you to do your work in minimal lines of code. In Python you can use English keywords instead of more punctuation, unlike other programming languages. 

It also has one of the biggest community support and is one of the most popular languages in many career fields.

What is a Loop?

In computer programming, a loop is a sequence of instructions that is repeated until a certain condition is reached. 

In a loop, an operation is done, such as incrementing an integer value, and then some condition is checked such as whether a counter has reached a specific number. If the counter has not reached the desired number, the next instruction in the sequence returns to the first instruction in the sequence and repeats it. 

If the condition has been reached, the next instruction “falls through” to the next sequential instruction or branches outside the loop.

FOR Loops in Python

The for loop in Python is used to iterate over a sequence (list or array) or other iterable objects. 

Syntax of for Loop

for val in sequence:
   {action to take within the loop}

Here, val is the variable that takes the value of the item inside the sequence on each iteration.

The loop runs until we reach the last item in the sequence. The body of the for loop is separated from the rest of the code using indentation. 

Fig. 1.0 is a visual representation of for loops for a better understanding of the for python index.

for python index
Fig. 1.0

Python Range Function for Python Index

Python provides a built in function range() that generates a sequence of numbers. Range(10) will generate numbers from 0 to 9 (10 numbers). 

For python indexing the range function allows users to define the start, stop and step size as “range(start, stop,step_size)”. Step_size defaults to 1 if not provided. The range object is “lazy” in a sense because it doesn’t generate every number that it “contains” when we create it.

However, it is not an iterator since it supports in, len and __getitem__ operations. This function does not store all the values in memory as it would be inefficient. So it remembers the start, stop, step size and generates the next number on the go.

Sample codes:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>print(range(2, 8))
[2, 3, 4, 5, 6, 7]
>>print(range(2, 20, 3))
[2, 5, 8, 11, 14, 17]

The range() function can also be used for python indexing to iterate through a sequence of numbers. It can be combined with the len() function to iterate through a sequence using indexing for example:

>>sports = ['Football', 'Cricket, 'Tennis', ‘Golf’]
>>for i in range(len(sports)):
>> print("I like", +sports[i])

I like Football
I like Cricket
​I like Tennis
​I like Golf

FOR Python Index Without Range Function on Lists

Instead of using the range function to access the values of a list using index, python allows you to take the value of the item inside the sequence on each iteration. 

For example:

>>sports = ['Football', 'Cricket, 'Tennis', ‘Golf’]
>>for sport in sports:
>> print("I like", +sport)

I like Football
I like Cricket
I like Tennis
​I like Golf

If you want to see the index along with the value, the built in python function enumerate() can be used in the following manner:

>>sports = ['Football', 'Cricket, 'Tennis', ‘Golf’]
>>for idx, val in enumerate(sports): 
>> print(idx + "I like", +val)

0 I like Football
1 I like Cricket
​2 I like Tennis
3 ​I like Golf

If Python is your choice of programming language, you might face issues with package compatibility with other packages or the python version you are using. You can fix any such problems with a Virtual Environment – check this guide out on how to create a Python Virtual Environment.


Leave a Reply