Looping in a list is used to access every element in the list.
There are two tyes of looping.
Normal for-in loop as specified in line 8 & 9.
Range of length loop as specified in line 13 & 14.
Try Yourself:
There are two tyes of looping.
Normal for-in loop as specified in line 8 & 9.
Range of length loop as specified in line 13 & 14.
Try Yourself:
- #List looping example
- colors_list = ["red", "green", "yellow", "violet"]
- months_list = ["Jan", "Feb", "Sep", "Mar"]
- print "Colours in the list: "
- # Normal Looping
- for i in range(len(colors_list)):
- print(colors_list[i])
- # Range Looping
- print "Months in the list: "
- for month in months_list:
- print(month)
The lines 8 & 9 are like normal for loop; where each element in the list are accessed using the index value. The lines 13 & 14 are special case; where every elements in the list of months_list is taken. The iteration stops when all the elements in the list are exhausted.
Comments
Post a Comment