Loops and lists: an overview
Contents
Loops and lists: an overview#
Using loops is very important for writing efficient code. Loops are used to perform the same calculation multiple times, for example iterating over all items in a list, or until some condition is met. The most common loop type we’ll come accross is the for loop
For loop characteristics#
The example code below shows the structure of a for loop. This code is not interactive, it’s just to show you the basic syntax.
for x in range(10):
#instructions for operating on x go inside the loop
print(x)
print('Done')
Using the above example, we can see a for loop has the following characteristics:
A variable we use to access each item in a sequence
A sequence to iterate over (defined here using the inbuilt
rangefunction).Some operations performed on each item in the sequence
A for loop might be written inside a function, and we will see examples of this today.