Python Tutorial – Chapter 10 – Loops(Part 1) – For Loop

In chapter 10 of Python Tutorial, we will learn about Loops (Part 1) – For Loop

In programming, there may be instances where you need to execute a block of code multiple times.

Using loops in Python, you can execute block of code multiple times for set number of times or until a certain condition is met.

There are two types of loops in Python:

for loop

while loop

Using for loop, we can iterate over an iterable objects such as strings, lists, tuple, dictionaries and sets. With for loop, same block of code is executed repeatedly for each element in the iterable objects.

First, let’s look at the syntax and then, we will go for an example:

for value in iterable_object:  
    { code block } 

In this example, the variable value is used to hold the value of every element in the iterable_object before the iteration begins until this particular iteration is completed.

Until the last element in the iterable_object is reached, the loop iterates.

Also, please note the indentation for code block which is 4 spaces with the spacebar.

Now, let’s take the following example:

DataCamp Light | Standalone example
fruits = ["Apples", "Bananas", "Oranges", "Pineapples"] # iterate over each element in the list for fruit in fruits: print(fruit)

In the above example, I created a list “fruits”. As mentioned in the syntax earlier, fruit is the variable refers to each element in the list “fruits”.

On the first iteration of the loop, fruit points to the first element in the list, Apples. It prints out Apples to the console and goes for the second iteration pointing to the second element in the list, Bananas. It executes the same code again which is printing Bananas to the console.

The iteration will continue until all the elements in the list are covered.

In the above example, we iterated over a list, same can be done for tuples and strings as well. Take a look at the example below:

DataCamp Light | Standalone example
#tuple example fruits = ("Apples", "Bananas", "Oranges", "Pineapples") # iterate over each element in the tuple for fruit in fruits: print(fruit,"\n") #string example fruit = "Apple" #iterate over each character in the string for character in fruit: print(character)

With Python’s built-in range() function, you can iterate over a sequence of numbers. Please note that the range() function is zero based.

DataCamp Light | Standalone example
for i in range(5): print(i)

In the above example, it will start counting from 0, and increments by 1 on each iteration. At the end, it will print out the numbers 0,1,2,3,4.

Please note that the range will end at 4, and not at 5. So, if you want to include 5 as well then you should write as range(6).

Moving on, if you would like to print loop through a range say from 30 to 40(it will end at 39, want to include 40 as well then update it to 41). Check out the below example:

DataCamp Light | Standalone example
for i in range(30,40): print(i)

The default increment for range() function is 1. However, if you want the increase increment to more than 1 then, you can use step parameter. Syntax for this is as below:

range(start, end, step)

Let’s look at an example:

DataCamp Light | Standalone example
for i in range(30,40,2): print(i)

That brings to the end of chapter 10 – For Loop, see you in the next tutorial!!!

By the way, if you haven’t installed Python and Pycharm IDE on your Windows laptop, you can visit the below links to learn on how to install and configure/install Python and Pycharm IDE on Windows OS. This might be helpful in understanding the Python language easier.

How to Install Python 3 and Pycharm IDE on Windows

How to configure Pycharm IDE and “Run” your first Python 3 program

For your reference, to download Pycharm IDE, visit below link:

https://www.jetbrains.com/pycharm/download/?section=windows

However, if you don’t want to go through installation process, you can practice your learnings on this page itself by using integrated Python IDE above.

If you have any questions or comments about chapter 10 – For Loop, please let us know in the comments section below.

Leave a Reply