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 – 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 – 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.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
- How to use Python decorator
- How to use the Python argparse module
- How to use Python queue
- How to use Python threading
- How to concatenate strings in Python
- How to use the Python absolute value
- How to use the Python comparison operators
- How to use the Python logical operators
- How to use Python if-elif-else statements
- How to use the Python printing method and indentation
- Python control flow – while and break
- Python control flow – for loop and range
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.
