In the previous session, you learnt about lists and various other data structures. Let’s look at a small example where you have a person’s income and expense data across five months in the form of a list, and you want to compute his savings across these five months. You may be thinking to do this manually by taking the first elements from the two lists and subtracting them, then again taking the second elements and subtracting, and so on. This may look simple, but let’s say it is across 10 or 20 years. Would you do the same?
This is where the concept of iteration comes in handy, as you are repeating the same operation multiple times. With this in mind, let’s learn more about it.
By now, you have understood the syntax of a for loop.
for val in seq : statements
A few things to note in the syntax include:
seq, which represents a sequence; it can be a list, a tuple or a string. To put it in a simple way it can be any iterable object.
The in operator, which, as you may recall, is a membership operator that helps to check for the presence of an element in a sequence.
But what role is the in operator playing here?
When you say for val in seq, it means that for a value in seq, it executes the set of statements under for block once and returns to for again and shifts the position to the next value. Let’s say you are at the last element in the seq. Here, the for block executes, and now, when it again goes to for val in seq, there are no more elements, and hence, val in seq returns a false and stops the execution.
Now that you have learnt about the for-statement, can you answer our initial question of calculating savings using a person’s income and expenses data? Let’s try this out.
Assume you have L1 = [10,20,30,24,18] (in thousands)
L2 = [8,14,15,20,10]
What you were doing manually is subtracting the first element of each list and then the second element, and so on. In other words, it is L1[i] - L2[i].
Since you need these indexes, let’s create a dummy array of five elements that represent the index positions L3 = [0,1,2,3,4].
Let’s implement the for loop using the list L3:
L1 = [10,20,30,24,18] L2 = [8,14,15,20,10] L3 = [0,1,2,3,4] for i in L3: L3[i] = L1[i] - L2[i]
Here, you are updating elements of L3 at each iteration. Now think whether you can use the same approach for a list with 1000 elements? To answer this, let's look at our next video:
The syntax of the range function is simple. Different implementations of range function include:
range(n): This creates a range object that has elements from 0 to n-1 [ both inclusive].
range(m,n): This creates a range object that has elements from m to n-1 [ both inclusive].
range(m,n,t): This creates a range object that has elements from m to n-1 with a step count of t. In other words, the range object has elements m, m+t, m+2t…so on. If t is negative, the elements would be decreasing, and that’s exactly what happens in the range (100,0,-1).
An important thing to note here is that you saw a way to create lists using the list function on the range object. list(range(0,10) would create a list of 10 elements.
Suppose a situation arises where the developer has no idea about the number of iterations he needs to have. Such iterations are called as Event-based iterations. Let's learn about them in detail.
In the video above, you learnt the concept of a while loop and its syntax. A while loop is used when you are not sure of the number of iterations that you need to have, and it should be used when your results depend on the value of a variable.
Let’s see how a while loop would be useful by taking our initial example defined at the start of the segment.
L1 = [10,20,30,24,18] L2 = [8,14,15,20,10] i =0 while i<len(L1): print(L1[i]-L2[i], end=' ') i=i+1
In the given code, a while loop and the len() function are being used, and the loop executes until the condition i < len(L1) fails. This condition is important, as it ensures whether or not you will be able to access all the elements.