for
loopLet us look at a simple problem of printing numbers. We would like to print the first 5 non-negative integers. We have a different kind of a loop now, the for
loop:
xxxxxxxxxx
31for i in range(5):
2 print(i)
3# A dummy line
The output is:
xxxxxxxxxx
510
21
32
43
54
for
and in
are keywords in Python. range
is an object that represents a sequence of numbers. Line-2 is the body of the loop. An intuitive understanding of the code given above is as follows:
A visual representation is given below:
Similar to while
loops and if-else
blocks, the body of a for
loop should be indented.
range(5)
represents the following sequence: 0, 1, 2, 3, 4
. In general, range(n)
represents the sequence: 0, 1, ..., n - 1
. range
is quite versatile. The following code prints all two digit numbers greater than zero:
xxxxxxxxxx
21for i in range(10, 100):
2 print(i)
range(10, 100)
represents the sequence 10, 11, ..., 99
. In general, range(start, stop)
represents the sequence start, start + 1, ..., stop - 1
. Let us add another level of complexity. The following code prints all even two digit numbers greater than 0:
xxxxxxxxxx
21for i in range(10, 100, 2):
2 print(i)
range(10, 100, 2)
represents the sequence 10, 12, ..., 98
. In general, range(start, stop, step)
represents the sequence start, start + step, start + 2 * step, ..., last
, where last
is the largest element in this sequence that is less than stop
. This is true when the step
parameter is positive.
The following are equivalent:
range(n)
range(0, n)
range(0, n, 1)
So far we have seen only increasing sequences. With the help of a negative step size, we can also come up with decreasing sequences. The following code prints all two-digit even numbers greater than zero in descending order:
xxxxxxxxxx
21for i in range(98, 9, -2):
2 print(i)
For a negative step
value, range(start, stop, step)
represents the sequence start, start + step, start + 2 * step, ..., last
, where last
is the smallest element in the sequence greater than stop
.
Now, consider the following code:
xxxxxxxxxx
21for i in range(5, 5):
2 print(i)
range(5, 5)
is an empty sequence. So, the above code will not print anything. Another instance of an empty sequence:
xxxxxxxxxx
21for i in range(10, 5):
2 print(i)
The point to note is that neither of these code snippets produces any error. Finally, try executing the following snippet and observe the output.
xxxxxxxxxx
41##### Alarm! Wrong code snippet! #####
2for i in range(0.0, 10.0):
3 print(i)
4##### Alarm! Wrong code snippet! #####
Since a string is a sequence of characters, we can use the for
loop to iterate through strings. The following code will print each character of the string x
in one line:
xxxxxxxxxx
31word = 'good'
2for char in word:
3 print(char)
The output is:
xxxxxxxxxx
41g
2o
3o
4d
We can add some more code to enrich the output:
xxxxxxxxxx
51word = 'good'
2count = 1
3for char in word:
4 print(char, 'occurs at position', count, 'in the string', word)
5 count = count + 1
The output is:
xxxxxxxxxx
41g occurs at position 1 in the string good
2o occurs at position 2 in the string good
3o occurs at position 3 in the string good
4d occurs at position 4 in the string good