Python control flow – For loop and range function – with example

  • Reading time:17 mins read

Python control flow –  For loop and range function

Loops are an important part of Python and other programming languages. Besides being able to iterate over iterables, they can also be used to control the flow of a program.

In this article, we will explore how to use the for loop to iterate through a sequence. We will also learn how we can construct lists using the range function and how you can combine the range function and the for loop.

 

 

python control flow

Python control flow – Basics

We are going to begin by creating a list with three items. To iterate over the list we will first type the keyword ‘for’ followed by ‘item in items’,  where item is a variable that will take the value inside the list on each iteration.

Now in the subsequent indented block which is the body of the loop, we can write code that will perform the operation we want on the items from the list. However in this case we are just going to print out the items in the list.

 

 

There are various actions that can be performed in the body of the for loop, for example, you can perform mathematical operations and even call functions. In this case, the for loop iterates through the list the first time and assigns the element “John” to the item which is then printed out. This goes on for the other elements in the list.

Please note that there is a difference between ‘item’ and ‘items’. In this context, items refer to the whole list while ‘item’ is a variable and can be changed to any name to refer to each individual item from the list. Suppose we try to print out ‘items’ instead of ‘item’ in that case we will get the whole list printed out multiple times instead of individual items that are in the list.

 

 

Therefore you need to be careful and make sure that you are iterating as intended.

 

python control flow

 

Python control flow –  Range() function in Python

The range() function allows us to generate a sequence of numbers starting from zero. For example range(5) will generate a sequence of numbers starting from 0 to 4.

Using the range function we can easily create a list of numbers using the list() function and the range function as shown below.

 

 

As shown above, the range function generates a sequence of numbers starting from zero and increments by 1 but does not include the 10 in the list. We can also override the defaults and declare different values as the start, step and stop parameters. 

In the example below the list starts from 10, increments by 10 and stops at 90 since 100 the stop parameter is normally not included.

 

 

We can also use the range() function within a for loop to generate a sequence of numbers as shown below.

 

 

Using the list from our earlier examples we can also use the len() function and the range() function to return the index numbers of the items in the list as shown here.

 

 

Using the index numbers that we have generated you can index the items in the list and return their actual values instead.

 

 

For the avoidance of confusion that can be as a result of item and items, we can instead use x we had done before.

 

 

A better approach to the above example in Python is by instead using the enumerate function. The enumerate function will return the index and the corresponding to that item as shown below.

 

 

python control flow

 

Summary

So this is how you can handle Python control flow in a nutshell. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.