Currently set to Index
Currently set to Follow
Skip main navigation

Python Loops

Loops are blocks of code that keep repeating the same instructions until a certain condition is met. We also use loops to access the different items in a given sequence and iterate them over the sequence. Loops are a fundamental concept in any programming language. Repetitive tasks are common in programming, so loops help save time and reduce errors.
Loops are blocks of code that keep repeating the same instructions until a certain condition is met. We also use loops to access the different items in a given sequence and iterate them over the sequence. Loops are a fundamental concept in any programming language. Repetitive tasks are common in programming, so loops help save time and reduce errors.

Now, let’s look more closely at for and while loops.

For loops

If you were to write a program to check the grades of a class with 50 students, you may want to use a loop to perform a ‘check grade’ task that stops looping when it reaches the end of the class list (i.e. the 50th student).
A for loop is mostly used for iterating over lists. In order to access a list item we call it by index. But what if we don’t know the index of some item or we don’t know how long the list is? This is where the for loop comes to the rescue:
The syntax for a for loop is:
for <variable> in <sequence>: 
 
Try this example in your code editor:
 
shopping_list = ['bread', 'milk', 'cereals', 'donuts', 'snacks']
for i in shopping_list:
 print(i)
 
Output:
 
bread 
milk 
cereals 
donuts 
snacks 
 
For the first iteration, the variable i will have the value ‘bread’; for the next iteration, the value for the same variable will change to ‘milk’, and then to ‘cereals’, and so on and so forth.
 
The for loop goes through every item in a list (i.e. iterates the list) and performs some operation (usually for every item in that list). The i is simply the iterator. By convention, i, k, and n are typical iterator names, but you can call them whatever you want.
 
For example, we can write:
 
for items in shopping_list:
 print(items)
 
Output:
 
bread 
milk 
cereals 
donuts 
snacks 
 
The program will work exactly the same and will generate the same output, no matter what you call the iterator.
 

While loops

 
A while loop is mostly used for iterating as long as the test expression or the condition is True. It is useful when you need to repeat a piece of code, without knowing the number of repetitions needed ahead of time. You might need to repeat some code until you receive specific user input, for example:
 
If you were to check the grades of students in a class but did not know how many students there were, you could use while to specify when it should stop looping – for example, specifying a condition that would no longer be met once the end of the loop is reached, such as ‘while there is a record for the student’s grade.’ Once it reaches the end of the list of grades, this condition would no longer be true and the loop would stop.
 
The syntax for a while loop is:
 
while test_condition
 statement(s)
 
As with other code structures with code blocks, notice the indentation before statement(s): it is important as it indicates the body of the loop. Here, test_condition is a Boolean expression. If the condition is True, the body of the while loop is executed. This, unlike the simple if statement, continues in a loop until the statement is False.
 
Here is a code example for you to try:
 
counter = 0
while counter < 3:
 counter+=1
 print(counter)
 
Output:
 
1 
2 
3 
This program is printing numbers from one to three. First, we declare a variable: an integer that has a value of zero. Then, we use a while loop with a condition that says ‘while the counter is less than three, do the following’. Inside the while loop, we increase the counter by one in each iteration and print its value. Once the counter is increased to three (which returns False, since it won’t be less than three anymore), our loop stops.
Essentially, to implement the while loop, give it a condition and put statements inside it, which will repeat as long as the given condition is True.

Apply your learning

Write a Python program that will serve as a basic to-do list. You should allow the user to:
  1. input their tasks one by one
  2. display the complete list if the user submits nothing (empty).
This article is from the free online

Introduction to Programming with Python

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education