Sitemap
Faizan Ahmed

Faizan Ahmed

Hi! my name is Faizan and I write about nearly everything but mostly fun stuff or nerd stuff

Loops in Python

4 min readJun 27, 2026

--

Press enter or click to view image in full size
Flow chart of for and while loops in python. Image taken from daily-dev-tips.com

Hi! My name is Faizan Ahmed, and I am currently pursuing Software Engineering. I have just completed my 4th semester and I am trying to learn AI/ML, and I got to know that to learn AI/ML you first have to learn about python. So, here I am. In this article, I am gonna talk about loops in python.

Get Faizan Ahmed’s stories in your inbox

Join Medium for free to get updates from this writer.

Loops are used to do a task in a loop (Woowww). Loops are used if you have to do the same task a number of times. Like, if you have to print 5 lines which say “hello World!”, you can use a loop which will print the sentence five times rather than using five print statements.

There are main two type of loops in python, which are for loop and while loop.

We are gonna start with the for loop and then we will jump to the while loop.

FOR LOOP

For loop is a loop which we used to iterate over a range. Like, for our print “hello World!“ example, for loop is the best loop to be used in this case. We are gonna take to examples, one is to print a certain range of numbers and the other is to print elements of a list.

EXAMPLE 1

To print a certain range of numbers, we will study the following piece of code:

for index in range(1,5): #will not include 5
print(index)

To write for loop, we use the keyword for (what a bold choice), and then we use a variable, which is used for iteration, in our code, we have used index as our iterative variable. After that we use the keyword in and then we specify the range, in our case the range is (1,5).

There is a catch, the second number(5) in the range is exclusive, the range will be from 1 to 4, and will not include 5.

After that we use indentation to specify the statements which are inside the for loop, which in our code is only print(index) which will print the numbers from 1 to 4 line by line.

EXAMPLE 2

In our 2nd example, we will print elements of a list using for loop. To explain this example, we will study the following piece of code.

friends = [“Faizan”, “Ahmed”, “Shaikh”, “Hussain”]
for friend in friends:
print(friend)
for i in range(4):
print(friends[i])
for i in range(len(friends)):
print(friends[i])

Firstly, we have declared a list of friends (you can change the names of friends if you have any), after that we have used three methods through which we can print the elements of our list. The first method is just using a variable friend, which will be our iterative variable and then iterate over the friends list by specifying it as our range. The second method, is using i as our iterative variable with a range of 4 as there are 4 elements in our list and then print friends[i], which will print all the elements of the list.

If we use range(4), then it is understood that the starting of the range is from 0 and it ends at 3, as 4 is exclusive.

The third method, is using i again as our iterative variable, and in range, rather than counting the number of elements manually, we give this job to python by using a built-in function which is len(friends) and then we print friends[i], which will print all the elements of the list.

len(name_of_the_list), for e.g, len(friends), is a built-in function in python, which returns the number of elements in a list, i.e, the length of the list.

Now, we will learn about the while loop.

WHILE LOOP

While loop which iterates based on a condition. I am gonna explain you while loop by using a simple example. We will use the code given below to understand while loop.

i=1
while i<=10:
print(“Hello world “+ str(i))
i+=1
print(“Done with loop”)

First we declare i, which is our iterative variable, and initialize it with 1, you can initialize it with any value, mainly we use 0 or 1. Then, we use the keyword while with a condition and a colon, then by using indentation we print “Hello world “ + the value of i in that iteration, and then we add 1 to i, and then we check the condition again and again, until it becomes false, when it becomes false, we print “Done with loop“.

Some important ideas you should remember when you are using while loop:

Firstly, always specify a condition, without it the loop will not run properly. Secondly, always increment your iterative variable, which in our case is i, by 1 or any other value, if you don’t do it, your loop will most probably run infinite times.

So, with that, the article loops in python comes to an end. I have tried my best to tell you guys about loops according to my knowledge and learning.

If you guys have any questions, you can ask me or google it. With that said, have a great day and remember LIFE GOES ON UNTIL IT GOES OFF. Byeeeeeeee!

--

--

Written by Faizan Ahmed

Hi! my name is Faizan and I write about nearly everything but mostly fun stuff or nerd stuff