-
Integers, Floats and Strings 3
-
Lecture1.1
-
Lecture1.2
-
Lecture1.3
-
-
If Statements 3
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
-
Lists, Sets and Tuples 5
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
-
Loops 3
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
-
Functions 3
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
-
Dictionaries 3
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
-
Assertions 2
-
Lecture7.1
-
Lecture7.2
-
-
Classes 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
-
-
Matplotlib 2
-
Lecture9.1
-
Lecture9.2
-
Looping over a Range
Loops¶
Loops are the way in which we iterate through a range of numbers. Get used to using them now because they are going to be crucial for programming. I am going to use the word iterable to be what we are iterating through. We have already seen a common type, the range, in the lesson prior. Recall that a range takes the form range(a, b) and returns the integers from a to b not including b.
The format of a for loop is:
for i in iterable:
execute code
The first line basically says to define a varaible i and set it equal to the first value in the iterable, then the second, then the third. The tabbed in code is what gets executed each time that variable is defined. So if the iterable is n elements long, the code will be executed n times, once for each item. Let’s create our first loop!
#Loops let you iterate through something
#In the case of range we go from a to b, but not including b, so in the below case 0, 1, 2, 3 and 4
#We assign these values to i and execute any code that is indented
for i in range(0,5):
print(i)
0
1
2
3
4
Of course you might want to do something more than just print it, what about printing the number squared?
#You could also print the number squared
for i in range(0,5):
print(i**2)
0
1
4
9
16
An important thing to understand is that you can change the variable in the tabbed area, but after the tabbed code has run it will be reset to whatever the next value in the iterable is. In the code below, we are going to loop through with a variable i then re-define it to be its square and print it. It will still get reset though! We will print the value of i before and after squaring to really make it obvious.
#The loop will overwrite the value of i each time it begins again
for i in range(0,5):
print(i)
i = i**2
print(i)
print()
0
0
1
1
2
4
3
9
4
16
Loops are especially useful when you want to do something like find the sum of all values after a function is applied to it. Let's say we have the numbers 0 to 4 again, and we want to find the value of the square of all these numbers. Loops will help us. Pay attention to how the counter variable is set outside of the loop, this is because otherwise it would keep getting re-defined to 0. Instead it gets set to 0 once at the top and then we add to it with the loop!
counter = 0
for i in range(0,5):
counter += i**2
print(counter)
0
1
5
14
30
Nested Loops¶
Just like nested if statements, we are able to use loop statements within loop statements. When we do this, you need to think about how if we do the outer loop n times, then in the inner loop will be run n times. So if the inner loop runs m times, then the total number of times the code on the inside will be run is n * m. Below, we show how we run through two loops. An important thing that we need to do is to make sure we use a different variable to be assigned in the loops to keep track of them. In this case we use i in the outer loop and j in the inner loop.
#We could also have a loop within a loop
#You'll notice we have 15 things printed because we have 5 in the outter loop and 3 in the inner loop
#Each time we set i equal to a new number, we also go through all 3 loops of j
#So we call the j loop 5 times in total and each time we call the j loop we loop through it 3 times
for i in range(0,5):
for j in range(0,3):
print("First: "+str(i)+", Last: "+str(j))
First: 0, Last: 0
First: 0, Last: 1
First: 0, Last: 2
First: 1, Last: 0
First: 1, Last: 1
First: 1, Last: 2
First: 2, Last: 0
First: 2, Last: 1
First: 2, Last: 2
First: 3, Last: 0
First: 3, Last: 1
First: 3, Last: 2
First: 4, Last: 0
First: 4, Last: 1
First: 4, Last: 2
Break¶
Sometimes we have a reason that we need to stop a loop. In this case we can call break which ends the current loop. Below you will see how if the variable x is equal to 2 we are going to end the loop with break.
#If we call break then we stop a loop
for x in range(5):
print(x)
if x==2:
break
0
1
2
Break will only stop the current loop, this is very important to understand. If it stopped all loops, then in the code below we would only print out three lines. However, it only stops the current loop so we cycle through the inner loop 3 times and break, but do this 5 total times because of the outer loop. The code below should make it more clear.
#But break will only break the current loop, not any outter loops
for i in range(0,5):
for j in range(0,5):
print("First: "+str(i)+", Last: "+str(j))
if j ==2:
break
First: 0, Last: 0
First: 0, Last: 1
First: 0, Last: 2
First: 1, Last: 0
First: 1, Last: 1
First: 1, Last: 2
First: 2, Last: 0
First: 2, Last: 1
First: 2, Last: 2
First: 3, Last: 0
First: 3, Last: 1
First: 3, Last: 2
First: 4, Last: 0
First: 4, Last: 1
First: 4, Last: 2