Skip main navigation

What are loops in coding?

Python provides the keywords and code structure, called loops, for repeating a code snippet. This will make your code more efficient.
4.1
Python has two methods of repeating code blocks, for loops and while loops. For loops are mainly used for iterating over lists. In this case, iterate simply means working through each item in the sequence as part of a cycle. The for loop uses a variable to store the current item from the list as the cycle repeats. As the program moves through each iteration, the variable updates to the next item in the list. The for loop can have as many statements as you need as part of its code block. Use indents to separate the statements from the rest of the program.
42.8
After the code block runs for the last item in the list, the for loop stops. In contrast, a while loop repeats as long as a condition remains True. For example, the condition might be user input. The loop might repeat until the user types a specific word or presses a key. As another example, the condition might be a counter variable that updates on each iteration of the loop. When the counter reaches a certain number, the loop ends.
73.9
As with for loops, you can include as many statements as you need in the while loop. Just remember to indent every line that’s part of the code block. Use loops and make your programmes more efficient and powerful.

There are scenarios, based on the evaluation of a condition, where we may need to repeat a block of code. Imagine you are a software developer and you are required to provide a software module for all the employees in your department, for example.

To do this, you must print the payroll details of each employee separately. Printing the details of all the employees will be a tiresome task, so instead, you can use the logic for calculating the details and keep on iterating the same logic statement.

For such practical purposes, Python provides the keywords and code structure, called loops, for repeating a code snippet. This will save you time and make your code more efficient.

Watch the video to learn about loops and how they work in Python.

What is ‘for loops’?

We use for loops in Python to loop over a collection (like list or tuple) or an iterator, and perform some action repetitively.

The standard syntax for a for loop is:

for value in collection:
 # do something with value

For example, in the code snippet, we are printing all the values stored in a list.

Code:

alist = [10,20,30,40,50,5,56,57]
for num in alist:
... print(num)
...

 

Output:
10
20
30
40
50
5
56
57

Loop control statements

There are various loop control statements that are used to break the usual repetition of code. Let’s look at code snippets of what these loop statements look like.

Break

A for loop can be exited altogether using the break keyword. In the following code, we are summing up the elements of the list until a None value is encountered, at which point the termination is executed.

Code:

sequence = [1,2,3,4,None,6,None,8,9,10]
total = 0
for value in sequence:
... if value == None:
... break
... total+=value
...
print(total)

 

Output:
10

Continue

In the following code we are summing up all the elements in the list, skipping the None values.

Code:

total = 0
for value in sequence:
... if value is None:
 continue
... total += value
...
print(total)

Output: 43

This article is from the free online

Data Analytics and Python Fundamentals

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